/** * Create from a Class Node * * @param ClassNode $node * @param LocatedSource $locatedSource * @param NamespaceNode|null $namespace optional - if omitted, we assume it is global namespaced class * @return ReflectionClass */ public static function createFromNode(ClassNode $node, LocatedSource $locatedSource, NamespaceNode $namespace = null) { $class = new self(); $class->locatedSource = $locatedSource; $class->name = $node->name; if (null !== $namespace) { $class->declaringNamespace = $namespace; } $methodNodes = $node->getMethods(); foreach ($methodNodes as $methodNode) { $class->methods[] = ReflectionMethod::createFromNode($methodNode, $class); } foreach ($node->stmts as $stmt) { if ($stmt instanceof ConstNode) { $constName = $stmt->consts[0]->name; $constValue = (new CompileNodeToValue())->__invoke($stmt->consts[0]->value); $class->constants[$constName] = $constValue; } if ($stmt instanceof PropertyNode) { $prop = ReflectionProperty::createFromNode($stmt, $class); $class->properties[$prop->getName()] = $prop; } } return $class; }
/** * @return ParsedMethod[] */ public function getMethods() { if (NULL === $this->methods) { $this->methods = array_map(function (ClassMethod $method) { return new ParsedMethod($this, $method); }, $this->node->getMethods()); } return $this->methods; }
/** * Finds or creates a class method (and eventually attaches it to the class itself) * * @param Class_ $class * @param string $name * * @return ClassMethod */ protected function findOrCreateMethod(Class_ $class, $name) { $foundMethods = array_filter($class->getMethods(), function (ClassMethod $method) use($name) { return $name === $method->name; }); $method = reset($foundMethods); if (!$method) { $class->stmts[] = $method = new ClassMethod($name); } return $method; }
public function testGetMethods() { $methods = array(new ClassMethod('foo'), new ClassMethod('bar')); $interface = new Class_('Foo', array('stmts' => array(new Node\Stmt\ClassConst(array(new Node\Const_('C1', new Node\Scalar\String_('C1')))), $methods[0], new Node\Stmt\ClassConst(array(new Node\Const_('C2', new Node\Scalar\String_('C2')))), $methods[1], new Node\Stmt\ClassConst(array(new Node\Const_('C3', new Node\Scalar\String_('C3'))))))); $this->assertSame($methods, $interface->getMethods()); }
private function detectAndHandlePhp4Ctor(Node\Stmt\Class_ $cls) { if ($this->mode & self::MODE_DEPRECATION && !$cls->isAnonymous()) { $name = isset($cls->namespacedName) ? $cls->namespacedName->toString() : $cls->name; $possibleCtorInfo = null; /** @var Node\Stmt\ClassMethod $method */ foreach ($cls->getMethods() as $method) { if (strcasecmp($method->name, '__construct') === 0) { return; // This will always be treated as ctor. Drop everything else } elseif (strcasecmp($method->name, ltrim($name, '\\')) === 0) { $possibleCtorInfo = [Reason::PHP4_CONSTRUCTOR, $method->getLine(), null, ['name' => $method->name]]; } } if ($possibleCtorInfo !== null) { call_user_func_array([$this->getResult(), 'addLimit'], $possibleCtorInfo); } } }