/** * @return string */ public function getDescription() { $docBlock = new DocBlock($this->reflectionParameter->getDeclaringFunction()->getDocComment()); /** @var \phpDocumentor\Reflection\DocBlock\Tag\ParamTag[] $paramTags */ $paramTags = $docBlock->getTagsByName('param'); foreach ($paramTags as $paramTag) { if ($paramTag->getVariableName() === '$' . $this->reflectionParameter->getName()) { $lines = Multiline::create($paramTag->getDescription()); $lines->apply('ltrim'); return (string) $lines; } } return ''; }
/** * Populate the common elements of the function abstract * * @param MethodOrFunctionNode $node * @param LocatedSource $locatedSource * @param NamespaceNode|null $declaringNamespace */ protected function populateFunctionAbstract(MethodOrFunctionNode $node, LocatedSource $locatedSource, NamespaceNode $declaringNamespace = null) { $this->node = $node; $this->name = $node->name; $this->locatedSource = $locatedSource; $this->declaringNamespace = $declaringNamespace; if ($node->hasAttribute('comments')) { /* @var \PhpParser\Comment\Doc $comment */ $comment = $node->getAttribute('comments')[0]; $this->docBlock = $comment->getReformattedText(); } // We must determine if params are optional or not ahead of time, but // we must do it in reverse... $overallOptionalFlag = true; $lastParamIndex = count($node->params) - 1; for ($i = $lastParamIndex; $i >= 0; $i--) { $hasDefault = $node->params[$i]->default !== null; // When we find the first parameter that does not have a default, // flip the flag as all params for this are no longer optional // EVEN if they have a default value if (!$hasDefault) { $overallOptionalFlag = false; } $node->params[$i]->isOptional = $overallOptionalFlag; } foreach ($node->params as $paramIndex => $paramNode) { $this->parameters[] = ReflectionParameter::createFromNode($paramNode, $this, $paramIndex); } }
/** * Factory depuis une reflection * * @param ReflectionParameter $reflection * @return static */ public static function fromReflection(ReflectionParameter $reflection) { // VALUE $value = $reflection->isDefaultValueAvailable() ? $reflection->getDefaultValue() : Maker::NO_VALUE; // TYPE $type = $reflection->getTypeHint(); // analyse du type if (!is_null($type)) { if ($type instanceof Object_) { $type = strval($type->getFqsen()); } elseif ($type instanceof Array_) { $type = 'array'; } else { exc('Impossible de determiner le type du paramètre : ' . $reflection->getName()); } } // construction $parameter = new static($reflection->getName(), $value, $type); return $parameter; }
/** * Get an array list of the parameters for this method signature, as an * array of ReflectionParameter instances. * * @return ReflectionParameter[] */ public function getParameters() { $parameters = []; foreach ($this->node->params as $paramIndex => $paramNode) { $parameters[] = ReflectionParameter::createFromNode($paramNode, $this, $paramIndex); } return $parameters; }
public function testExportThrowsException() { $this->setExpectedException(\Exception::class); ReflectionParameter::export(); }
private function assertSameParameterAttributes(\ReflectionParameter $original, ReflectionParameter $stubbed) { $this->assertSame($original->getName(), $stubbed->getName()); $this->assertSame($original->isArray(), $stubbed->isArray()); $this->assertSame($original->isCallable(), $stubbed->isCallable()); //$this->assertSame($original->allowsNull(), $stubbed->allowsNull()); @TODO WTF? $this->assertSame($original->canBePassedByValue(), $stubbed->canBePassedByValue()); $this->assertSame($original->isOptional(), $stubbed->isOptional()); $this->assertSame($original->isPassedByReference(), $stubbed->isPassedByReference()); $this->assertSame($original->isVariadic(), $stubbed->isVariadic()); if ($class = $original->getClass()) { $stubbedClass = $stubbed->getClass(); $this->assertInstanceOf(ReflectionClass::class, $stubbedClass); $this->assertSame($class->getName(), $stubbedClass->getName()); } else { $this->assertNull($stubbed->getClass()); } }
public function name() : string { return $this->reflection->getName(); }
public function formatParameter(ReflectionMethod $method, ReflectionParameter $parameter) { $code = '$' . $parameter->getName(); $text = ''; if ($parameter->isDefaultValueAvailable()) { if ($parameter->isDefaultValueConstant()) { $code .= ' = ' . $parameter->getDefaultValueConstantName(); } $code .= ' = ' . $parameter->getDefaultValueAsString(); } if ($parameter->isPassedByReference()) { $code = '&' . $code; } $doc = $this->getMethodDocBlock($method); $paramTags = $doc->getTagsByName('param'); $type = 'mixed'; /** @var \phpDocumentor\Reflection\DocBlock\Tag\ParamTag $paramTag */ foreach ($paramTags as $paramTag) { if ($paramTag->getVariableName() === '$' . $parameter->getName() && $paramTag->getDescription() !== '') { $text .= ' — ' . $paramTag->getDescription(); $type = $paramTag->getType(); } } return sprintf('%d. `%s` — `%s` %s', $parameter->getPosition() + 1, $code, $type, $text); }
function it_has_a_name(ReflectionParameter $parameter) { $parameter->getName()->willReturn('id'); $this->name()->shouldBe('id'); }
/** * {@inheritDoc} */ public function getDefaultValueConstantName() { return $this->betterReflectionParameter->getDefaultValueConstantName(); }