Implement ReturnTypeFromStrictParamRector#4501
Conversation
|
@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. |
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 { |
| if ($node->stmts === null) { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
this can be moved to shouldSkipNode() method so early cheap check is handled for abstract method.
There was a problem hiding this comment.
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.
No worries, I will check after it get merged 👍
rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector.php
Outdated
Show resolved
Hide resolved
rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictParamRector.php
Show resolved
Hide resolved
2331300 to
f3a19cb
Compare
|
should be good to go |
|
ping @TomasVotruba :) |
|
Looks good, let's give it a try 👍 |
<?php namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector\Fixture; class Example { - public function doFoo(Example $param) { + public function doFoo(Example $param): Example { return $param; } } ?>