private function isCallable(Reflection\ReflectionMethod $method) { if (!$method->isPublic()) { return false; } if ($method->getNumberOfRequiredParameters() < 1) { return false; } $firstParameter = $method->getParameters()[0]; if ((string) $firstParameter->getTypeHint() !== '\\' . Call::class) { return false; } return true; }
/** * 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; }
/** * Get only the methods that this class implements (i.e. do not search * up parent classes etc.) * * @return ReflectionMethod[] */ public function getImmediateMethods() { /* @var $methods \ReflectionMethod[] */ $methods = array_map(function (ClassMethod $methodNode) { return ReflectionMethod::createFromNode($this->reflector, $methodNode, $this); }, $this->node->getMethods()); $methodsByName = []; foreach ($methods as $method) { $methodsByName[$method->getName()] = $method; } return $methodsByName; }
public function testCreateFromInstance() { $method = ReflectionMethod::createFromInstance(new \SplDoublyLinkedList(), 'add'); $this->assertInstanceOf(ReflectionMethod::class, $method); $this->assertSame('add', $method->getName()); }
/** * Create from a Class Node. * * @param Reflector $reflector * @param ClassLikeNode $node * @param LocatedSource $locatedSource * @param NamespaceNode|null $namespace optional - if omitted, we assume it is global namespaced class * * @return ReflectionClass */ public static function createFromNode(Reflector $reflector, ClassLikeNode $node, LocatedSource $locatedSource, NamespaceNode $namespace = null) { $class = new self(); $class->reflector = $reflector; $class->node = $node; $class->locatedSource = $locatedSource; $class->name = $node->name; if (null !== $namespace) { $class->declaringNamespace = $namespace; } if ($node instanceof ClassNode && null !== $node->extends) { $objectType = (new FindTypeFromAst())->__invoke($node->extends, $locatedSource, $class->getNamespaceName()); if (null !== $objectType && $objectType instanceof Object_) { $class->extendsClassType = $objectType->getFqsen(); } } $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; }
private function assertSameMethodAttributes(\ReflectionMethod $original, ReflectionMethod $stubbed) { $this->assertSame(array_map(function (\ReflectionParameter $parameter) { return $parameter->getDeclaringFunction()->getName() . '.' . $parameter->getName(); }, $original->getParameters()), array_map(function (ReflectionParameter $parameter) { return $parameter->getDeclaringFunction()->getName() . '.' . $parameter->getName(); }, $stubbed->getParameters())); foreach ($original->getParameters() as $parameter) { $this->assertSameParameterAttributes($parameter, $stubbed->getParameter($parameter->getName())); } $this->assertSame($original->isPublic(), $stubbed->isPublic()); $this->assertSame($original->isPrivate(), $stubbed->isPrivate()); $this->assertSame($original->isProtected(), $stubbed->isProtected()); $this->assertSame($original->returnsReference(), $stubbed->returnsReference()); $this->assertSame($original->isStatic(), $stubbed->isStatic()); $this->assertSame($original->isFinal(), $stubbed->isFinal()); }
/** * {@inheritDoc} */ public function getPrototype() { return new ReflectionMethod($this->betterReflectionMethod->getPrototype()); }
public function formatMethod(ReflectionMethod $method) { // $params = []; // foreach ($method->getParameters() as $parameter) { // $params[] = '$' . $parameter->getName(); // } $ml = Multiline::create($method->getLocatedSource()->getSource()); return trim($ml[$method->getStartLine() - 1]); // return sprintf('%s(%s)', $method->getName(), implo/de(', ', $params)); }
/** * Construct a flat list of methods that are available. This will search up * all parent classes/traits/interfaces/current scope for methods. * * @return ReflectionMethod[] */ private function scanMethods() { // merging together methods from interfaces, parent class, traits, current class (in this precise order) /* @var $inheritedMethods \ReflectionMethod[] */ $inheritedMethods = array_merge(array_merge([], ...array_map(function (ReflectionClass $ancestor) { return $ancestor->getMethods(); }, array_values(array_merge($this->getInterfaces(), array_filter([$this->getParentClass()]), $this->getTraits())))), array_map(function (ClassMethod $methodNode) { return ReflectionMethod::createFromNode($this->reflector, $methodNode, $this); }, $this->node->getMethods())); $methodsByName = []; foreach ($inheritedMethods as $inheritedMethod) { $methodsByName[$inheritedMethod->getName()] = $inheritedMethod; } return $methodsByName; }
/** * * * @param ReflectionMethod $reflection * @return Method */ public static function fromReflection(ReflectionMethod $reflection) { // gestion du type // $type = implode('|', $reflection->getDocBlockTypeStrings()); // // construction $method = new static($reflection->getName(), [], $reflection->getBodyCode()); // docblock $docblock = new \phpDocumentor\Reflection\DocBlock($reflection->getDocComment()); $method->setSummary($docblock->getShortDescription()); $method->setDescription($docblock->getLongDescription()); // gestion des modifiers $reflection->isPrivate() ? $method->enablePrivate() : $method->disablePrivate(); $reflection->isProtected() ? $method->enableProtected() : $method->disabledProtected(); $reflection->isPublic() ? $method->enablePublic() : $method->disablePublic(); $reflection->isStatic() ? $method->enableStatic() : $method->disableStatic(); $reflection->isFinal() ? $method->enableFinal() : $method->disableFinal(); foreach ($reflection->getParameters() as $parameter) { $method->addParameter(Parameter::fromReflection($parameter)); } return $method; }
/** * Call a placeholder method with the Call object and parameters extracted from the raw template call. */ public function call(Call $call) { $callable = [$this->placeholder, $this->method->getShortName()]; $params = array_merge([$call], $call->getParameters()); call_user_func_array($callable, $params); }
public function countParameters() : int { return $this->reflection->getNumberOfParameters(); }
function it_can_count_its_parameters(ReflectionMethod $reflectionMethod) { $reflectionMethod->getNumberOfParameters()->willReturn(3); $this->countParameters()->shouldBe(3); }
function it_might_be_invokable(ReflectionClass $reflectionClass, ReflectionMethod $method, AnnotationScanner $annotations, TypeFactory $typeFactory, Type $type, Annotations $methodAnnotations) { $reflectionClass->getName()->willReturn('MyClass'); $method->getName()->willReturn('__invoke'); $method->getReturnType()->willReturn(null); $method->getDocBlockReturnTypes()->willReturn([]); $method->getParameters()->willReturn([]); $method->getDocComment()->willReturn(''); $reflectionClass->getMethods()->willReturn([$method]); $annotations->scanForAnnotations(Argument::any(), Argument::any(), $this->imports)->willReturn($methodAnnotations); $typeFactory->create(Argument::any(), Argument::any(), Argument::any(), Argument::any())->willReturn($type); $this->isInvokable()->shouldBe(true); $reflectionClass->getMethods()->willReturn([]); $this->isInvokable()->shouldBe(false); }