Mercurial > hg > isophonics-drupal-site
comparison vendor/psy/psysh/src/Psy/CodeCleaner/StaticConstructorPass.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 /* | |
4 * This file is part of Psy Shell. | |
5 * | |
6 * (c) 2012-2017 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\Stmt\Class_; | |
16 use PhpParser\Node\Stmt\ClassMethod; | |
17 use PhpParser\Node\Stmt\Namespace_; | |
18 use Psy\Exception\FatalErrorException; | |
19 | |
20 /** | |
21 * Validate that the old-style constructor function is not static. | |
22 * | |
23 * As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name | |
24 * will no longer be treated as constructor. This change doesn't affect non-namespaced classes. | |
25 * | |
26 * Validation of the __construct method ensures the PHP Parser. | |
27 * | |
28 * @author Martin HasoĊ <martin.hason@gmail.com> | |
29 */ | |
30 class StaticConstructorPass extends CodeCleanerPass | |
31 { | |
32 private $isPHP533; | |
33 private $namespace; | |
34 | |
35 public function __construct() | |
36 { | |
37 $this->isPHP533 = version_compare(PHP_VERSION, '5.3.3', '>='); | |
38 } | |
39 | |
40 public function beforeTraverse(array $nodes) | |
41 { | |
42 $this->namespace = array(); | |
43 } | |
44 | |
45 /** | |
46 * Validate that the old-style constructor function is not static. | |
47 * | |
48 * @throws FatalErrorException if the old-style constructor function is static | |
49 * | |
50 * @param Node $node | |
51 */ | |
52 public function enterNode(Node $node) | |
53 { | |
54 if ($node instanceof Namespace_) { | |
55 $this->namespace = isset($node->name) ? $node->name->parts : array(); | |
56 } elseif ($node instanceof Class_) { | |
57 // Bail early if this is PHP 5.3.3 and we have a namespaced class | |
58 if (!empty($this->namespace) && $this->isPHP533) { | |
59 return; | |
60 } | |
61 | |
62 $constructor = null; | |
63 foreach ($node->stmts as $stmt) { | |
64 if ($stmt instanceof ClassMethod) { | |
65 // Bail early if we find a new-style constructor | |
66 if ('__construct' === strtolower($stmt->name)) { | |
67 return; | |
68 } | |
69 | |
70 // We found a possible old-style constructor | |
71 // (unless there is also a __construct method) | |
72 if (strtolower($node->name) === strtolower($stmt->name)) { | |
73 $constructor = $stmt; | |
74 } | |
75 } | |
76 } | |
77 | |
78 if ($constructor && $constructor->isStatic()) { | |
79 $msg = sprintf( | |
80 'Constructor %s::%s() cannot be static', | |
81 implode('\\', array_merge($this->namespace, (array) $node->name)), | |
82 $constructor->name | |
83 ); | |
84 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine()); | |
85 } | |
86 } | |
87 } | |
88 } |