Skip to content
This repository has been archived by the owner on Feb 16, 2019. It is now read-only.

Commit

Permalink
feature #345 Add support for PHP7 return types (markdunphy)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.0-dev branch.

Discussion
----------

Add support for PHP7 return types

Hi @fabpot -

Thanks for all your work on Sami! This feature was discussed in https://github.com/FriendsOfPHP/Sami/issues/256 and you suggested it could be implemented. Please let me know your thoughts on this PR.

Commits
-------

95eeb68 add support for php7 return types
  • Loading branch information
fabpot committed Jun 25, 2018
2 parents c660012 + 95eeb68 commit cc1bfea
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Sami/Parser/NodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,31 @@ protected function addMethod(ClassMethodNode $node)

$method->setErrors($errors);

$returnType = $node->getReturnType();
$returnTypeStr = null;

if (is_string($returnType)) {
$returnTypeStr = (string) $returnType;
} elseif ($returnType instanceof NullableType) {
$returnTypeStr = (string) $returnType->type;
} elseif (null !== $returnType) {
$returnTypeStr = (string) $returnType;
}

if ($returnType instanceof FullyQualified && 0 !== strpos($returnTypeStr, '\\')) {
$returnTypeStr = '\\'.$returnTypeStr;
}

if (null !== $returnTypeStr) {
$returnTypeArr = array(array($returnTypeStr, false));

if ($returnType instanceof NullableType) {
$returnTypeArr[] = array('null', false);
}

$method->setHint($this->resolveHint($returnTypeArr));
}

if ($this->context->getFilter()->acceptMethod($method)) {
$this->context->getClass()->addMethod($method);

Expand Down
100 changes: 100 additions & 0 deletions Sami/Tests/Parser/NodeVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Name\Relative;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeTraverser;
Expand Down Expand Up @@ -50,6 +51,23 @@ public function testMethodTypehints(ClassReflection $classReflection, ClassMetho
}
}

/**
* @dataProvider getMethodReturnTypeHints
*/
public function testMethodReturnTypeHints(ClassReflection $classReflection, ClassMethod $method, $expectedReturnType)
{
$parserContext = new ParserContext(new TrueFilter(), new DocBlockParser(), new Standard());
$parserContext->enterClass($classReflection);
$traverser = new NodeTraverser();
$traverser->addVisitor(new NodeVisitor($parserContext));
$traverser->traverse(array($method));

/* @var $method MethodReflection */
$reflMethod = $classReflection->getMethod($method->name);

$this->assertEquals($expectedReturnType, $reflMethod->getHintAsString());
}

/**
* @return array
*/
Expand All @@ -64,6 +82,88 @@ public function getMethodTypehints()
);
}

/**
* @return array
*/
public function getMethodReturnTypeHints()
{
return array(
'primitive' => $this->getPrimitiveMethodReturnType(),
'class' => $this->getClassMethodReturnType(),
'nullableType' => $this->getNullableMethodReturnType(),
);
}

private function getPrimitiveMethodReturnType()
{
$expectedReturnType = 'string';
$classReflection = new ClassReflection('C1', 1);
$method = new ClassMethod('testMethod', array(
'returnType' => 'string'
));

$classReflection->setMethods(array($method));

$store = new ArrayStore();
$store->setClasses(array($classReflection));

$project = new Project($store);
$project->loadClass('C1');

return array(
$classReflection,
$method,
$expectedReturnType,
);
}

private function getClassMethodReturnType()
{
$expectedReturnType = 'Class';
$classReflection = new ClassReflection('C1', 1);
$method = new ClassMethod('testMethod', array(
'returnType' => new FullyQualified('Test\\Class'),
));

$classReflection->setMethods(array($method));

$store = new ArrayStore();
$store->setClasses(array($classReflection));

$project = new Project($store);
$project->loadClass('C1');

return array(
$classReflection,
$method,
$expectedReturnType
);
}

private function getNullableMethodReturnType()
{
$expectedReturnType = 'Class|null';
$classReflection = new ClassReflection('C1', 1);
$method = new ClassMethod('testMethod', array(
'returnType' => new NullableType('Test\\Class'),
));

$classReflection->setMethods(array($method));

$store = new ArrayStore();
$store->setClasses(array($classReflection));

$project = new Project($store);
$project->loadClass('C1');

return array(
$classReflection,
$method,
$expectedReturnType
);
}


private function getMethodTypehintsPrimiteveParameters()
{
$classReflection = new ClassReflection('C1', 1);
Expand Down

0 comments on commit cc1bfea

Please sign in to comment.