Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of Psy Shell.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) 2012-2017 Justin Hileman
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Psy\CodeCleaner;
|
Chris@0
|
13
|
Chris@0
|
14 use PhpParser\Node;
|
Chris@0
|
15 use PhpParser\Node\Expr;
|
Chris@0
|
16 use PhpParser\Node\Expr\ClassConstFetch;
|
Chris@0
|
17 use PhpParser\Node\Expr\New_;
|
Chris@0
|
18 use PhpParser\Node\Expr\StaticCall;
|
Chris@0
|
19 use PhpParser\Node\Stmt;
|
Chris@0
|
20 use PhpParser\Node\Stmt\Class_;
|
Chris@0
|
21 use PhpParser\Node\Stmt\Do_;
|
Chris@0
|
22 use PhpParser\Node\Stmt\If_;
|
Chris@0
|
23 use PhpParser\Node\Stmt\Interface_;
|
Chris@0
|
24 use PhpParser\Node\Stmt\Switch_;
|
Chris@0
|
25 use PhpParser\Node\Stmt\Trait_;
|
Chris@0
|
26 use PhpParser\Node\Stmt\While_;
|
Chris@0
|
27 use Psy\Exception\FatalErrorException;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * Validate that classes exist.
|
Chris@0
|
31 *
|
Chris@0
|
32 * This pass throws a FatalErrorException rather than letting PHP run
|
Chris@0
|
33 * headfirst into a real fatal error and die.
|
Chris@0
|
34 */
|
Chris@0
|
35 class ValidClassNamePass extends NamespaceAwarePass
|
Chris@0
|
36 {
|
Chris@0
|
37 const CLASS_TYPE = 'class';
|
Chris@0
|
38 const INTERFACE_TYPE = 'interface';
|
Chris@0
|
39 const TRAIT_TYPE = 'trait';
|
Chris@0
|
40
|
Chris@0
|
41 protected $checkTraits;
|
Chris@0
|
42 private $conditionalScopes = 0;
|
Chris@0
|
43
|
Chris@0
|
44 public function __construct()
|
Chris@0
|
45 {
|
Chris@0
|
46 $this->checkTraits = function_exists('trait_exists');
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * Validate class, interface and trait definitions.
|
Chris@0
|
51 *
|
Chris@0
|
52 * Validate them upon entering the node, so that we know about their
|
Chris@0
|
53 * presence and can validate constant fetches and static calls in class or
|
Chris@0
|
54 * trait methods.
|
Chris@0
|
55 *
|
Chris@0
|
56 * @param Node
|
Chris@0
|
57 */
|
Chris@0
|
58 public function enterNode(Node $node)
|
Chris@0
|
59 {
|
Chris@0
|
60 parent::enterNode($node);
|
Chris@0
|
61
|
Chris@0
|
62 if (self::isConditional($node)) {
|
Chris@0
|
63 $this->conditionalScopes++;
|
Chris@0
|
64 } else {
|
Chris@0
|
65 // @todo add an "else" here which adds a runtime check for instances where we can't tell
|
Chris@0
|
66 // whether a class is being redefined by static analysis alone.
|
Chris@0
|
67 if ($this->conditionalScopes === 0) {
|
Chris@0
|
68 if ($node instanceof Class_) {
|
Chris@0
|
69 $this->validateClassStatement($node);
|
Chris@0
|
70 } elseif ($node instanceof Interface_) {
|
Chris@0
|
71 $this->validateInterfaceStatement($node);
|
Chris@0
|
72 } elseif ($node instanceof Trait_) {
|
Chris@0
|
73 $this->validateTraitStatement($node);
|
Chris@0
|
74 }
|
Chris@0
|
75 }
|
Chris@0
|
76 }
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * Validate `new` expressions, class constant fetches, and static calls.
|
Chris@0
|
81 *
|
Chris@0
|
82 * @throws FatalErrorException if a class, interface or trait is referenced which does not exist
|
Chris@0
|
83 * @throws FatalErrorException if a class extends something that is not a class
|
Chris@0
|
84 * @throws FatalErrorException if a class implements something that is not an interface
|
Chris@0
|
85 * @throws FatalErrorException if an interface extends something that is not an interface
|
Chris@0
|
86 * @throws FatalErrorException if a class, interface or trait redefines an existing class, interface or trait name
|
Chris@0
|
87 *
|
Chris@0
|
88 * @param Node $node
|
Chris@0
|
89 */
|
Chris@0
|
90 public function leaveNode(Node $node)
|
Chris@0
|
91 {
|
Chris@0
|
92 if (self::isConditional($node)) {
|
Chris@0
|
93 $this->conditionalScopes--;
|
Chris@0
|
94 } elseif ($node instanceof New_) {
|
Chris@0
|
95 $this->validateNewExpression($node);
|
Chris@0
|
96 } elseif ($node instanceof ClassConstFetch) {
|
Chris@0
|
97 $this->validateClassConstFetchExpression($node);
|
Chris@0
|
98 } elseif ($node instanceof StaticCall) {
|
Chris@0
|
99 $this->validateStaticCallExpression($node);
|
Chris@0
|
100 }
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 private static function isConditional(Node $node)
|
Chris@0
|
104 {
|
Chris@0
|
105 return $node instanceof If_ ||
|
Chris@0
|
106 $node instanceof While_ ||
|
Chris@0
|
107 $node instanceof Do_ ||
|
Chris@0
|
108 $node instanceof Switch_;
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 /**
|
Chris@0
|
112 * Validate a class definition statement.
|
Chris@0
|
113 *
|
Chris@0
|
114 * @param Class_ $stmt
|
Chris@0
|
115 */
|
Chris@0
|
116 protected function validateClassStatement(Class_ $stmt)
|
Chris@0
|
117 {
|
Chris@0
|
118 $this->ensureCanDefine($stmt);
|
Chris@0
|
119 if (isset($stmt->extends)) {
|
Chris@0
|
120 $this->ensureClassExists($this->getFullyQualifiedName($stmt->extends), $stmt);
|
Chris@0
|
121 }
|
Chris@0
|
122 $this->ensureInterfacesExist($stmt->implements, $stmt);
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * Validate an interface definition statement.
|
Chris@0
|
127 *
|
Chris@0
|
128 * @param Interface_ $stmt
|
Chris@0
|
129 */
|
Chris@0
|
130 protected function validateInterfaceStatement(Interface_ $stmt)
|
Chris@0
|
131 {
|
Chris@0
|
132 $this->ensureCanDefine($stmt);
|
Chris@0
|
133 $this->ensureInterfacesExist($stmt->extends, $stmt);
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 /**
|
Chris@0
|
137 * Validate a trait definition statement.
|
Chris@0
|
138 *
|
Chris@0
|
139 * @param Trait_ $stmt
|
Chris@0
|
140 */
|
Chris@0
|
141 protected function validateTraitStatement(Trait_ $stmt)
|
Chris@0
|
142 {
|
Chris@0
|
143 $this->ensureCanDefine($stmt);
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 /**
|
Chris@0
|
147 * Validate a `new` expression.
|
Chris@0
|
148 *
|
Chris@0
|
149 * @param New_ $stmt
|
Chris@0
|
150 */
|
Chris@0
|
151 protected function validateNewExpression(New_ $stmt)
|
Chris@0
|
152 {
|
Chris@0
|
153 // if class name is an expression or an anonymous class, give it a pass for now
|
Chris@0
|
154 if (!$stmt->class instanceof Expr && !$stmt->class instanceof Class_) {
|
Chris@0
|
155 $this->ensureClassExists($this->getFullyQualifiedName($stmt->class), $stmt);
|
Chris@0
|
156 }
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@0
|
159 /**
|
Chris@0
|
160 * Validate a class constant fetch expression's class.
|
Chris@0
|
161 *
|
Chris@0
|
162 * @param ClassConstFetch $stmt
|
Chris@0
|
163 */
|
Chris@0
|
164 protected function validateClassConstFetchExpression(ClassConstFetch $stmt)
|
Chris@0
|
165 {
|
Chris@0
|
166 // there is no need to check exists for ::class const for php 5.5 or newer
|
Chris@0
|
167 if (strtolower($stmt->name) === 'class'
|
Chris@0
|
168 && version_compare(PHP_VERSION, '5.5', '>=')) {
|
Chris@0
|
169 return;
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 // if class name is an expression, give it a pass for now
|
Chris@0
|
173 if (!$stmt->class instanceof Expr) {
|
Chris@0
|
174 $this->ensureClassOrInterfaceExists($this->getFullyQualifiedName($stmt->class), $stmt);
|
Chris@0
|
175 }
|
Chris@0
|
176 }
|
Chris@0
|
177
|
Chris@0
|
178 /**
|
Chris@0
|
179 * Validate a class constant fetch expression's class.
|
Chris@0
|
180 *
|
Chris@0
|
181 * @param StaticCall $stmt
|
Chris@0
|
182 */
|
Chris@0
|
183 protected function validateStaticCallExpression(StaticCall $stmt)
|
Chris@0
|
184 {
|
Chris@0
|
185 // if class name is an expression, give it a pass for now
|
Chris@0
|
186 if (!$stmt->class instanceof Expr) {
|
Chris@0
|
187 $this->ensureMethodExists($this->getFullyQualifiedName($stmt->class), $stmt->name, $stmt);
|
Chris@0
|
188 }
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 /**
|
Chris@0
|
192 * Ensure that no class, interface or trait name collides with a new definition.
|
Chris@0
|
193 *
|
Chris@0
|
194 * @throws FatalErrorException
|
Chris@0
|
195 *
|
Chris@0
|
196 * @param Stmt $stmt
|
Chris@0
|
197 */
|
Chris@0
|
198 protected function ensureCanDefine(Stmt $stmt)
|
Chris@0
|
199 {
|
Chris@0
|
200 $name = $this->getFullyQualifiedName($stmt->name);
|
Chris@0
|
201
|
Chris@0
|
202 // check for name collisions
|
Chris@0
|
203 $errorType = null;
|
Chris@0
|
204 if ($this->classExists($name)) {
|
Chris@0
|
205 $errorType = self::CLASS_TYPE;
|
Chris@0
|
206 } elseif ($this->interfaceExists($name)) {
|
Chris@0
|
207 $errorType = self::INTERFACE_TYPE;
|
Chris@0
|
208 } elseif ($this->traitExists($name)) {
|
Chris@0
|
209 $errorType = self::TRAIT_TYPE;
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 if ($errorType !== null) {
|
Chris@0
|
213 throw $this->createError(sprintf('%s named %s already exists', ucfirst($errorType), $name), $stmt);
|
Chris@0
|
214 }
|
Chris@0
|
215
|
Chris@0
|
216 // Store creation for the rest of this code snippet so we can find local
|
Chris@0
|
217 // issue too
|
Chris@0
|
218 $this->currentScope[strtolower($name)] = $this->getScopeType($stmt);
|
Chris@0
|
219 }
|
Chris@0
|
220
|
Chris@0
|
221 /**
|
Chris@0
|
222 * Ensure that a referenced class exists.
|
Chris@0
|
223 *
|
Chris@0
|
224 * @throws FatalErrorException
|
Chris@0
|
225 *
|
Chris@0
|
226 * @param string $name
|
Chris@0
|
227 * @param Stmt $stmt
|
Chris@0
|
228 */
|
Chris@0
|
229 protected function ensureClassExists($name, $stmt)
|
Chris@0
|
230 {
|
Chris@0
|
231 if (!$this->classExists($name)) {
|
Chris@0
|
232 throw $this->createError(sprintf('Class \'%s\' not found', $name), $stmt);
|
Chris@0
|
233 }
|
Chris@0
|
234 }
|
Chris@0
|
235
|
Chris@0
|
236 /**
|
Chris@0
|
237 * Ensure that a referenced class _or interface_ exists.
|
Chris@0
|
238 *
|
Chris@0
|
239 * @throws FatalErrorException
|
Chris@0
|
240 *
|
Chris@0
|
241 * @param string $name
|
Chris@0
|
242 * @param Stmt $stmt
|
Chris@0
|
243 */
|
Chris@0
|
244 protected function ensureClassOrInterfaceExists($name, $stmt)
|
Chris@0
|
245 {
|
Chris@0
|
246 if (!$this->classExists($name) && !$this->interfaceExists($name)) {
|
Chris@0
|
247 throw $this->createError(sprintf('Class \'%s\' not found', $name), $stmt);
|
Chris@0
|
248 }
|
Chris@0
|
249 }
|
Chris@0
|
250
|
Chris@0
|
251 /**
|
Chris@0
|
252 * Ensure that a statically called method exists.
|
Chris@0
|
253 *
|
Chris@0
|
254 * @throws FatalErrorException
|
Chris@0
|
255 *
|
Chris@0
|
256 * @param string $class
|
Chris@0
|
257 * @param string $name
|
Chris@0
|
258 * @param Stmt $stmt
|
Chris@0
|
259 */
|
Chris@0
|
260 protected function ensureMethodExists($class, $name, $stmt)
|
Chris@0
|
261 {
|
Chris@0
|
262 $this->ensureClassExists($class, $stmt);
|
Chris@0
|
263
|
Chris@0
|
264 // let's pretend all calls to self, parent and static are valid
|
Chris@0
|
265 if (in_array(strtolower($class), array('self', 'parent', 'static'))) {
|
Chris@0
|
266 return;
|
Chris@0
|
267 }
|
Chris@0
|
268
|
Chris@0
|
269 // ... and all calls to classes defined right now
|
Chris@0
|
270 if ($this->findInScope($class) === self::CLASS_TYPE) {
|
Chris@0
|
271 return;
|
Chris@0
|
272 }
|
Chris@0
|
273
|
Chris@0
|
274 // if method name is an expression, give it a pass for now
|
Chris@0
|
275 if ($name instanceof Expr) {
|
Chris@0
|
276 return;
|
Chris@0
|
277 }
|
Chris@0
|
278
|
Chris@0
|
279 if (!method_exists($class, $name) && !method_exists($class, '__callStatic')) {
|
Chris@0
|
280 throw $this->createError(sprintf('Call to undefined method %s::%s()', $class, $name), $stmt);
|
Chris@0
|
281 }
|
Chris@0
|
282 }
|
Chris@0
|
283
|
Chris@0
|
284 /**
|
Chris@0
|
285 * Ensure that a referenced interface exists.
|
Chris@0
|
286 *
|
Chris@0
|
287 * @throws FatalErrorException
|
Chris@0
|
288 *
|
Chris@0
|
289 * @param $interfaces
|
Chris@0
|
290 * @param Stmt $stmt
|
Chris@0
|
291 */
|
Chris@0
|
292 protected function ensureInterfacesExist($interfaces, $stmt)
|
Chris@0
|
293 {
|
Chris@0
|
294 foreach ($interfaces as $interface) {
|
Chris@0
|
295 /** @var string $name */
|
Chris@0
|
296 $name = $this->getFullyQualifiedName($interface);
|
Chris@0
|
297 if (!$this->interfaceExists($name)) {
|
Chris@0
|
298 throw $this->createError(sprintf('Interface \'%s\' not found', $name), $stmt);
|
Chris@0
|
299 }
|
Chris@0
|
300 }
|
Chris@0
|
301 }
|
Chris@0
|
302
|
Chris@0
|
303 /**
|
Chris@0
|
304 * Get a symbol type key for storing in the scope name cache.
|
Chris@0
|
305 *
|
Chris@0
|
306 * @param Stmt $stmt
|
Chris@0
|
307 *
|
Chris@0
|
308 * @return string
|
Chris@0
|
309 */
|
Chris@0
|
310 protected function getScopeType(Stmt $stmt)
|
Chris@0
|
311 {
|
Chris@0
|
312 if ($stmt instanceof Class_) {
|
Chris@0
|
313 return self::CLASS_TYPE;
|
Chris@0
|
314 } elseif ($stmt instanceof Interface_) {
|
Chris@0
|
315 return self::INTERFACE_TYPE;
|
Chris@0
|
316 } elseif ($stmt instanceof Trait_) {
|
Chris@0
|
317 return self::TRAIT_TYPE;
|
Chris@0
|
318 }
|
Chris@0
|
319 }
|
Chris@0
|
320
|
Chris@0
|
321 /**
|
Chris@0
|
322 * Check whether a class exists, or has been defined in the current code snippet.
|
Chris@0
|
323 *
|
Chris@0
|
324 * Gives `self`, `static` and `parent` a free pass.
|
Chris@0
|
325 *
|
Chris@0
|
326 * @param string $name
|
Chris@0
|
327 *
|
Chris@0
|
328 * @return bool
|
Chris@0
|
329 */
|
Chris@0
|
330 protected function classExists($name)
|
Chris@0
|
331 {
|
Chris@0
|
332 // Give `self`, `static` and `parent` a pass. This will actually let
|
Chris@0
|
333 // some errors through, since we're not checking whether the keyword is
|
Chris@0
|
334 // being used in a class scope.
|
Chris@0
|
335 if (in_array(strtolower($name), array('self', 'static', 'parent'))) {
|
Chris@0
|
336 return true;
|
Chris@0
|
337 }
|
Chris@0
|
338
|
Chris@0
|
339 return class_exists($name) || $this->findInScope($name) === self::CLASS_TYPE;
|
Chris@0
|
340 }
|
Chris@0
|
341
|
Chris@0
|
342 /**
|
Chris@0
|
343 * Check whether an interface exists, or has been defined in the current code snippet.
|
Chris@0
|
344 *
|
Chris@0
|
345 * @param string $name
|
Chris@0
|
346 *
|
Chris@0
|
347 * @return bool
|
Chris@0
|
348 */
|
Chris@0
|
349 protected function interfaceExists($name)
|
Chris@0
|
350 {
|
Chris@0
|
351 return interface_exists($name) || $this->findInScope($name) === self::INTERFACE_TYPE;
|
Chris@0
|
352 }
|
Chris@0
|
353
|
Chris@0
|
354 /**
|
Chris@0
|
355 * Check whether a trait exists, or has been defined in the current code snippet.
|
Chris@0
|
356 *
|
Chris@0
|
357 * @param string $name
|
Chris@0
|
358 *
|
Chris@0
|
359 * @return bool
|
Chris@0
|
360 */
|
Chris@0
|
361 protected function traitExists($name)
|
Chris@0
|
362 {
|
Chris@0
|
363 return $this->checkTraits && (trait_exists($name) || $this->findInScope($name) === self::TRAIT_TYPE);
|
Chris@0
|
364 }
|
Chris@0
|
365
|
Chris@0
|
366 /**
|
Chris@0
|
367 * Find a symbol in the current code snippet scope.
|
Chris@0
|
368 *
|
Chris@0
|
369 * @param string $name
|
Chris@0
|
370 *
|
Chris@0
|
371 * @return string|null
|
Chris@0
|
372 */
|
Chris@0
|
373 protected function findInScope($name)
|
Chris@0
|
374 {
|
Chris@0
|
375 $name = strtolower($name);
|
Chris@0
|
376 if (isset($this->currentScope[$name])) {
|
Chris@0
|
377 return $this->currentScope[$name];
|
Chris@0
|
378 }
|
Chris@0
|
379 }
|
Chris@0
|
380
|
Chris@0
|
381 /**
|
Chris@0
|
382 * Error creation factory.
|
Chris@0
|
383 *
|
Chris@0
|
384 * @param string $msg
|
Chris@0
|
385 * @param Stmt $stmt
|
Chris@0
|
386 *
|
Chris@0
|
387 * @return FatalErrorException
|
Chris@0
|
388 */
|
Chris@0
|
389 protected function createError($msg, $stmt)
|
Chris@0
|
390 {
|
Chris@0
|
391 return new FatalErrorException($msg, 0, E_ERROR, null, $stmt->getLine());
|
Chris@0
|
392 }
|
Chris@0
|
393 }
|