-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathAssertMethodAnalyzer.php
More file actions
50 lines (40 loc) · 1.6 KB
/
AssertMethodAnalyzer.php
File metadata and controls
50 lines (40 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\CodeQuality\NodeAnalyser;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Reflection\ClassReflection;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PHPUnit\CodeQuality\Enum\NonAssertStaticableMethods;
use Rector\PHPUnit\Enum\PHPUnitClassName;
use Rector\Reflection\ReflectionResolver;
final readonly class AssertMethodAnalyzer
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
private ReflectionResolver $reflectionResolver,
) {
}
public function detectTestCaseCall(MethodCall|StaticCall $call): bool
{
$methodName = $this->nodeNameResolver->getName($call->name);
if (! str_starts_with((string) $methodName, 'assert') && ! in_array(
$methodName,
NonAssertStaticableMethods::ALL
)) {
return false;
}
$classReflection = $this->reflectionResolver->resolveClassReflection($call);
if (! $classReflection instanceof ClassReflection) {
return false;
}
if ($call instanceof StaticCall && ! $this->nodeNameResolver->isNames($call->class, ['static', 'self'])) {
return false;
}
$extendedMethodReflection = $classReflection->getNativeMethod($methodName);
// only handle methods in TestCase or Assert class classes
$declaringClassName = $extendedMethodReflection->getDeclaringClass()
->getName();
return in_array($declaringClassName, [PHPUnitClassName::TEST_CASE, PHPUnitClassName::ASSERT]);
}
}