-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TypeInfo] Add PhpDocAwareReflectionTypeResolver
#57618
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.2 | ||
--- | ||
|
||
* Add `PhpDocAwareReflectionTypeResolver` resolver | ||
|
||
7.1 | ||
--- | ||
|
||
|
21 changes: 21 additions & 0 deletions
21
src/Symfony/Component/TypeInfo/Tests/Fixtures/DummyWithPhpDoc.php
This file contains hidden or 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,21 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\TypeInfo\Tests\Fixtures; | ||
|
||
final class DummyWithPhpDoc | ||
{ | ||
/** | ||
* @var array<Dummy> | ||
*/ | ||
public mixed $arrayOfDummies = []; | ||
|
||
/** | ||
* @param Dummy $dummy | ||
* | ||
* @return Dummy | ||
*/ | ||
public function getNextDummy(mixed $dummy): mixed | ||
{ | ||
throw new \BadMethodCallException(sprintf('"%s" is not implemented.', __METHOD__)); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Symfony/Component/TypeInfo/Tests/TypeResolver/PhpDocAwareReflectionTypeResolverTest.php
This file contains hidden or 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,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\TypeInfo\Tests\TypeResolver; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\TypeInfo\Tests\Fixtures\Dummy; | ||
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithPhpDoc; | ||
use Symfony\Component\TypeInfo\Type; | ||
use Symfony\Component\TypeInfo\TypeContext\TypeContextFactory; | ||
use Symfony\Component\TypeInfo\TypeResolver\PhpDocAwareReflectionTypeResolver; | ||
use Symfony\Component\TypeInfo\TypeResolver\StringTypeResolver; | ||
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver; | ||
|
||
class PhpDocAwareReflectionTypeResolverTest extends TestCase | ||
{ | ||
public function testReadPhpDoc() | ||
{ | ||
$resolver = new PhpDocAwareReflectionTypeResolver(TypeResolver::create(), new StringTypeResolver(), new TypeContextFactory()); | ||
$reflection = new \ReflectionClass(DummyWithPhpDoc::class); | ||
|
||
$this->assertEquals(Type::array(Type::object(Dummy::class)), $resolver->resolve($reflection->getProperty('arrayOfDummies'))); | ||
$this->assertEquals(Type::object(Dummy::class), $resolver->resolve($reflection->getMethod('getNextDummy'))); | ||
$this->assertEquals(Type::object(Dummy::class), $resolver->resolve($reflection->getMethod('getNextDummy')->getParameters()[0])); | ||
} | ||
|
||
public function testFallbackWhenNoPhpDoc() | ||
{ | ||
$resolver = new PhpDocAwareReflectionTypeResolver(TypeResolver::create(), new StringTypeResolver(), new TypeContextFactory()); | ||
$reflection = new \ReflectionClass(Dummy::class); | ||
|
||
$this->assertEquals(Type::int(), $resolver->resolve($reflection->getProperty('id'))); | ||
$this->assertEquals(Type::int(), $resolver->resolve($reflection->getMethod('getId'))); | ||
$this->assertEquals(Type::int(), $resolver->resolve($reflection->getMethod('setId')->getParameters()[0])); | ||
} | ||
} |
6D40
This file contains hidden or 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
87 changes: 87 additions & 0 deletions
87
src/Symfony/Component/TypeInfo/TypeResolver/PhpDocAwareReflectionTypeResolver.php
This file contains hidden or 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,87 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\TypeInfo\TypeResolver; | ||
|
||
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; | ||
use PHPStan\PhpDocParser\Lexer\Lexer; | ||
use PHPStan\PhpDocParser\Parser\ConstExprParser; | ||
use PHPStan\PhpDocParser\Parser\PhpDocParser; | ||
use PHPStan\PhpDocParser\Parser\TokenIterator; | ||
use PHPStan\PhpDocParser\Parser\TypeParser; | ||
use Symfony\Component\TypeInfo\Exception\UnsupportedException; | ||
use Symfony\Component\TypeInfo\Type; | ||
use Symfony\Component\TypeInfo\TypeContext\TypeContext; | ||
use Symfony\Component\TypeInfo\TypeContext\TypeContextFactory; | ||
use Symfony\Component\TypeInfo\TypeResolver\TypeResolverInterface; | ||
|
||
/** | ||
* Resolves type on reflection prioriziting PHP documentation. | ||
* | ||
* @author Mathias Arlaud <mathias.arlaud@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
final readonly class PhpDocAwareReflectionTypeResolver implements TypeResolverInterface | ||
{ | ||
public function __construct( | ||
private TypeResolverInterface $reflectionTypeResolver, | ||
private TypeResolverInterface $stringTypeResolver, | ||
private TypeContextFactory $typeContextFactory, | ||
private PhpDocParser $phpDocParser = new PhpDocParser(new TypeParser(), new ConstExprParser()), | ||
private Lexer $lexer = new Lexer(), | ||
) { | ||
} | ||
|
||
public function resolve(mixed $subject, ?TypeContext $typeContext = null): Type | ||
{ | ||
if (!$subject instanceof \ReflectionProperty && !$subject instanceof \ReflectionParameter && !$subject instanceof \ReflectionFunctionAbstract) { | ||
throw new UnsupportedException(sprintf('Expected subject to be a "ReflectionProperty", a "ReflectionParameter" or a "ReflectionFunctionAbstract", "%s" given.', get_debug_type($subject)), $subject); | ||
} | ||
|
||
$docComment = match (true) { | ||
$subject instanceof \ReflectionProperty => $subject->getDocComment(), | ||
$subject instanceof \ReflectionParameter => $subject->getDeclaringFunction()->getDocComment(), | ||
$subject instanceof \ReflectionFunctionAbstract => $subject->getDocComment(), | ||
}; | ||
|
||
if (!$docComment) { | ||
return $this->reflectionTypeResolver->resolve($subject); | ||
} | ||
|
||
$typeContext ??= $this->typeContextFactory->createFromReflection($subject); | ||
|
||
$tagName = match (true) { | ||
$subject instanceof \ReflectionProperty => '@var', | ||
$subject instanceof \ReflectionParameter => '@param', | ||
$subject instanceof \ReflectionFunctionAbstract => '@return', | ||
}; | ||
|
||
$tokens = new TokenIterator($this->lexer->tokenize($docComment)); | ||
$docNode = $this->phpDocParser->parse($tokens); | ||
|
||
foreach ($docNode->getTagsByName($tagName) as $tag) { | ||
$tagValue = $tag->value; | ||
|
||
if ( | ||
$tagValue instanceof VarTagValueNode | ||
|| $tagValue instanceof ParamTagValueNode && $tagName && '$'.$subject->getName() === $tagValue->parameterName | ||
|| $tagValue instanceof ReturnTagValueNode | ||
) { | ||
return $this->stringTypeResolver->resolve((string) $tagValue, $typeContext); | ||
} | ||
} | ||
|
||
return $this->reflectionTypeResolver->resolve($subject); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.