-
Notifications
You must be signed in to change notification settings - Fork 532
Add @param-out
support
#1804
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
Add @param-out
support
#1804
Conversation
5e4eed5
to
54ff192
Compare
In NodeScopeResolver there are places that are interested in PassedByReference
- @param-out
should be handled there.
Also - require your version of phpdoc-parser with the support added in composer.json, so that the pipeline can actually get green.
Also, you have to guard any call to new method on PhpDocNode with method_exists
so that Rector can do its downgrading job. See: #1799 (comment)
thanks for the tips.
I tried testing the PR with the following code
<?php use function PHPStan\Testing\assertType; class X { /** * @param-out string $s */ function addFoo(?string &$s): void { if ($s === null) { $s = "hello"; } $s .= "foo"; } } function foo1(?string $s) { assertType('string|null', $s); $x = new X(); $x->addFoo($s); assertType('string', $s); }
but I currently struggle with php-doc resolving. 2 problems arise:
(削除) inNodeScopeResolver->getParameterOutTypes(..)
the phpdoc/** @param-out string $s */
gets always resolved to an empty-block from these lines (削除ここまで)phpstan-src/src/Type/FileTypeMapper.php
Lines 111 to 113 in 49cd131
if (!isset($this->inProcess[$fileName][$nameScopeKey])) { // wrong $fileName due to traitsreturn ResolvedPhpDocBlock::createEmpty();}(削除)FunctionReflection
does not yet contain agetDocComment
method (likeMethodReflection
has)... it seems this is a missing part I need to implement with this PR? (削除ここまで)
I should write comments about my problems more often... sorry/thanks for rubberducking.. found the reason for doc-block resolving problem because I had mixed up caller and callee side of things - 48a2cf8.
I think the PR works now as expected for basic method examples. open todo is for regular functions. I would implement a getDocComment
method on FunctionReflection
now, to get it working... if we agree on this
48a2cf8
to
6a7faf2
Compare
5a0d684
to
a316129
Compare
9a1e4f9
to
3b8ff47
Compare
ba182dd
to
53c3b12
Compare
(削除) for some reason the PR as is failling after rebase, because I am running into (削除ここまで) -> fixed by re-adding a recently added use
-statement which got lost after manual merge conflict resolving
phpstan-src/src/Analyser/NodeScopeResolver.php
Lines 531 to 533 in f097ca9
e274a59
to
09342ee
Compare
2c3136e
to
2df5ad2
Compare
You updated a lot irrelevant deps. You need to run just composer update phpstan/phpdoc-parser
instead.
f43fc38
to
a7347f5
Compare
And now you get relevant build errors finally 😊
5823ae9
to
3cdc61b
Compare
I had a look into the psalm test-suite for some more advanced use-cases... I will push them as a followup after we got the basic support landed
3cdc61b
to
b768df8
Compare
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.
Also please test a scenario where @param-out
on a method is inherited from a parent:
- When the parameter names match.
- When the parameters are renamed (but it should still be applied based on parameter order).
- When the
@param-out
is overriden for the same parameter in the child class.
src/Analyser/NodeScopeResolver.php
Outdated
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.
FunctionReflection|ExtendedMethodReflection
should already have a method for @param-out
tags. But to be honest, it should probably be an information for a specific parameter on ParameterReflectionWithPhpDocs
to be really clean. Something like public function getOutType(): ?Type
.
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.
You shouldn't definitely parse PHPDoc here again. That's a job for PhpClassReflectionExtension, BetterReflectionProvider::getCustomFunction()
and NativeFunctionReflectionProvider
.
test.php
Outdated
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.
This file shouldn't be commited.
ad647db
to
35e3325
Compare
fixed and rebased. thank you
@param-out
support (追記ここまで)
Thank you!
btw: I opened a new issue on php-src, to add/discuss @param-out
directly into php-src-stubs
@staabm Could you describe and open another issue for flag constants added to the stubs too? Like all the valid constants for $flags
on json_encode (https://www.php.net/manual/en/function.json-encode.php) could be there.
open another issue for flag constants added to the stubs too?
here we go: php/php-src#9900
Uh oh!
There was an error while loading. Please reload this page.
requires phpstan/phpdoc-parser#150
refs phpstan/phpstan#6426 (comment)
closes phpstan/phpstan#7231
closes phpstan/phpstan#6871
closes phpstan/phpstan#6186
closes phpstan/phpstan#4372
see https://psalm.dev/docs/annotating_code/supported_annotations/#param-out-psalm-param-out
just put togehter the basic building blocks and tests:
I had the idea of adding param-out-type specification of the expressions right before the return-type of
FuntionLike
s is handled - I have not yet an idea where this place is though and whether thats a good idea :)would be great to get a hint, where/how the actual param-out logic should be implemented.