Skip to content

Commit

Permalink
Check conditional types in @param-out
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 13, 2024
1 parent effad95 commit 67f9420
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Rules/PhpDoc/ConditionalReturnTypeRuleHelper.php
Expand Up @@ -2,7 +2,7 @@

namespace PHPStan\Rules\PhpDoc;

use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ConditionalType;
Expand All @@ -23,7 +23,7 @@ class ConditionalReturnTypeRuleHelper
/**
* @return RuleError[]
*/
public function check(ParametersAcceptor $acceptor): array
public function check(ParametersAcceptorWithPhpDocs $acceptor): array
{
$conditionalTypes = [];
$parametersByName = [];
Expand All @@ -36,6 +36,16 @@ public function check(ParametersAcceptor $acceptor): array
return $traverse($type);
});

if ($parameter->getOutType() !== null) {
TypeTraverser::map($parameter->getOutType(), static function (Type $type, callable $traverse) use (&$conditionalTypes): Type {
if ($type instanceof ConditionalType || $type instanceof ConditionalTypeForParameter) {
$conditionalTypes[] = $type;
}

return $traverse($type);
});
}

$parametersByName[$parameter->getName()] = $parameter;
}

Expand Down
Expand Up @@ -63,6 +63,10 @@ public function testRule(): void
'Condition "array{foo: string} is array{foo: int}" in conditional return type is always false.',
156,
],
[
'Condition "int is int" in conditional return type is always true.',
185,
],
]);
}

Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/method-conditional-return-type.php
Expand Up @@ -175,3 +175,15 @@ public function foo(): bool
}

}

class ParamOut
{

/**
* @param-out ($i is int ? 1 : 2) $out
*/
public function doFoo(int $i, &$out) {

}

}

0 comments on commit 67f9420

Please sign in to comment.