-
Notifications
You must be signed in to change notification settings - Fork 0
/
Moses.php
80 lines (70 loc) · 1.59 KB
/
Moses.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
namespace ArturZeAlves\MosesBundle;
use ArturZeAlves\MosesBundle\Writer\DefaultWriter;
class Moses
{
/**
* @var Writer
*/
private $writer;
/**
* Constructor
*
* @param DefaultWriter $writer
*/
public function __construct(DefaultWriter $writer)
{
$this->writer = $writer;
}
/**
* Gets the class name from a given PHP file
*
* @param string $filenam
*
* @return string
*/
public function getClassName($filename)
{
return str_replace(".php", "", basename($filename));
}
/**
* Gets the namespace from a given PHP file
*
* @param string $filename
*
* @return string
*/
public function getNamespace($filename)
{
$src = file_get_contents($filename);
if (preg_match('#^namespace\s+(.+?);$#sm', $src, $match)) {
return $match[1];
}
return null;
}
/**
* Guesses the namespace to be applied to the generated test class
*
* @param string $namespace
*
* @return string
*/
public function guessTestNamespace($namespace)
{
$parts = explode("\\", $namespace);
array_splice($parts, -1, 0, "Tests");
return implode("\\", $parts);
}
/**
* Generates the test class
*
* @param \ReflectionClass $reflection
* @param string $testNamespace
*
* @return string
*/
public function generate($reflection, $testNamespace)
{
return $this->writer->writeClass($reflection, $testNamespace);
}
}