Skip to content

Commit

Permalink
Too wide @param-out - fix for conditional types
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 13, 2024
1 parent 27c73f7 commit 5a47893
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function sprintf;
Expand Down Expand Up @@ -85,6 +86,7 @@ private function processSingleParameter(
$outType = $parameter->getType();
}

$outType = TypeUtils::resolveLateResolvableTypes($outType);
if (!$outType instanceof UnionType) {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function sprintf;
Expand Down Expand Up @@ -85,6 +86,7 @@ private function processSingleParameter(
$outType = $parameter->getType();
}

$outType = TypeUtils::resolveLateResolvableTypes($outType);
if (!$outType instanceof UnionType) {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function testRule(): void
23,
'You can narrow the parameter out type with @param-out PHPDoc tag.',
],
[
'Function TooWideFunctionParameterOut\bug10699() never assigns 20 to &$out so it can be removed from the @param-out type.',
48,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function testRule(): void
26,
'You can narrow the parameter out type with @param-out PHPDoc tag.',
],
[
'Method TooWideMethodParameterOut\Foo::bug10699() never assigns 20 to &$out so it can be removed from the @param-out type.',
37,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ function ipCheckData(string $host, ?\stdClass &$ipdata): bool

return true;
}

/**
* @param int $flags
* @param 10 $out
*
* @param-out ($flags is 2 ? 20 : 10) $out
*/
function bug10699(int $flags, &$out): void
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,16 @@ public function doLorem(?string &$p): void
$p = 'foo';
}

/**
* @param int $flags
* @param 10 $out
*
* @param-out ($flags is 2 ? 20 : 10) $out
*/
function bug10699(int $flags, &$out): void
{

}


}

0 comments on commit 5a47893

Please sign in to comment.