Chris@0: checkTraits = function_exists('trait_exists'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validate class, interface and trait definitions. Chris@0: * Chris@0: * Validate them upon entering the node, so that we know about their Chris@0: * presence and can validate constant fetches and static calls in class or Chris@0: * trait methods. Chris@0: * Chris@0: * @param Node Chris@0: */ Chris@0: public function enterNode(Node $node) Chris@0: { Chris@0: parent::enterNode($node); Chris@0: Chris@0: if (self::isConditional($node)) { Chris@0: $this->conditionalScopes++; Chris@0: } else { Chris@0: // @todo add an "else" here which adds a runtime check for instances where we can't tell Chris@0: // whether a class is being redefined by static analysis alone. Chris@0: if ($this->conditionalScopes === 0) { Chris@0: if ($node instanceof Class_) { Chris@0: $this->validateClassStatement($node); Chris@0: } elseif ($node instanceof Interface_) { Chris@0: $this->validateInterfaceStatement($node); Chris@0: } elseif ($node instanceof Trait_) { Chris@0: $this->validateTraitStatement($node); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validate `new` expressions, class constant fetches, and static calls. Chris@0: * Chris@0: * @throws FatalErrorException if a class, interface or trait is referenced which does not exist Chris@0: * @throws FatalErrorException if a class extends something that is not a class Chris@0: * @throws FatalErrorException if a class implements something that is not an interface Chris@0: * @throws FatalErrorException if an interface extends something that is not an interface Chris@0: * @throws FatalErrorException if a class, interface or trait redefines an existing class, interface or trait name Chris@0: * Chris@0: * @param Node $node Chris@0: */ Chris@0: public function leaveNode(Node $node) Chris@0: { Chris@0: if (self::isConditional($node)) { Chris@0: $this->conditionalScopes--; Chris@0: } elseif ($node instanceof New_) { Chris@0: $this->validateNewExpression($node); Chris@0: } elseif ($node instanceof ClassConstFetch) { Chris@0: $this->validateClassConstFetchExpression($node); Chris@0: } elseif ($node instanceof StaticCall) { Chris@0: $this->validateStaticCallExpression($node); Chris@0: } Chris@0: } Chris@0: Chris@0: private static function isConditional(Node $node) Chris@0: { Chris@0: return $node instanceof If_ || Chris@0: $node instanceof While_ || Chris@0: $node instanceof Do_ || Chris@0: $node instanceof Switch_; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validate a class definition statement. Chris@0: * Chris@0: * @param Class_ $stmt Chris@0: */ Chris@0: protected function validateClassStatement(Class_ $stmt) Chris@0: { Chris@0: $this->ensureCanDefine($stmt); Chris@0: if (isset($stmt->extends)) { Chris@0: $this->ensureClassExists($this->getFullyQualifiedName($stmt->extends), $stmt); Chris@0: } Chris@0: $this->ensureInterfacesExist($stmt->implements, $stmt); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validate an interface definition statement. Chris@0: * Chris@0: * @param Interface_ $stmt Chris@0: */ Chris@0: protected function validateInterfaceStatement(Interface_ $stmt) Chris@0: { Chris@0: $this->ensureCanDefine($stmt); Chris@0: $this->ensureInterfacesExist($stmt->extends, $stmt); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validate a trait definition statement. Chris@0: * Chris@0: * @param Trait_ $stmt Chris@0: */ Chris@0: protected function validateTraitStatement(Trait_ $stmt) Chris@0: { Chris@0: $this->ensureCanDefine($stmt); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validate a `new` expression. Chris@0: * Chris@0: * @param New_ $stmt Chris@0: */ Chris@0: protected function validateNewExpression(New_ $stmt) Chris@0: { Chris@0: // if class name is an expression or an anonymous class, give it a pass for now Chris@0: if (!$stmt->class instanceof Expr && !$stmt->class instanceof Class_) { Chris@0: $this->ensureClassExists($this->getFullyQualifiedName($stmt->class), $stmt); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validate a class constant fetch expression's class. Chris@0: * Chris@0: * @param ClassConstFetch $stmt Chris@0: */ Chris@0: protected function validateClassConstFetchExpression(ClassConstFetch $stmt) Chris@0: { Chris@0: // there is no need to check exists for ::class const for php 5.5 or newer Chris@0: if (strtolower($stmt->name) === 'class' Chris@0: && version_compare(PHP_VERSION, '5.5', '>=')) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // if class name is an expression, give it a pass for now Chris@0: if (!$stmt->class instanceof Expr) { Chris@0: $this->ensureClassOrInterfaceExists($this->getFullyQualifiedName($stmt->class), $stmt); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validate a class constant fetch expression's class. Chris@0: * Chris@0: * @param StaticCall $stmt Chris@0: */ Chris@0: protected function validateStaticCallExpression(StaticCall $stmt) Chris@0: { Chris@0: // if class name is an expression, give it a pass for now Chris@0: if (!$stmt->class instanceof Expr) { Chris@0: $this->ensureMethodExists($this->getFullyQualifiedName($stmt->class), $stmt->name, $stmt); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensure that no class, interface or trait name collides with a new definition. Chris@0: * Chris@0: * @throws FatalErrorException Chris@0: * Chris@0: * @param Stmt $stmt Chris@0: */ Chris@0: protected function ensureCanDefine(Stmt $stmt) Chris@0: { Chris@0: $name = $this->getFullyQualifiedName($stmt->name); Chris@0: Chris@0: // check for name collisions Chris@0: $errorType = null; Chris@0: if ($this->classExists($name)) { Chris@0: $errorType = self::CLASS_TYPE; Chris@0: } elseif ($this->interfaceExists($name)) { Chris@0: $errorType = self::INTERFACE_TYPE; Chris@0: } elseif ($this->traitExists($name)) { Chris@0: $errorType = self::TRAIT_TYPE; Chris@0: } Chris@0: Chris@0: if ($errorType !== null) { Chris@0: throw $this->createError(sprintf('%s named %s already exists', ucfirst($errorType), $name), $stmt); Chris@0: } Chris@0: Chris@0: // Store creation for the rest of this code snippet so we can find local Chris@0: // issue too Chris@0: $this->currentScope[strtolower($name)] = $this->getScopeType($stmt); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensure that a referenced class exists. Chris@0: * Chris@0: * @throws FatalErrorException Chris@0: * Chris@0: * @param string $name Chris@0: * @param Stmt $stmt Chris@0: */ Chris@0: protected function ensureClassExists($name, $stmt) Chris@0: { Chris@0: if (!$this->classExists($name)) { Chris@0: throw $this->createError(sprintf('Class \'%s\' not found', $name), $stmt); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensure that a referenced class _or interface_ exists. Chris@0: * Chris@0: * @throws FatalErrorException Chris@0: * Chris@0: * @param string $name Chris@0: * @param Stmt $stmt Chris@0: */ Chris@0: protected function ensureClassOrInterfaceExists($name, $stmt) Chris@0: { Chris@0: if (!$this->classExists($name) && !$this->interfaceExists($name)) { Chris@0: throw $this->createError(sprintf('Class \'%s\' not found', $name), $stmt); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensure that a statically called method exists. Chris@0: * Chris@0: * @throws FatalErrorException Chris@0: * Chris@0: * @param string $class Chris@0: * @param string $name Chris@0: * @param Stmt $stmt Chris@0: */ Chris@0: protected function ensureMethodExists($class, $name, $stmt) Chris@0: { Chris@0: $this->ensureClassExists($class, $stmt); Chris@0: Chris@0: // let's pretend all calls to self, parent and static are valid Chris@0: if (in_array(strtolower($class), array('self', 'parent', 'static'))) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // ... and all calls to classes defined right now Chris@0: if ($this->findInScope($class) === self::CLASS_TYPE) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // if method name is an expression, give it a pass for now Chris@0: if ($name instanceof Expr) { Chris@0: return; Chris@0: } Chris@0: Chris@0: if (!method_exists($class, $name) && !method_exists($class, '__callStatic')) { Chris@0: throw $this->createError(sprintf('Call to undefined method %s::%s()', $class, $name), $stmt); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensure that a referenced interface exists. Chris@0: * Chris@0: * @throws FatalErrorException Chris@0: * Chris@0: * @param $interfaces Chris@0: * @param Stmt $stmt Chris@0: */ Chris@0: protected function ensureInterfacesExist($interfaces, $stmt) Chris@0: { Chris@0: foreach ($interfaces as $interface) { Chris@0: /** @var string $name */ Chris@0: $name = $this->getFullyQualifiedName($interface); Chris@0: if (!$this->interfaceExists($name)) { Chris@0: throw $this->createError(sprintf('Interface \'%s\' not found', $name), $stmt); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get a symbol type key for storing in the scope name cache. Chris@0: * Chris@0: * @param Stmt $stmt Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: protected function getScopeType(Stmt $stmt) Chris@0: { Chris@0: if ($stmt instanceof Class_) { Chris@0: return self::CLASS_TYPE; Chris@0: } elseif ($stmt instanceof Interface_) { Chris@0: return self::INTERFACE_TYPE; Chris@0: } elseif ($stmt instanceof Trait_) { Chris@0: return self::TRAIT_TYPE; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check whether a class exists, or has been defined in the current code snippet. Chris@0: * Chris@0: * Gives `self`, `static` and `parent` a free pass. Chris@0: * Chris@0: * @param string $name Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: protected function classExists($name) Chris@0: { Chris@0: // Give `self`, `static` and `parent` a pass. This will actually let Chris@0: // some errors through, since we're not checking whether the keyword is Chris@0: // being used in a class scope. Chris@0: if (in_array(strtolower($name), array('self', 'static', 'parent'))) { Chris@0: return true; Chris@0: } Chris@0: Chris@0: return class_exists($name) || $this->findInScope($name) === self::CLASS_TYPE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check whether an interface exists, or has been defined in the current code snippet. Chris@0: * Chris@0: * @param string $name Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: protected function interfaceExists($name) Chris@0: { Chris@0: return interface_exists($name) || $this->findInScope($name) === self::INTERFACE_TYPE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check whether a trait exists, or has been defined in the current code snippet. Chris@0: * Chris@0: * @param string $name Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: protected function traitExists($name) Chris@0: { Chris@0: return $this->checkTraits && (trait_exists($name) || $this->findInScope($name) === self::TRAIT_TYPE); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Find a symbol in the current code snippet scope. Chris@0: * Chris@0: * @param string $name Chris@0: * Chris@0: * @return string|null Chris@0: */ Chris@0: protected function findInScope($name) Chris@0: { Chris@0: $name = strtolower($name); Chris@0: if (isset($this->currentScope[$name])) { Chris@0: return $this->currentScope[$name]; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Error creation factory. Chris@0: * Chris@0: * @param string $msg Chris@0: * @param Stmt $stmt Chris@0: * Chris@0: * @return FatalErrorException Chris@0: */ Chris@0: protected function createError($msg, $stmt) Chris@0: { Chris@0: return new FatalErrorException($msg, 0, E_ERROR, null, $stmt->getLine()); Chris@0: } Chris@0: }