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,20 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

use PHPUnit\Framework\TestCase;

use function PHPUnit\Framework\assertNotNull;

final class SkipImportedAssertFunction extends TestCase
{
public function testSomething(): void
{
assertNotNull(true);
}

public function testFullyQualified(): void
{
\PHPUnit\Framework\assertTrue(true);
}
}
30 changes: 30 additions & 0 deletions src/NodeAnalyzer/AssertCallAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\PHPUnit\NodeAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
Expand All @@ -21,6 +22,11 @@ final class AssertCallAnalyzer
{
private const int MAX_NESTED_METHOD_CALL_LEVEL = 5;

/**
* @see https://docs.phpunit.de/en/12.4/assertions.html
*/
private const string PHPUNIT_FUNCTION_NAMESPACE = 'PHPUnit\Framework\\';

/**
* @var string[]
*/
Expand Down Expand Up @@ -134,10 +140,34 @@ private function hasDirectAssertOrMockCall(ClassMethod $classMethod): bool
return $this->isAssertMethodCall($node);
}

// standalone function assert, e.g. "use function PHPUnit\Framework\assertNotNull;"
if ($node instanceof FuncCall) {
return $this->isAssertFuncCall($node);
}

return false;
});
}

private function isAssertFuncCall(FuncCall $funcCall): bool
{
$funcCallName = $this->nodeNameResolver->getName($funcCall);
if (! is_string($funcCallName)) {
return false;
}

if (! str_starts_with($funcCallName, self::PHPUNIT_FUNCTION_NAMESPACE)) {
return false;
}

$shortFuncCallName = substr($funcCallName, strlen(self::PHPUNIT_FUNCTION_NAMESPACE));

return array_any(
self::ASSERT_METHOD_NAME_PREFIXES,
fn (string $assertMethodNamePrefix): bool => str_starts_with($shortFuncCallName, $assertMethodNamePrefix)
);
}

private function hasNestedAssertCall(ClassMethod $classMethod): bool
{
$currentClassMethod = $classMethod;
Expand Down
Loading