annotate vendor/psy/psysh/src/Psy/CodeCleaner/StaticConstructorPass.php @ 7:848c88cfe644

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