Skip to content

Commit 531a677

Browse files
committed
Do not fix too-wide return types the returned values are not a subtype of
When a value is accepted only by coercion, like int by float, or the declared type does not accept it at all, removing the members the rule reports as unused can break the function, for example ?float became null in a function returning 1.
1 parent e879fc9 commit 531a677

5 files changed

Lines changed: 99 additions & 0 deletions

File tree

‎src/Rules/TooWideTypehints/TooWideTypeCheck.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,21 @@ public function createReturnTypeFixPlan(
282282
return null;
283283
}
284284

285+
// when the declared type accepts a returned value only by coercion, like int by float, or does not accept it at all,
286+
// it is not known which member the value belongs to and removing members could break the function
287+
$functionReturnType = TypeUtils::resolveLateResolvableTypes(TypehintHelper::decideType($nativeFunctionReturnType, $phpDocFunctionReturnType));
288+
$returnType = $this->getReturnedType($node);
289+
if ($returnType !== null) {
290+
$returnedNull = !(new VoidType())->isSuperTypeOf($returnType)->no();
291+
$returnType = TypeCombinator::remove($returnType, new VoidType());
292+
if ($returnedNull) {
293+
$returnType = TypeCombinator::addNull($returnType);
294+
}
295+
if (!$functionReturnType->isSuperTypeOf($returnType)->yes()) {
296+
return null;
297+
}
298+
}
299+
285300
if ($node instanceof MethodReturnStatementsNode) {
286301
$functionNode = $node->getClassMethod();
287302
$classReflection = $node->getClassReflection();

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/functions-php73.php.fixed‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,24 @@ function inlineVarTag(): ?\stdClass
9797

9898
return $object;
9999
}
100+
101+
function coercedToFloat(bool $b): ?float
102+
{
103+
if ($b) {
104+
return 1;
105+
}
106+
107+
return null;
108+
}
109+
110+
/**
111+
* @return \DateTimeImmutable|\Exception|null
112+
*/
113+
function returnsUndeclaredType(bool $b)
114+
{
115+
if ($b) {
116+
return new \stdClass();
117+
}
118+
119+
return null;
120+
}

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/functions-php74.php.fixed‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,24 @@ function inlineVarTag(): ?\stdClass
9797

9898
return $object;
9999
}
100+
101+
function coercedToFloat(bool $b): ?float
102+
{
103+
if ($b) {
104+
return 1;
105+
}
106+
107+
return null;
108+
}
109+
110+
/**
111+
* @return \DateTimeImmutable|\Exception|null
112+
*/
113+
function returnsUndeclaredType(bool $b)
114+
{
115+
if ($b) {
116+
return new \stdClass();
117+
}
118+
119+
return null;
120+
}

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/functions-php82.php.fixed‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,24 @@ function inlineVarTag(): ?\stdClass
9797

9898
return $object;
9999
}
100+
101+
function coercedToFloat(bool $b): ?float
102+
{
103+
if ($b) {
104+
return 1;
105+
}
106+
107+
return null;
108+
}
109+
110+
/**
111+
* @return \DateTimeImmutable|\Exception|null
112+
*/
113+
function returnsUndeclaredType(bool $b)
114+
{
115+
if ($b) {
116+
return new \stdClass();
117+
}
118+
119+
return null;
120+
}

‎tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type/functions.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,24 @@ function inlineVarTag(): ?\stdClass
9797

9898
return $object;
9999
}
100+
101+
function coercedToFloat(bool $b): ?float
102+
{
103+
if ($b) {
104+
return 1;
105+
}
106+
107+
return null;
108+
}
109+
110+
/**
111+
* @return \DateTimeImmutable|\Exception|null
112+
*/
113+
function returnsUndeclaredType(bool $b)
114+
{
115+
if ($b) {
116+
return new \stdClass();
117+
}
118+
119+
return null;
120+
}

0 commit comments

Comments
 (0)