feat: add NarrowTooWideReturnTypeRector#7150
Conversation
|
This skips all native mixed returns as can't be sure |
83bd9a1 to
3e59478
Compare
rules/DeadCode/Rector/FunctionLike/NarrowTooWideReturnTypeRector.php
Outdated
Show resolved
Hide resolved
|
Please run on existing this change seems invalid and should be skipped: 9) src/Console/Command/SetupCICommand.php:50
---------- begin diff ----------
@@ @@
return self::SUCCESS;
}
- /**
- * @return CiDetector::CI_*|null
- */
- private function resolveCurrentCI(): ?string
+ private function resolveCurrentCI(): null
{
if (file_exists(getcwd() . '/.github')) {
return CiDetector::CI_GITHUB_ACTIONS;
----------- end diff -----------
Applied rules:
* RemoveUselessReturnTagRector
* NarrowTooWideReturnTypeRector |
|
@samsonasik, there's only 1 change now:
|
|
The issue on so probably better loop the docblock type, if found something that can't be converted to native type, just skip it, eg these pattern: |
|
is The docs would seem to want to use https://phpstan.org/writing-php-code/phpdoc-types#key-and-value-types-of-arrays-and-iterables |
|
Yes, that's valid docblock, see https://phpstan.org/r/77bb2280-9ef5-48a3-846a-02009e413399 |
|
You can fixture to skip this: use OndraM\CiDetector\CiDetector;
/**
* @return int|CiDetector::CI_*|null
*/
function skipSomeComplexDocSubType(): int|string|null
{
if (file_exists(getcwd() . '/.github')) {
return CiDetector::CI_GITHUB_ACTIONS;
}
if (file_exists(getcwd() . '/.gitlab-ci.yml')) {
return CiDetector::CI_GITLAB;
}
if (rand(0, 1)) {
return 1;
}
return null;
} |
I'm digging into this, and it's a bug in the |
|
Please add fixture for this example to be skipped: use OndraM\CiDetector\CiDetector;
/**
* @return int|CiDetector::CI_*|null
*/
function skipSomeComplexDocSubType(): int|string|null
{
if (file_exists(getcwd() . '/.github')) {
return CiDetector::CI_GITHUB_ACTIONS;
}
if (file_exists(getcwd() . '/.gitlab-ci.yml')) {
return CiDetector::CI_GITLAB;
}
if (rand(0, 1)) {
return 1;
}
return null;
} |
|
That seems not, as its docblock value become extracted, and not what we want: 1) src/Console/Command/SetupCICommand.php:51
---------- begin diff ----------
@@ @@
}
/**
- * @return int|CiDetector::CI_*|null
+ * @return 'GitHub Actions'|'GitLab'|int|null
*/
private function resolveCurrentCI(): int|string|null
{
@@ @@
return CiDetector::CI_GITLAB;
}
- if (rand(0, 1)) {
+ if (random_int(0, 1) !== 0) {
return 1;
}
----------- end diff -----------
Applied rules:
* ExplicitBoolCompareRector
* NarrowTooWideReturnTypeRector
* RandomFunctionRector |
|
Sorry, I should have clarified, I have another test fixture with a union of exact values and it's removing all but the last one |
|
I'll work on this more tomorrow, I'm going to restructure the rector so that the native and phpdoc return types are handled separately |
rules/DeadCode/Rector/FunctionLike/NarrowTooWideReturnTypeRector.php
Outdated
Show resolved
Hide resolved
rules/DeadCode/Rector/FunctionLike/NarrowTooWideReturnTypeRector.php
Outdated
Show resolved
Hide resolved
|
@samsonasik, I've refactored the code to handle the native return types and the phpdoc return types separately and this helps with flexibility. However, the test is failing because calling
Commenting out this line causes the test to pass because the extra constant types are no longer being removed:
|
|
I will check your branch and take care the rest, I prefer to just skip these for now:
which improvement can be in separate PRs |
|
@calebdw this should be good to merge now 👍 , I skipped various complex sub type and mostly placeholder: null, false, true usage. Improvement if needed, can be in separate PR. thank you @TomasVotruba let's merge it and test in the wild ;) |
|
Can't wait to test this on clients' projects 👌😎 Thank you @calebdw |
|
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!
Supercedes #7147
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!