-
Notifications
You must be signed in to change notification settings - Fork 568
phpstorm.meta.php support for dynamic return types #2219
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
Closed
Closed
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 |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\PhpStormMeta; | ||
|
|
||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Reflection\FunctionReflection; | ||
| use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
| use PHPStan\Type\Type; | ||
| use function array_map; | ||
| use function count; | ||
| use function in_array; | ||
| use function strtolower; | ||
|
|
||
| class FunctionReturnTypeResolver implements DynamicFunctionReturnTypeExtension | ||
| { | ||
|
|
||
| /** @var list<string> */ | ||
| private readonly array $functionNames; | ||
|
|
||
| /** | ||
| * @param list<string> $functionNames | ||
| */ | ||
| public function __construct( | ||
| private readonly TypeFromMetaResolver $metaResolver, | ||
| array $functionNames, | ||
| ) | ||
| { | ||
| $this->functionNames = array_map(strtolower(...), $functionNames); | ||
| } | ||
|
|
||
| public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
| { | ||
| $functionName = strtolower($functionReflection->getName()); | ||
|
|
||
| return in_array($functionName, $this->functionNames, true); | ||
| } | ||
|
|
||
| public function getTypeFromFunctionCall( | ||
| FunctionReflection $functionReflection, | ||
| FuncCall $functionCall, | ||
| Scope $scope, | ||
| ): ?Type | ||
| { | ||
| $functionName = strtolower($functionReflection->getName()); | ||
| $args = $functionCall->getArgs(); | ||
|
|
||
| if (count($args) > 0) { | ||
| return $this->metaResolver->resolveReferencedType($functionName, ...$args); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| } |
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,127 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\PhpStormMeta; | ||
|
|
||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PhpParser\Node\Name; | ||
| use PhpParser\Node\Stmt; | ||
| use PhpParser\Node\Stmt\Expression; | ||
| use PHPStan\File\FileHelper; | ||
| use PHPStan\Parser\CachedParser; | ||
| use PHPStan\PhpStormMeta\TypeMapping\CallReturnOverrideCollection; | ||
| use PHPStan\PhpStormMeta\TypeMapping\FunctionCallTypeOverride; | ||
| use PHPStan\PhpStormMeta\TypeMapping\MethodCallTypeOverride; | ||
| use SplFileInfo; | ||
| use Symfony\Component\Finder\Finder; | ||
| use function array_key_exists; | ||
| use function count; | ||
| use function file_exists; | ||
| use function is_dir; | ||
|
|
||
| class MetaFileParser | ||
| { | ||
|
|
||
| private readonly CallReturnOverrideCollection $parsedMeta; | ||
|
|
||
| /** @var array<string, string> */ | ||
| private array $parsedMetaPaths = []; | ||
|
|
||
| private bool $metaParsed; | ||
|
|
||
| /** | ||
| * @param list<string> $metaPaths | ||
| */ | ||
| public function __construct( | ||
| private readonly CachedParser $parser, | ||
| private readonly OverrideParser $overrideParser, | ||
| private readonly FileHelper $fileHelper, | ||
| private readonly array $metaPaths, | ||
| ) | ||
| { | ||
| $this->parsedMeta = new CallReturnOverrideCollection(); | ||
| $this->metaParsed = $this->metaPaths === []; | ||
| } | ||
|
|
||
| public function getMeta(): CallReturnOverrideCollection | ||
| { | ||
| if (!$this->metaParsed) { | ||
| $this->parseMeta($this->parsedMeta, ...$this->metaPaths); | ||
| $this->metaParsed = true; | ||
| } | ||
|
|
||
| return $this->parsedMeta; | ||
| } | ||
|
|
||
| private function parseMeta( | ||
| CallReturnOverrideCollection $resultCollector, | ||
| string ...$metaPaths, | ||
| ): void | ||
| { | ||
| $metaFiles = []; | ||
|
|
||
| /** @var array<string, string> $newlyParsedMetaPaths */ | ||
| $newlyParsedMetaPaths = []; | ||
| foreach ($metaPaths as $relativePath) { | ||
| if (array_key_exists($relativePath, $this->parsedMetaPaths)) { | ||
| continue; | ||
| } | ||
|
|
||
| $path = $this->fileHelper->absolutizePath($relativePath); | ||
|
|
||
| if (is_dir($path)) { | ||
| $finder = (new Finder())->in($path)->files()->ignoreDotFiles(false); | ||
| foreach ($finder as $fileInfo) { | ||
| $metaFiles [] = $fileInfo->getPathname(); | ||
| } | ||
| } elseif (file_exists($path)) { | ||
| $singleFile = new SplFileInfo($path); | ||
| $metaFiles[] = $singleFile->getPathname(); | ||
| } | ||
|
|
||
| $newlyParsedMetaPaths[$relativePath] = $path; | ||
| } | ||
|
|
||
| foreach ($metaFiles as $metaFile) { | ||
| $stmts = $this->parser->parseFile($metaFile); | ||
|
|
||
| foreach ($stmts as $topStmt) { | ||
| if (!($topStmt instanceof Stmt\Namespace_)) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($topStmt->name === null || $topStmt->name->toString() !== 'PHPSTORM_META') { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($topStmt->stmts as $metaStmt) { | ||
| if (!($metaStmt instanceof Expression) | ||
| || !($metaStmt->expr instanceof FuncCall) | ||
| || !($metaStmt->expr->name instanceof Name) | ||
| || $metaStmt->expr->name->toString() !== 'override' | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| $args = $metaStmt->expr->getArgs(); | ||
| if (count($args) < 2) { | ||
| continue; | ||
| } | ||
|
|
||
| [$callableArg, $overrideArg] = $args; | ||
| $parsedOverride = $this->overrideParser->parseOverride($callableArg, $overrideArg); | ||
|
|
||
| if ($parsedOverride instanceof MethodCallTypeOverride) { | ||
| $resultCollector->addMethodCallOverride($parsedOverride); | ||
| } elseif ($parsedOverride instanceof FunctionCallTypeOverride) { | ||
| $resultCollector->addFunctionCallOverride($parsedOverride); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| foreach ($newlyParsedMetaPaths as $relativePath => $path) { | ||
| $this->parsedMetaPaths[$relativePath] = $path; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
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,63 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\PhpStormMeta; | ||
|
|
||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Reflection\MethodReflection; | ||
| use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
| use PHPStan\Type\Type; | ||
| use function array_map; | ||
| use function count; | ||
| use function in_array; | ||
| use function sprintf; | ||
| use function strtolower; | ||
|
|
||
| class NonStaticMethodReturnTypeResolver implements DynamicMethodReturnTypeExtension | ||
| { | ||
|
|
||
| /** @var list<string> */ | ||
| private readonly array $methodNames; | ||
|
|
||
| /** | ||
| * @param list<string> $methodNames | ||
| */ | ||
| public function __construct( | ||
| private readonly TypeFromMetaResolver $metaResolver, | ||
| private readonly string $className, | ||
| array $methodNames, | ||
| ) | ||
| { | ||
| $this->methodNames = array_map(strtolower(...), $methodNames); | ||
| } | ||
|
|
||
| public function getClass(): string | ||
| { | ||
| return $this->className; | ||
| } | ||
|
|
||
| public function isMethodSupported(MethodReflection $methodReflection): bool | ||
| { | ||
| $methodName = strtolower($methodReflection->getName()); | ||
|
|
||
| return in_array($methodName, $this->methodNames, true); | ||
| } | ||
|
|
||
| public function getTypeFromMethodCall( | ||
| MethodReflection $methodReflection, | ||
| MethodCall $methodCall, | ||
| Scope $scope, | ||
| ): ?Type | ||
| { | ||
| $methodName = strtolower($methodReflection->getName()); | ||
| $args = $methodCall->getArgs(); | ||
|
|
||
| if (count($args) > 0) { | ||
| $fqn = sprintf('%s::%s', $this->className, $methodName); | ||
| return $this->metaResolver->resolveReferencedType($fqn, ...$args); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| } |
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.
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.
What does this path mean?
I'm clinging towards only looking at the project file for now, I've never seen a public package to include this file.
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.
here's an example of one in
mockery/mockeryhttps://github.com/mockery/mockery/blob/master/.phpstorm.meta.php
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.
According to PHPStorm’s documentation (link), I would say it scans the entire project for files and directories with this name, and parses them all. This implementation only looks for it next to
composer.json, but that is not the only place where PHPStorm would look.I found the following packages with this file in my projects:
nesbot/carbon,phpunit/phpunit,mockery/mockery,league/commonmark,nette/di,nette/utils. There are also several files with this name insidejetbrains/phpstorm-stubs, and there’sthomas-schulz/symfony-metathat provides these definitions for various Symfony components.I think that searching the entire project (or at least
vendor/) would give the closest experience to PHPStorm. But I can see arguments against this search as well, e.g. performance, metadata conflict resolution.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 think the way forward is to describe behaviour with advanced PHPDoc features which there are plenty of already, and PhpStorm starts supporting them too. That's why I think we don't need to look in
vendor.I think we should look for these files in
%composerAutoloaderProjectPaths%. These are the paths where PHPStan looks forcomposer.jsonfiles.Also, to be honest - I'm not sure I want to merge and maintain these 800 lines of code if the decided way forward for the community is advanced and already implemented PHPDoc features like conditional types,
asserttags etc.Maybe I can give you the needed hooks so that you can register these extensions in your own package instead?
Thank you for understanding.
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.
Yes, I can see your reasoning about leaving this out. Adding a hook in
LazyDynamicReturnTypeExtensionRegistryProviderto register these extensions would be enough, then I could leave this in a separate package. Even without such a hook, a separate package would still work, the users would just have to declare those extensions manually.