Skip to content

Commit

Permalink
Too wide return type - report for conditional return types
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 13, 2024
1 parent 5a47893 commit de1c07e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function count;
Expand All @@ -30,6 +31,7 @@ public function processNode(Node $node, Scope $scope): array
$function = $node->getFunctionReflection();

$functionReturnType = ParametersAcceptorSelector::selectSingle($function->getVariants())->getReturnType();
$functionReturnType = TypeUtils::resolveLateResolvableTypes($functionReturnType);
if (!$functionReturnType instanceof UnionType) {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function count;
Expand Down Expand Up @@ -56,6 +57,7 @@ public function processNode(Node $node, Scope $scope): array
}

$methodReturnType = ParametersAcceptorSelector::selectSingle($method->getVariants())->getReturnType();
$methodReturnType = TypeUtils::resolveLateResolvableTypes($methodReturnType);
if (!$methodReturnType instanceof UnionType) {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public function testRule(): void
'Function TooWideFunctionReturnType\dolor6() never returns null so it can be removed from the return type.',
79,
],
[
'Function TooWideFunctionReturnType\conditionalType() never returns string so it can be removed from the return type.',
90,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public function testPrivate(): void
'Method TooWideMethodReturnType\Foo::dolor6() never returns null so it can be removed from the return type.',
86,
],
[
'Method TooWideMethodReturnType\ConditionalTypeClass::conditionalType() never returns string so it can be removed from the return type.',
119,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,11 @@ function dolor6() {

return 'bar';
}

/**
* @return ($flag is 1 ? string : int)
*/
function conditionalType(int $flag)
{
return $flag;
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,17 @@ class UsesFooTrait
use FooTrait;

}

class ConditionalTypeClass
{

/**
* @return ($flag is 1 ? int : string)
*/
private function conditionalType(int $flag)
{
return $flag;
}


}

0 comments on commit de1c07e

Please sign in to comment.