Skip to content

Commit 5d1c0bb

Browse files
authored
[fix] skip AddInstanceofAssertForNullableArgumentRector on assert call (#602)
1 parent fe23a4d commit 5d1c0bb

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector\Fixture;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector\Source\SomeClassUsedInTests;
9+
10+
final class SkipAssertPass extends TestCase
11+
{
12+
public function test(): void
13+
{
14+
$someObject = $this->getSomeObject();
15+
$this->assertInstanceOf(\stdClass::class, $someObject);
16+
}
17+
18+
private function getSomeObject(): ?SomeClassUsedInTests
19+
{
20+
if (mt_rand(0, 1)) {
21+
return new SomeClassUsedInTests();
22+
}
23+
24+
return null;
25+
}
26+
}

rules/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableArgumentRector.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Rector\PHPUnit\CodeQuality\TypeAnalyzer\SimpleTypeAnalyzer;
1919
use Rector\PHPUnit\CodeQuality\ValueObject\VariableNameToType;
2020
use Rector\PHPUnit\CodeQuality\ValueObject\VariableNameToTypeCollection;
21+
use Rector\PHPUnit\NodeAnalyzer\AssertCallAnalyzer;
2122
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
2223
use Rector\Rector\AbstractRector;
2324
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -33,6 +34,7 @@ public function __construct(
3334
private readonly NullableObjectAssignCollector $nullableObjectAssignCollector,
3435
private readonly AssertMethodCallFactory $assertMethodCallFactory,
3536
private readonly MethodCallParameterTypeResolver $methodCallParameterTypeResolver,
37+
private readonly AssertCallAnalyzer $assertCallAnalyzer,
3638
) {
3739
}
3840

@@ -180,6 +182,11 @@ private function matchedNullableArgumentNameToType(
180182
return null;
181183
}
182184

185+
// avoid double null on assert
186+
if ($this->assertCallAnalyzer->isAssertMethodCall($node)) {
187+
return null;
188+
}
189+
183190
$classMethodParameterTypes = $this->methodCallParameterTypeResolver->resolve($node);
184191

185192
foreach ($node->getArgs() as $position => $arg) {

src/NodeAnalyzer/AssertCallAnalyzer.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,26 @@ public function containsAssertCall(ClassMethod $classMethod): bool
8989
return $hasNestedAssertOrMockCall;
9090
}
9191

92+
public function isAssertMethodCall(MethodCall|StaticCall $call): bool
93+
{
94+
if (! $call->name instanceof Identifier) {
95+
return false;
96+
}
97+
98+
$callName = $this->nodeNameResolver->getName($call->name);
99+
if (! is_string($callName)) {
100+
return false;
101+
}
102+
103+
foreach (self::ASSERT_METHOD_NAME_PREFIXES as $assertMethodNamePrefix) {
104+
if (str_starts_with($callName, $assertMethodNamePrefix)) {
105+
return true;
106+
}
107+
}
108+
109+
return false;
110+
}
111+
92112
private function hasDirectAssertOrMockCall(ClassMethod $classMethod): bool
93113
{
94114
return (bool) $this->betterNodeFinder->findFirst((array) $classMethod->stmts, function (Node $node): bool {
@@ -108,11 +128,11 @@ private function hasDirectAssertOrMockCall(ClassMethod $classMethod): bool
108128
return true;
109129
}
110130

111-
return $this->isAssertMethodName($node);
131+
return $this->isAssertMethodCall($node);
112132
}
113133

114134
if ($node instanceof StaticCall) {
115-
return $this->isAssertMethodName($node);
135+
return $this->isAssertMethodCall($node);
116136
}
117137

118138
return false;
@@ -171,24 +191,4 @@ private function resolveClassMethodFromCall(StaticCall | MethodCall $call): ?Cla
171191

172192
return $this->astResolver->resolveClassMethod($objectType->getClassName(), $methodName);
173193
}
174-
175-
private function isAssertMethodName(MethodCall|StaticCall $call): bool
176-
{
177-
if (! $call->name instanceof Identifier) {
178-
return false;
179-
}
180-
181-
$callName = $this->nodeNameResolver->getName($call->name);
182-
if (! is_string($callName)) {
183-
return false;
184-
}
185-
186-
foreach (self::ASSERT_METHOD_NAME_PREFIXES as $assertMethodNamePrefix) {
187-
if (str_starts_with($callName, $assertMethodNamePrefix)) {
188-
return true;
189-
}
190-
}
191-
192-
return false;
193-
}
194194
}

0 commit comments

Comments
 (0)