|
9 | 9 | use PhpParser\Node\Expr\StaticCall; |
10 | 10 | use PhpParser\Node\Scalar\Int_; |
11 | 11 | use PhpParser\Node\Scalar\String_; |
| 12 | +use PHPStan\Type\Type; |
12 | 13 | use PHPStan\Type\TypeCombinator; |
| 14 | +use Rector\PHPUnit\CodeQuality\Reflection\MethodParametersAndReturnTypesResolver; |
13 | 15 | use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
14 | 16 | use Rector\Rector\AbstractRector; |
15 | 17 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
|
21 | 23 | final class MatchAssertSameExpectedTypeRector extends AbstractRector |
22 | 24 | { |
23 | 25 | public function __construct( |
24 | | - private readonly TestsNodeAnalyzer $testsNodeAnalyzer |
| 26 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 27 | + private readonly MethodParametersAndReturnTypesResolver $methodParametersAndReturnTypesResolver |
25 | 28 | ) { |
26 | 29 | } |
27 | 30 |
|
@@ -103,7 +106,16 @@ public function refactor(Node $node): ?Node |
103 | 106 |
|
104 | 107 | $variableExpr = $node->getArgs()[1] |
105 | 108 | ->value; |
106 | | - $variableType = $this->nodeTypeResolver->getNativeType($variableExpr); |
| 109 | + |
| 110 | + if ($variableExpr instanceof MethodCall || $variableExpr instanceof StaticCall) { |
| 111 | + // an earlier assert can leave the scope type narrowed, even after the caller is mutated |
| 112 | + $variableType = $this->methodParametersAndReturnTypesResolver->resolveCallReturnType($variableExpr); |
| 113 | + if (! $variableType instanceof Type) { |
| 114 | + return null; |
| 115 | + } |
| 116 | + } else { |
| 117 | + $variableType = $this->nodeTypeResolver->getNativeType($variableExpr); |
| 118 | + } |
107 | 119 |
|
108 | 120 | $directVariableType = TypeCombinator::removeNull($variableType); |
109 | 121 |
|
|
0 commit comments