-
-
Notifications
You must be signed in to change notification settings - Fork 374
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
Implement ReturnTypeFromStrictParamRector #4501
Conversation
staabm
commented
Jul 13, 2023
•
edited
Loading
edited
@samsonasik hopefully I am right and this feature is not covered by a existing rule which I was not able to find ? :-) |
Possible void return fixture should be added: class SkipPossibleVoid
{
public function run(int $p)
{
if (rand(0, 1)) {
return $p;
}
}
} You can check with root Return validation like in other rule: $inferReturnType = $this->returnTypeInferer->inferFunctionLike($node); and verify the inferred return type. rector-src/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php Lines 135 to 140 in a10ddea
|
great hint, thanks.
I am not sure I would want to infer a union type at all (besides a simple nullable-object-type). |
Yes, but that possible void are exists in many legacy code :) |
could you give me an example? the one you posted above is already covered |
not sure if I can find and show it here, but It seems you already covered it 👍 |
...laration/Rector/ClassMethod/ReturnTypeFromStrictParamRector/Fixture/skip_call_by_ref.php.inc
Show resolved
Hide resolved
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; | ||
|
||
class SkipAssign { |
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.
covers https://3v4l.org/E2XOE
if ($node->stmts === null) { | ||
return null; | ||
} |
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 can be moved to shouldSkipNode()
method so early cheap check is handled for abstract method.
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.
👍
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.
reverted, since its required to make phpstan happy
------ ------------------------------------------------------------------------------
Line rules\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector.php
------ ------------------------------------------------------------------------------
128 Parameter #1 $nodes of method
Rector\Core\Rector\AbstractRector::traverseNodesWithCallable()
expects array<PhpParser\Node>|PhpParser\Node,
array<PhpParser\Node\Stmt>|null given.
157 Parameter #1 $nodes of method
Rector\Core\Rector\AbstractRector::traverseNodesWithCallable()
expects array<PhpParser\Node>|PhpParser\Node,
array<PhpParser\Node\Stmt>|null given.
------ ------------------------------------------------------------------------------
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.
No worries, I will check after it get merged 👍
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.
rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector.php
Outdated
Show resolved
Hide resolved
rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector.php
Show resolved
Hide resolved
Co-authored-by: Abdul Malik Ikhsan <[email protected]>
This reverts commit 82107dd.
This reverts commit 6d61b59.
2331300
to
f3a19cb
Compare
should be good to go |
ping @TomasVotruba :) |
Looks good, let's give it a try 👍 |