feat: add NarrowTooWideReturnTypeRector#7147
feat: add NarrowTooWideReturnTypeRector#7147calebdw wants to merge 17 commits intorectorphp:mainfrom
Conversation
ffa5c1c to
856c8f3
Compare
|
Thanks for great rule 👌 Will save a lot of manual work. The rule name seems negative, like PHPStan rule. |
.../DeadCode/Rector/FunctionLike/TooWideReturnTypeRector/Fixture/skip_non_final_classes.php.inc
Outdated
Show resolved
Hide resolved
...s/DeadCode/Rector/FunctionLike/TooWideReturnTypeRector/Fixture/skip_return_all_types.php.inc
Outdated
Show resolved
Hide resolved
rules-tests/DeadCode/Rector/FunctionLike/TooWideReturnTypeRector/Fixture/arrow_function.php.inc
Outdated
Show resolved
Hide resolved
rules-tests/DeadCode/Rector/FunctionLike/TooWideReturnTypeRector/Fixture/edge_cases.php.inc
Show resolved
Hide resolved
d2f8c9b to
ca11ae8
Compare
rules/DeadCode/Rector/FunctionLike/NarrowTooWideReturnTypeRector.php
Outdated
Show resolved
Hide resolved
ca11ae8 to
946abdb
Compare
e8e70bb to
e138a71
Compare
...-tests/DeadCode/Rector/FunctionLike/NarrowTooWideReturnTypeRector/Fixture/edge_cases.php.inc
Outdated
Show resolved
Hide resolved
ec418af to
627b521
Compare
...-tests/DeadCode/Rector/FunctionLike/NarrowTooWideReturnTypeRector/Fixture/edge_cases.php.inc
Outdated
Show resolved
Hide resolved
89d59c5 to
ecb9c33
Compare
|
Please:
|
2af5b38 to
7558829
Compare
|
I think /**
- * @return \Iterator<int>|string
+ * @return \Iterator<int>
*/
-function foo(): \Iterator|string
+function foo(): \Iterator
{
return new \ArrayIterator([1]);
}You can use : $this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, $newType); |
I imagine that I can longer just use |
|
|
|
I'm not too clear on the difference, but I imagine that |
|
$types = $returnType instanceof UnionType
? $returnType->types
: [$returnType->type, new Identifier('null')];
$usedTypes = [];
foreach ($types as $type) {
$declaredType = $type instanceof Expr
? $this->nodeTypeResolver->getNativeType($type)
: $this->getType($type);The |
rules/DeadCode/Rector/FunctionLike/NarrowTooWideReturnTypeRector.php
Outdated
Show resolved
Hide resolved
a3111e2 to
6837fd4
Compare
|
Please add fixture to skip docblock base type: /**
* @param int $a
*/
function foo($a): int|string
{
return $a;
} |
|
Please add test for this: /**
* @param int $a
* @return int|string
*/
function mixedReturn($a): int|string
{
return $a;
} |
rules-tests/DeadCode/Rector/FunctionLike/NarrowTooWideReturnTypeRector/Fixture/phpdocs.php.inc
Show resolved
Hide resolved
| * @param class-string<SomeInterface> $class | ||
| * @return class-string<SomeInterface>|int | ||
| */ | ||
| public function bar(string $class): string|int |
There was a problem hiding this comment.
please add fixture to skip non-typed based then, to skip this:
/**
* @param class-string<SomeInterface> $class
* @return class-string<SomeInterface>|int
*/
public function bar($class): string|int
{
return $class;
}Above is marked as MixedType on its type, see
There was a problem hiding this comment.
I see no reason to do that, this signature is enforced by PHPStan and trying to loop over all the parameter types is only going to unnecessarily complicate the code
There was a problem hiding this comment.
That's needed, upgrading to native type is only when we sure about this, when unsure, just skip :)
We had docblock based changing rules in the past, but mostly cause issues in projects, that's why using native type as posible should be used
There was a problem hiding this comment.
Expr returned is not always based on param, see my latest comments :)
441b483 to
a9e401f
Compare
|
Please add fixture to skip this, which actually skip MixedType: /**
* @return class-string<\stdClass>|int
*/
public function skipMixedByDoc(): string|int
{
/**
* @var class-string<\stdClass> $class
*/
$class = get();
return $class;
}see https://phpstan.org/r/47037a56-f5da-4e99-bb3d-7adc3b609612 that it still mixed. |
|
@calebdw sorry, I am going to close this, as you don't follow my guideline and suggestion. I am here for the barricade to avoid the new rule cause issues in our project. Take your time to understand our philosophy to take upgrade as safe as possible. Thank you for understanding |
|
This pull request has been automatically locked because it has been closed for 150 days. Please open a new PR if you want to continue the work. |
Hello!
Closes rectorphp/rector#9309
This adds a rule that automatically narrows too wide return types (helps solve the
return.unusedTypephpstan errors):final class SomeClass { - public function getData(): string|int|\DateTime + public function getData(): string|int { if (rand(0, 1)) { return 'text'; } return 1000; } }Thanks!