Chris@0: Chris@0: */ Chris@0: class StaticConstructorPass extends CodeCleanerPass Chris@0: { Chris@0: private $isPHP533; Chris@0: private $namespace; Chris@0: Chris@0: public function __construct() Chris@0: { Chris@0: $this->isPHP533 = version_compare(PHP_VERSION, '5.3.3', '>='); Chris@0: } Chris@0: Chris@0: public function beforeTraverse(array $nodes) Chris@0: { Chris@0: $this->namespace = array(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validate that the old-style constructor function is not static. Chris@0: * Chris@0: * @throws FatalErrorException if the old-style constructor function is static Chris@0: * Chris@0: * @param Node $node Chris@0: */ Chris@0: public function enterNode(Node $node) Chris@0: { Chris@0: if ($node instanceof Namespace_) { Chris@0: $this->namespace = isset($node->name) ? $node->name->parts : array(); Chris@0: } elseif ($node instanceof Class_) { Chris@0: // Bail early if this is PHP 5.3.3 and we have a namespaced class Chris@0: if (!empty($this->namespace) && $this->isPHP533) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $constructor = null; Chris@0: foreach ($node->stmts as $stmt) { Chris@0: if ($stmt instanceof ClassMethod) { Chris@0: // Bail early if we find a new-style constructor Chris@0: if ('__construct' === strtolower($stmt->name)) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // We found a possible old-style constructor Chris@0: // (unless there is also a __construct method) Chris@0: if (strtolower($node->name) === strtolower($stmt->name)) { Chris@0: $constructor = $stmt; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if ($constructor && $constructor->isStatic()) { Chris@0: $msg = sprintf( Chris@0: 'Constructor %s::%s() cannot be static', Chris@0: implode('\\', array_merge($this->namespace, (array) $node->name)), Chris@0: $constructor->name Chris@0: ); Chris@0: throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine()); Chris@0: } Chris@0: } Chris@0: } Chris@0: }