/** * 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); } }
/** * @return string */ public function getDocComment() { if (!$this->node->hasAttribute('comments')) { return ''; } /* @var \PhpParser\Comment\Doc $comment */ $comment = $this->node->getAttribute('comments')[0]; return $comment->getReformattedText(); }