-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discover return types for functions and methods #104
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace BetterReflection\TypesFinder; | ||
|
||
use BetterReflection\Reflection\ReflectionMethod; | ||
use phpDocumentor\Reflection\Types\Context; | ||
use phpDocumentor\Reflection\Types\ContextFactory; | ||
use phpDocumentor\Reflection\DocBlock; | ||
use phpDocumentor\Reflection\Type; | ||
use BetterReflection\Reflection\ReflectionFunctionAbstract; | ||
|
||
class FindReturnType | ||
{ | ||
/** | ||
* Given a function, attempt to find the return type. | ||
* | ||
* @param ReflectionFunctionAbstract $function | ||
* @return Type[] | ||
*/ | ||
public function __invoke(ReflectionFunctionAbstract $function) | ||
{ | ||
$context = $this->createContextForFunction($function); | ||
|
||
$returnTags = (new DocBlock( | ||
$function->getDocComment(), | ||
new DocBlock\Context( | ||
$context->getNamespace(), | ||
$context->getNamespaceAliases() | ||
) | ||
))->getTagsByName('return'); | ||
|
||
foreach ($returnTags as $returnTag) { | ||
/* @var $returnTag \phpDocumentor\Reflection\DocBlock\Tag\ReturnTag */ | ||
return (new ResolveTypes())->__invoke($returnTag->getTypes(), $context); | ||
} | ||
return []; | ||
} | ||
|
||
/** | ||
* @param ReflectionFunctionAbstract $function | ||
* @return Context | ||
*/ | ||
private function createContextForFunction(ReflectionFunctionAbstract $function) | ||
{ | ||
if ($function instanceof ReflectionMethod) { | ||
$function = $function->getDeclaringClass(); | ||
} | ||
|
||
return (new ContextFactory())->createForNamespace( | ||
$function->getNamespaceName(), | ||
$function->getLocatedSource()->getSource() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
use BetterReflection\SourceLocator\LocatedSource; | ||
use BetterReflection\SourceLocator\SingleFileSourceLocator; | ||
use BetterReflection\SourceLocator\StringSourceLocator; | ||
use phpDocumentor\Reflection\Types\Boolean; | ||
use PhpParser\Node\Stmt\Function_; | ||
|
||
/** | ||
|
@@ -276,4 +277,22 @@ public function testGetLocatedSource() | |
|
||
$this->assertSame($locatedSource, $functionInfo->getLocatedSource()); | ||
} | ||
|
||
public function testGetDocblockReturnTypes() | ||
{ | ||
$php = '<?php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
/** | ||
* @return bool | ||
*/ | ||
function foo() {}'; | ||
|
||
$reflector = new FunctionReflector(new StringSourceLocator($php)); | ||
$function = $reflector->reflect('foo'); | ||
|
||
$types = $function->getDocblockReturnTypes(); | ||
|
||
$this->assertInternalType('array', $types); | ||
$this->assertCount(1, $types); | ||
$this->assertInstanceOf(Boolean::class, $types[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
use BetterReflection\Reflection\ReflectionFunction; | ||
use BetterReflection\Reflector\FunctionReflector; | ||
use BetterReflection\SourceLocator\StringSourceLocator; | ||
use phpDocumentor\Reflection\Types\Boolean; | ||
|
||
/** | ||
* @covers \BetterReflection\Reflection\ReflectionFunction | ||
|
@@ -107,4 +108,22 @@ public function testStringCast($functionName, $expectedStringValue) | |
|
||
$this->assertStringMatchesFormat($expectedStringValue, (string)$functionInfo); | ||
} | ||
|
||
public function testGetDocblockReturnTypes() | ||
{ | ||
$php = '<?php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
/** | ||
* @return bool | ||
*/ | ||
function foo() {}'; | ||
|
||
$reflector = new FunctionReflector(new StringSourceLocator($php)); | ||
$function = $reflector->reflect('foo'); | ||
|
||
$types = $function->getDocblockReturnTypes(); | ||
|
||
$this->assertInternalType('array', $types); | ||
$this->assertCount(1, $types); | ||
$this->assertInstanceOf(Boolean::class, $types[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace BetterReflectionTest\TypesFinder; | ||
|
||
use BetterReflection\Reflection\ReflectionClass; | ||
use BetterReflection\Reflection\ReflectionFunction; | ||
use BetterReflection\Reflection\ReflectionMethod; | ||
use BetterReflection\SourceLocator\LocatedSource; | ||
use BetterReflection\TypesFinder\FindReturnType; | ||
use phpDocumentor\Reflection\Types; | ||
|
||
/** | ||
* @covers \BetterReflection\TypesFinder\FindReturnType | ||
*/ | ||
class FindReturnTypeTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @return array | ||
*/ | ||
public function returnTypeProvider() | ||
{ | ||
return [ | ||
['@return int|string', [Types\Integer::class, Types\String_::class]], | ||
['@return array', [Types\Array_::class]], | ||
['@return \stdClass', [Types\Object_::class]], | ||
['@return int|int[]|int[][]', [Types\Integer::class, Types\Array_::class, Types\Array_::class]], | ||
['@return int A comment about the return type', [Types\Integer::class]], | ||
['', []], | ||
]; | ||
} | ||
|
||
/** | ||
* @param string $docBlock | ||
* @param string[] $expectedInstances | ||
* @dataProvider returnTypeProvider | ||
*/ | ||
public function testFindReturnTypeForFunction($docBlock, $expectedInstances) | ||
{ | ||
$docBlock = "/**\n * $docBlock\n */"; | ||
|
||
$function = $this->getMockBuilder(ReflectionFunction::class) | ||
->setMethods(['getDocComment', 'getLocatedSource']) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$function | ||
->expects($this->once()) | ||
->method('getDocComment') | ||
->will($this->returnValue($docBlock)); | ||
|
||
$function | ||
->expects($this->once()) | ||
->method('getLocatedSource') | ||
->will($this->returnValue(new LocatedSource('<?php', null))); | ||
|
||
/* @var ReflectionFunction $function */ | ||
$foundTypes = (new FindReturnType())->__invoke($function); | ||
|
||
$this->assertCount(count($expectedInstances), $foundTypes); | ||
|
||
foreach ($expectedInstances as $i => $expectedInstance) { | ||
$this->assertInstanceOf($expectedInstance, $foundTypes[$i]); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $docBlock | ||
* @param string[] $expectedInstances | ||
* @dataProvider returnTypeProvider | ||
*/ | ||
public function testFindReturnTypeForMethod($docBlock, $expectedInstances) | ||
{ | ||
$docBlock = "/**\n * $docBlock\n */"; | ||
|
||
$class = $this->getMockBuilder(ReflectionClass::class) | ||
->setMethods(['getLocatedSource']) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$class | ||
->expects($this->once()) | ||
->method('getLocatedSource') | ||
->will($this->returnValue(new LocatedSource('<?php', null))); | ||
|
||
$method = $this->getMockBuilder(ReflectionMethod::class) | ||
->setMethods(['getDocComment', 'getDeclaringClass']) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$method | ||
->expects($this->once()) | ||
->method('getDocComment') | ||
->will($this->returnValue($docBlock)); | ||
|
||
$method | ||
->expects($this->once()) | ||
->method('getDeclaringClass') | ||
->will($this->returnValue($class)); | ||
|
||
/* @var ReflectionMethod $method */ | ||
$foundTypes = (new FindReturnType())->__invoke($method); | ||
|
||
$this->assertCount(count($expectedInstances), $foundTypes); | ||
|
||
foreach ($expectedInstances as $i => $expectedInstance) { | ||
$this->assertInstanceOf($expectedInstance, $foundTypes[$i]); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be
mixed
by default?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so, I don't think we should make any assumptions about return types. If they're there, we return them, if not, we don't. Plain and simple that way - if applications using Better Refletion want to make that assumption, they can do so simply.