Skip to content

Commit c90b96c

Browse files
SanderMullerclaude
authored andcommitted
Inline the variadic element type into the two parameter-out checks
The helper class held three lines of logic. Both checks now unpack the element type themselves, and the reason for it moves to the docblock of ParameterOutTypeCheck. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent a291dbe commit c90b96c

3 files changed

Lines changed: 10 additions & 43 deletions

File tree

‎src/Rules/TooWideTypehints/TooWideParameterOutTypeCheck.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PHPStan\Node\ReturnStatement;
1010
use PHPStan\Reflection\ExtendedParameterReflection;
1111
use PHPStan\Rules\IdentifierRuleError;
12-
use PHPStan\Rules\VariadicByRefParameterOutType;
1312
use function lcfirst;
1413
use function sprintf;
1514

@@ -95,11 +94,12 @@ private function processSingleParameter(
9594
$variableExpr = new Variable($parameter->getName());
9695
$variableType = $scope->getType($variableExpr);
9796

97+
// a variadic out type describes one argument - see ParameterOutTypeCheck
9898
if ($parameter->isVariadic()) {
99-
$variableType = VariadicByRefParameterOutType::elementType($variableType);
100-
if ($variableType === null) {
99+
if (!$variableType->isArray()->yes()) {
101100
return [];
102101
}
102+
$variableType = $variableType->getIterableValueType();
103103
}
104104

105105
return $this->tooWideTypeCheck->checkParameterOutType(

‎src/Rules/Variables/ParameterOutTypeCheck.php‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use PHPStan\Rules\IdentifierRuleError;
1212
use PHPStan\Rules\RuleErrorBuilder;
1313
use PHPStan\Rules\RuleLevelHelper;
14-
use PHPStan\Rules\VariadicByRefParameterOutType;
1514
use PHPStan\Type\ErrorType;
1615
use PHPStan\Type\Type;
1716
use PHPStan\Type\VerbosityLevel;
@@ -23,8 +22,11 @@
2322
* The promise is either an explicit `@param-out` or, in its absence, the parameter's own type.
2423
* Which one it is only shows in the error message, so callers report it via $isParamOutType.
2524
*
26-
* For a variadic parameter the promise describes a single argument while the variable holds the
27-
* packed array of them, so the two sides are reconciled through VariadicByRefParameterOutType.
25+
* For a variadic parameter the promise describes a single argument, as NodeScopeResolver applies it at
26+
* the call site, while the variable holds the packed array of them, so its element type is compared.
27+
* Once the variable no longer holds an array, rebinding it has discarded the references and nothing
28+
* reaches a caller. A write through an offset, `$refs[0] = ...`, does reach the caller and leaves an
29+
* array, so an array is always compared.
2830
*
2931
* @internal
3032
*/
@@ -65,10 +67,10 @@ public function check(
6567

6668
$assignedExprType = $scope->getType($checkedExpr);
6769
if ($isVariadic) {
68-
$assignedExprType = VariadicByRefParameterOutType::elementType($assignedExprType);
69-
if ($assignedExprType === null) {
70+
if (!$assignedExprType->isArray()->yes()) {
7071
return [];
7172
}
73+
$assignedExprType = $assignedExprType->getIterableValueType();
7274
}
7375

7476
if ($outType->isSuperTypeOf($assignedExprType)->yes()) {

‎src/Rules/VariadicByRefParameterOutType.php‎

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)