Mercurial > hg > cmmr2012-drupal-site
comparison vendor/psy/psysh/src/CodeCleaner/ValidConstructorPass.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 /* | |
4 * This file is part of Psy Shell. | |
5 * | |
6 * (c) 2012-2018 Justin Hileman | |
7 * | |
8 * For the full copyright and license information, please view the LICENSE | |
9 * file that was distributed with this source code. | |
10 */ | |
11 | |
12 namespace Psy\CodeCleaner; | |
13 | |
14 use PhpParser\Node; | |
15 use PhpParser\Node\Identifier; | |
16 use PhpParser\Node\Stmt\Class_; | |
17 use PhpParser\Node\Stmt\ClassMethod; | |
18 use PhpParser\Node\Stmt\Namespace_; | |
19 use Psy\Exception\FatalErrorException; | |
20 | |
21 /** | |
22 * Validate that the constructor method is not static, and does not have a | |
23 * return type. | |
24 * | |
25 * Checks both explicit __construct methods as well as old-style constructor | |
26 * methods with the same name as the class (for non-namespaced classes). | |
27 * | |
28 * As of PHP 5.3.3, methods with the same name as the last element of a | |
29 * namespaced class name will no longer be treated as constructor. This change | |
30 * doesn't affect non-namespaced classes. | |
31 * | |
32 * @author Martin HasoĊ <martin.hason@gmail.com> | |
33 */ | |
34 class ValidConstructorPass extends CodeCleanerPass | |
35 { | |
36 private $namespace; | |
37 | |
38 public function beforeTraverse(array $nodes) | |
39 { | |
40 $this->namespace = []; | |
41 } | |
42 | |
43 /** | |
44 * Validate that the constructor is not static and does not have a return type. | |
45 * | |
46 * @throws FatalErrorException the constructor function is static | |
47 * @throws FatalErrorException the constructor function has a return type | |
48 * | |
49 * @param Node $node | |
50 */ | |
51 public function enterNode(Node $node) | |
52 { | |
53 if ($node instanceof Namespace_) { | |
54 $this->namespace = isset($node->name) ? $node->name->parts : []; | |
55 } elseif ($node instanceof Class_) { | |
56 $constructor = null; | |
57 foreach ($node->stmts as $stmt) { | |
58 if ($stmt instanceof ClassMethod) { | |
59 // If we find a new-style constructor, no need to look for the old-style | |
60 if ('__construct' === strtolower($stmt->name)) { | |
61 $this->validateConstructor($stmt, $node); | |
62 | |
63 return; | |
64 } | |
65 | |
66 // We found a possible old-style constructor (unless there is also a __construct method) | |
67 if (empty($this->namespace) && strtolower($node->name) === strtolower($stmt->name)) { | |
68 $constructor = $stmt; | |
69 } | |
70 } | |
71 } | |
72 | |
73 if ($constructor) { | |
74 $this->validateConstructor($constructor, $node); | |
75 } | |
76 } | |
77 } | |
78 | |
79 /** | |
80 * @throws FatalErrorException the constructor function is static | |
81 * @throws FatalErrorException the constructor function has a return type | |
82 * | |
83 * @param Node $constructor | |
84 * @param Node $classNode | |
85 */ | |
86 private function validateConstructor(Node $constructor, Node $classNode) | |
87 { | |
88 if ($constructor->isStatic()) { | |
89 // For PHP Parser 4.x | |
90 $className = $classNode->name instanceof Identifier ? $classNode->name->toString() : $classNode->name; | |
91 | |
92 $msg = sprintf( | |
93 'Constructor %s::%s() cannot be static', | |
94 implode('\\', array_merge($this->namespace, (array) $className)), | |
95 $constructor->name | |
96 ); | |
97 throw new FatalErrorException($msg, 0, E_ERROR, null, $classNode->getLine()); | |
98 } | |
99 | |
100 if (method_exists($constructor, 'getReturnType') && $constructor->getReturnType()) { | |
101 // For PHP Parser 4.x | |
102 $className = $classNode->name instanceof Identifier ? $classNode->name->toString() : $classNode->name; | |
103 | |
104 $msg = sprintf( | |
105 'Constructor %s::%s() cannot declare a return type', | |
106 implode('\\', array_merge($this->namespace, (array) $className)), | |
107 $constructor->name | |
108 ); | |
109 throw new FatalErrorException($msg, 0, E_ERROR, null, $classNode->getLine()); | |
110 } | |
111 } | |
112 } |