Chris@0: inClass = false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @throws ErrorException if get_class or get_called_class is called without an object from outside a class Chris@0: * Chris@0: * @param Node $node Chris@0: */ Chris@0: public function enterNode(Node $node) Chris@0: { Chris@0: if ($node instanceof Class_ || $node instanceof Trait_) { Chris@0: $this->inClass = true; Chris@0: } elseif ($node instanceof FuncCall && !$this->inClass) { Chris@0: // We'll give any args at all (besides null) a pass. Chris@0: // Technically we should be checking whether the args are objects, but this will do for now. Chris@0: // Chris@0: // @todo switch this to actually validate args when we get context-aware code cleaner passes. Chris@0: if (!empty($node->args) && !$this->isNull($node->args[0])) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // We'll ignore name expressions as well (things like `$foo()`) Chris@0: if (!($node->name instanceof Name)) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $name = strtolower($node->name); Chris@0: if (in_array($name, array('get_class', 'get_called_class'))) { Chris@0: $msg = sprintf('%s() called without object from outside a class', $name); Chris@0: throw new ErrorException($msg, 0, E_USER_WARNING, null, $node->getLine()); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param Node $node Chris@0: */ Chris@0: public function leaveNode(Node $node) Chris@0: { Chris@0: if ($node instanceof Class_) { Chris@0: $this->inClass = false; Chris@0: } Chris@0: } Chris@0: Chris@0: private function isNull(Node $node) Chris@0: { Chris@0: return $node->value instanceof ConstFetch && strtolower($node->value->name) === 'null'; Chris@0: } Chris@0: }