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 | 31 | public function __construct($srcPath) |
|
34 | { |
||
35 | 31 | if (!file_exists($srcPath)) { |
|
36 | 1 | throw new \InvalidArgumentException("file not found: " . $srcPath); |
|
37 | } |
||
38 | |||
39 | 30 | $this->path = $srcPath; |
|
40 | 30 | $declaredClasses = get_declared_classes(); |
|
41 | 30 | require_once($this->path); |
|
42 | 30 | $class = $this->getClassName($this->path, $declaredClasses); |
|
43 | 30 | if (!$class) { |
|
|
|||
44 | 2 | throw new NoClassInFileException(); |
|
45 | } |
||
46 | try { |
||
47 | 28 | $this->refl = new \ReflectionClass($class); |
|
48 | } catch (\ReflectionException $e) { |
||
49 | throw new \InvalidArgumentException("Unable to instantiate ReflectionClass. " . $class . " not found in: " . $srcPath); |
||
50 | } |
||
51 | 28 | } |
|
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 | 28 | public function getClass() |
|
60 | { |
||
61 | 28 | return ($this->refl->isAbstract()) |
|
62 | ? null |
||
63 | 28 | : new ParsedClass( |
|
64 | 28 | $this->refl->getDocComment(), |
|
65 | 28 | $this->getCleanReflectionName(), |
|
66 | 28 | $this->refl->getNamespaceName(), |
|
67 | 28 | $this->getMethods() |
|
68 | ); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Return reflection name with null bytes stripped |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | 28 | private function getCleanReflectionName() |
|
80 | |||
81 | /** |
||
82 | * Return all test methods present in the file |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | 28 | private function getMethods() |
|
100 | |||
101 | /** |
||
102 | * Return the class name of the class contained |
||
103 | * in the file |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | 30 | private function getClassName($filename, $previousDeclaredClasses) |
|
123 | |||
124 | /** |
||
125 | * Search for the name of the unit test |
||
126 | * @param string[] $classes |
||
127 | * @param string $filename |
||
128 | * @return string|null |
||
129 | */ |
||
130 | 30 | private function _searchForUnitTestClass(array $classes, $filename) { |
|
131 | // TODO: After merging this PR or other PR for phpunit 6 support, keep only the applicable subclass name |
||
132 | 30 | $matchingClassName = null; |
|
133 | 30 | foreach ($classes as $className) { |
|
134 | 30 | $class = new \ReflectionClass($className); |
|
135 | 30 | if ($class->getFileName() == $filename) { |
|
136 | 28 | if ($class->isSubclassOf('PHPUnit\Framework\TestCase')) { |
|
137 | 28 | if ($this->classNameMatchesFileName($filename, $className)) { |
|
138 | 21 | return $className; |
|
139 | 18 | } else if ($matchingClassName === null) { |
|
140 | 27 | $matchingClassName = $className; |
|
141 | } |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | 25 | return $matchingClassName; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param $filename |
||
150 | * @param $className |
||
151 | * @return bool |
||
152 | */ |
||
153 | 28 | private function classNameMatchesFileName($filename, $className) |
|
158 | |||
159 | 18 | private function invertSlashes($className) |
|
163 | } |
||
164 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: