1 | <?php |
||
27 | class ReflectionEngine |
||
28 | { |
||
29 | /** |
||
30 | * @var null|LocatorInterface |
||
31 | */ |
||
32 | protected static $locator = null; |
||
33 | |||
34 | /** |
||
35 | * @var array|Node[] |
||
36 | */ |
||
37 | protected static $parsedFiles = array(); |
||
38 | |||
39 | /** |
||
40 | * @var null|Parser |
||
41 | */ |
||
42 | protected static $parser = null; |
||
43 | |||
44 | /** |
||
45 | * @var null|NodeTraverser |
||
46 | */ |
||
47 | protected static $traverser = null; |
||
48 | |||
49 | private function __construct() {} |
||
50 | |||
51 | public static function init(LocatorInterface $locator) |
||
68 | |||
69 | /** |
||
70 | * Locates a file name for class |
||
71 | * |
||
72 | * @param string $fullClassName Full name of the class |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | 12 | public static function locateClassFile($fullClassName) |
|
77 | { |
||
78 | 12 | if (class_exists($fullClassName, false) |
|
79 | 1 | || interface_exists($fullClassName, false) |
|
80 | 12 | || trait_exists($fullClassName, false) |
|
81 | ) { |
||
82 | 12 | $refClass = new \ReflectionClass($fullClassName); |
|
83 | 12 | $classFileName = $refClass->getFileName(); |
|
84 | } else { |
||
85 | $classFileName = self::$locator->locateClass($fullClassName); |
||
86 | } |
||
87 | |||
88 | 12 | if (!$classFileName) { |
|
89 | throw new \InvalidArgumentException("Class $fullClassName was not found by locator"); |
||
90 | } |
||
91 | |||
92 | 12 | return $classFileName; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * Tries to parse a class by name using LocatorInterface |
||
97 | * |
||
98 | * @param string $fullClassName Class name to load |
||
99 | * |
||
100 | * @return ClassLike |
||
101 | */ |
||
102 | 12 | public static function parseClass($fullClassName) |
|
128 | |||
129 | /** |
||
130 | * Parses class method |
||
131 | * |
||
132 | * @param string $fullClassName Name of the class |
||
133 | * @param string $methodName Name of the method |
||
134 | * |
||
135 | * @return ClassMethod |
||
136 | */ |
||
137 | 1 | public static function parseClassMethod($fullClassName, $methodName) |
|
138 | { |
||
139 | 1 | $class = self::parseClass($fullClassName); |
|
140 | 1 | $classNodes = $class->stmts; |
|
141 | |||
142 | 1 | foreach ($classNodes as $classLevelNode) { |
|
143 | 1 | if ($classLevelNode instanceof ClassMethod && $classLevelNode->name == $methodName) { |
|
144 | 1 | return $classLevelNode; |
|
145 | } |
||
146 | } |
||
147 | |||
148 | throw new \InvalidArgumentException("Method $methodName was not found in the $fullClassName"); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Parses class property |
||
153 | * |
||
154 | * @param string $fullClassName Name of the class |
||
155 | * @param string $propertyName Name of the property |
||
156 | * |
||
157 | * @return array Pair of [Property and PropertyProperty] nodes |
||
158 | */ |
||
159 | 2 | public static function parseClassProperty($fullClassName, $propertyName) |
|
160 | { |
||
161 | 2 | $class = self::parseClass($fullClassName); |
|
162 | 2 | $classNodes = $class->stmts; |
|
163 | |||
164 | 2 | foreach ($classNodes as $classLevelNode) { |
|
165 | 2 | if ($classLevelNode instanceof Property) { |
|
166 | 2 | foreach ($classLevelNode->props as $classProperty) { |
|
167 | 2 | if ($classProperty->name == $propertyName) { |
|
168 | 2 | return [$classLevelNode, $classProperty]; |
|
169 | } |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | |||
174 | throw new \InvalidArgumentException("Property $propertyName was not found in the $fullClassName"); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Parses a file and returns an AST for it |
||
179 | * |
||
180 | * @param string $fileName Name of the file |
||
181 | * @param string|null $fileContent Optional content of the file |
||
182 | * |
||
183 | * @return \PhpParser\Node[] |
||
184 | */ |
||
185 | 139 | public static function parseFile($fileName, $fileContent = null) |
|
201 | |||
202 | /** |
||
203 | * Parses a file namespace and returns an AST for it |
||
204 | * |
||
205 | * @param string $fileName Name of the file |
||
206 | * @param string $namespaceName Namespace name |
||
207 | * |
||
208 | * @return Namespace_ |
||
209 | */ |
||
210 | 22 | public static function parseFileNamespace($fileName, $namespaceName) |
|
222 | |||
223 | } |
||
224 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.