1 | <?php |
||
4 | class Parser |
||
5 | { |
||
6 | /** |
||
7 | * The path to the source file to parse |
||
8 | * |
||
9 | * @var string |
||
10 | */ |
||
11 | private $path; |
||
12 | |||
13 | /** |
||
14 | * @var \ReflectionClass |
||
15 | */ |
||
16 | private $refl; |
||
17 | |||
18 | /** |
||
19 | * Matches a test method beginning with the conventional "test" |
||
20 | * word |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | private static $testName = '/^test/'; |
||
25 | |||
26 | /** |
||
27 | * A pattern for matching test methods that use the @test annotation |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | private static $testAnnotation = '/@test\b/'; |
||
32 | |||
33 | 30 | public function __construct($srcPath) |
|
34 | { |
||
35 | 30 | if (!file_exists($srcPath)) { |
|
36 | 1 | throw new \InvalidArgumentException("file not found: " . $srcPath); |
|
37 | } |
||
38 | |||
39 | 29 | $this->path = $srcPath; |
|
40 | 29 | $declaredClasses = get_declared_classes(); |
|
41 | 29 | require_once($this->path); |
|
42 | 29 | $class = $this->getClassName($this->path, $declaredClasses); |
|
43 | 29 | if (!$class) { |
|
44 | 2 | throw new NoClassInFileException(); |
|
45 | } |
||
46 | try { |
||
47 | 27 | $this->refl = new \ReflectionClass($class); |
|
48 | 27 | } catch (\ReflectionException $e) { |
|
49 | throw new \InvalidArgumentException("Unable to instantiate ReflectionClass. " . $class . " not found in: " . $srcPath); |
||
50 | } |
||
51 | 27 | } |
|
52 | |||
53 | /** |
||
54 | * Returns the fully constructed class |
||
55 | * with methods or null if the class is abstract |
||
56 | * |
||
57 | * @return null|ParsedClass |
||
58 | */ |
||
59 | 27 | public function getClass() |
|
70 | |||
71 | /** |
||
72 | * Return reflection name with null bytes stripped |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | 27 | private function getCleanReflectionName() |
|
80 | |||
81 | /** |
||
82 | * Return all test methods present in the file |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | 27 | private function getMethods() |
|
100 | |||
101 | /** |
||
102 | * Return the class name of the class contained |
||
103 | * in the file |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | 29 | private function getClassName($filename, $previousDeclaredClasses) |
|
130 | |||
131 | /** |
||
132 | * @param $filename |
||
133 | * @param $className |
||
134 | * @return bool |
||
135 | */ |
||
136 | 11 | private function classNameMatchesFileName($filename, $className) |
|
141 | |||
142 | 4 | private function invertSlashes($className) |
|
146 | } |
||
147 |