/** * Get the return type declaration (only for PHP 7+ code) * * @return ReflectionType|null */ public function getReturnType() { $namespaceForType = $this instanceof ReflectionMethod ? $this->getDeclaringClass()->getNamespaceName() : $this->getNamespaceName(); $typeHint = (new FindTypeFromAst())->__invoke($this->node->getReturnType(), $this->getLocatedSource(), $namespaceForType); if (null === $typeHint) { return null; } return ReflectionType::createFromType($typeHint, false); }
public function testItReturnTrueWhenReturnTypeRemoved() { $test = new ReturnTypeRemoved(); $old = new ClassMethod('foo'); $old->returnType = 'void if seal is broken'; $class = new Class_('the_class'); $class->namespacedName = $class->name; $old->setAttribute('parent', $class); $new = new ClassMethod('foo'); $this->assertTrue('' == (string) $new->getReturnType()); $this->assertTrue($test->handle($old, $new)); }
/** * @param Node\Stmt\ClassMethod $node */ protected function parseClassMethodNode(Node\Stmt\ClassMethod $node) { $parameters = []; /** @var \PhpParser\Node\Param $param */ foreach ($node->params as $param) { $parameters[] = ['name' => $param->name, 'type' => (string) $param->type, 'isReference' => $param->byRef, 'isVariadic' => $param->variadic, 'isOptional' => $param->default ? true : false]; } $this->structuralElements[$this->currentStructuralElement->namespacedName->toString()]['methods'][] = ['name' => $node->name, 'startLine' => $node->getLine(), 'isPublic' => $node->isPublic(), 'isPrivate' => $node->isPrivate(), 'isProtected' => $node->isProtected(), 'isStatic' => $node->isStatic(), 'returnType' => $node->getReturnType(), 'parameters' => $parameters, 'docComment' => $node->getDocComment() ? $node->getDocComment()->getText() : null]; }