Skip to content

Commit de1c07e

Browse files
committed
Too wide return type - report for conditional return types
1 parent 5a47893 commit de1c07e

File tree

6 files changed

+34
-0
lines changed

6 files changed

+34
-0
lines changed

src/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\Rules\Rule;
1010
use PHPStan\Rules\RuleErrorBuilder;
1111
use PHPStan\Type\TypeCombinator;
12+
use PHPStan\Type\TypeUtils;
1213
use PHPStan\Type\UnionType;
1314
use PHPStan\Type\VerbosityLevel;
1415
use function count;
@@ -30,6 +31,7 @@ public function processNode(Node $node, Scope $scope): array
3031
$function = $node->getFunctionReflection();
3132

3233
$functionReturnType = ParametersAcceptorSelector::selectSingle($function->getVariants())->getReturnType();
34+
$functionReturnType = TypeUtils::resolveLateResolvableTypes($functionReturnType);
3335
if (!$functionReturnType instanceof UnionType) {
3436
return [];
3537
}

src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Rules\RuleErrorBuilder;
1111
use PHPStan\Type\Constant\ConstantBooleanType;
1212
use PHPStan\Type\TypeCombinator;
13+
use PHPStan\Type\TypeUtils;
1314
use PHPStan\Type\UnionType;
1415
use PHPStan\Type\VerbosityLevel;
1516
use function count;
@@ -56,6 +57,7 @@ public function processNode(Node $node, Scope $scope): array
5657
}
5758

5859
$methodReturnType = ParametersAcceptorSelector::selectSingle($method->getVariants())->getReturnType();
60+
$methodReturnType = TypeUtils::resolveLateResolvableTypes($methodReturnType);
5961
if (!$methodReturnType instanceof UnionType) {
6062
return [];
6163
}

tests/PHPStan/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public function testRule(): void
4444
'Function TooWideFunctionReturnType\dolor6() never returns null so it can be removed from the return type.',
4545
79,
4646
],
47+
[
48+
'Function TooWideFunctionReturnType\conditionalType() never returns string so it can be removed from the return type.',
49+
90,
50+
],
4751
]);
4852
}
4953

tests/PHPStan/Rules/TooWideTypehints/TooWideMethodReturnTypehintRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function testPrivate(): void
4848
'Method TooWideMethodReturnType\Foo::dolor6() never returns null so it can be removed from the return type.',
4949
86,
5050
],
51+
[
52+
'Method TooWideMethodReturnType\ConditionalTypeClass::conditionalType() never returns string so it can be removed from the return type.',
53+
119,
54+
],
5155
]);
5256
}
5357

tests/PHPStan/Rules/TooWideTypehints/data/tooWideFunctionReturnType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,11 @@ function dolor6() {
8383

8484
return 'bar';
8585
}
86+
87+
/**
88+
* @return ($flag is 1 ? string : int)
89+
*/
90+
function conditionalType(int $flag)
91+
{
92+
return $flag;
93+
}

tests/PHPStan/Rules/TooWideTypehints/data/tooWideMethodReturnType-private.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,17 @@ class UsesFooTrait
109109
use FooTrait;
110110

111111
}
112+
113+
class ConditionalTypeClass
114+
{
115+
116+
/**
117+
* @return ($flag is 1 ? int : string)
118+
*/
119+
private function conditionalType(int $flag)
120+
{
121+
return $flag;
122+
}
123+
124+
125+
}

0 commit comments

Comments
 (0)