Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector\Source\SomeClassUsedInTests;

final class SkipAssertPass extends TestCase
{
public function test(): void
{
$someObject = $this->getSomeObject();
$this->assertInstanceOf(\stdClass::class, $someObject);
}

private function getSomeObject(): ?SomeClassUsedInTests
{
if (mt_rand(0, 1)) {
return new SomeClassUsedInTests();
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Rector\PHPUnit\CodeQuality\TypeAnalyzer\SimpleTypeAnalyzer;
use Rector\PHPUnit\CodeQuality\ValueObject\VariableNameToType;
use Rector\PHPUnit\CodeQuality\ValueObject\VariableNameToTypeCollection;
use Rector\PHPUnit\NodeAnalyzer\AssertCallAnalyzer;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -33,6 +34,7 @@ public function __construct(
private readonly NullableObjectAssignCollector $nullableObjectAssignCollector,
private readonly AssertMethodCallFactory $assertMethodCallFactory,
private readonly MethodCallParameterTypeResolver $methodCallParameterTypeResolver,
private readonly AssertCallAnalyzer $assertCallAnalyzer,
) {
}

Expand Down Expand Up @@ -180,6 +182,11 @@ private function matchedNullableArgumentNameToType(
return null;
}

// avoid double null on assert
if ($this->assertCallAnalyzer->isAssertMethodCall($node)) {
return null;
}

$classMethodParameterTypes = $this->methodCallParameterTypeResolver->resolve($node);

foreach ($node->getArgs() as $position => $arg) {
Expand Down
44 changes: 22 additions & 22 deletions src/NodeAnalyzer/AssertCallAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ public function containsAssertCall(ClassMethod $classMethod): bool
return $hasNestedAssertOrMockCall;
}

public function isAssertMethodCall(MethodCall|StaticCall $call): bool
{
if (! $call->name instanceof Identifier) {
return false;
}

$callName = $this->nodeNameResolver->getName($call->name);
if (! is_string($callName)) {
return false;
}

foreach (self::ASSERT_METHOD_NAME_PREFIXES as $assertMethodNamePrefix) {
if (str_starts_with($callName, $assertMethodNamePrefix)) {
return true;
}
}

return false;
}

private function hasDirectAssertOrMockCall(ClassMethod $classMethod): bool
{
return (bool) $this->betterNodeFinder->findFirst((array) $classMethod->stmts, function (Node $node): bool {
Expand All @@ -108,11 +128,11 @@ private function hasDirectAssertOrMockCall(ClassMethod $classMethod): bool
return true;
}

return $this->isAssertMethodName($node);
return $this->isAssertMethodCall($node);
}

if ($node instanceof StaticCall) {
return $this->isAssertMethodName($node);
return $this->isAssertMethodCall($node);
}

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

return $this->astResolver->resolveClassMethod($objectType->getClassName(), $methodName);
}

private function isAssertMethodName(MethodCall|StaticCall $call): bool
{
if (! $call->name instanceof Identifier) {
return false;
}

$callName = $this->nodeNameResolver->getName($call->name);
if (! is_string($callName)) {
return false;
}

foreach (self::ASSERT_METHOD_NAME_PREFIXES as $assertMethodNamePrefix) {
if (str_starts_with($callName, $assertMethodNamePrefix)) {
return true;
}
}

return false;
}
}