88use PhpParser \Node \Expr \MethodCall ;
99use PhpParser \Node \Expr \StaticCall ;
1010use PhpParser \Node \Stmt \Class_ ;
11- use PHPStan \Reflection \ClassReflection ;
12- use PHPStan \Type \ObjectType ;
13- use Rector \Enum \ClassName ;
14- use Rector \PHPUnit \CodeQuality \Enum \NonAssertStaticableMethods ;
15- use Rector \PHPUnit \Enum \PHPUnitClassName ;
11+ use Rector \PHPUnit \CodeQuality \NodeAnalyser \AssertMethodAnalyzer ;
1612use Rector \PHPUnit \NodeAnalyzer \TestsNodeAnalyzer ;
1713use Rector \Rector \AbstractRector ;
18- use Rector \Reflection \ReflectionResolver ;
1914use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
2015use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
2116
@@ -26,7 +21,7 @@ final class PreferPHPUnitSelfCallRector extends AbstractRector
2621{
2722 public function __construct (
2823 private readonly TestsNodeAnalyzer $ testsNodeAnalyzer ,
29- private readonly ReflectionResolver $ reflectionResolver ,
24+ private readonly AssertMethodAnalyzer $ assertMethodAnalyzer ,
3025 ) {
3126 }
3227
@@ -84,44 +79,48 @@ public function refactor(Node $node): ?Node
8479 return null ;
8580 }
8681
87- $ methodName = $ this ->getName ($ node ->name );
88- if (! is_string ($ methodName )) {
89- return null ;
90- }
91-
92- if (! str_starts_with ($ methodName , 'assert ' ) && ! in_array ($ methodName , NonAssertStaticableMethods::ALL )) {
82+ if ($ node ->isFirstClassCallable ()) {
9383 return null ;
9484 }
9585
96- if (! $ this ->isName ($ node ->var , 'this ' )) {
97- return null ;
98- }
99-
100- if (! $ this ->isObjectType ($ node ->var , new ObjectType (ClassName::TEST_CASE_CLASS ))) {
86+ $ methodName = $ this ->getName ($ node ->name );
87+ // if (! is_string($methodName)) {
88+ // return null;
89+ // }
90+ //
91+ if (! $ this ->assertMethodAnalyzer ->detectTestCaseCall ($ node )) {
10192 return null ;
10293 }
10394
104- $ classReflection = $ this ->reflectionResolver ->resolveClassReflection ($ node );
105- if ($ classReflection instanceof ClassReflection && $ classReflection ->hasNativeMethod ($ methodName )) {
106- if ($ node ->isFirstClassCallable ()) {
107- return null ;
108- }
109-
110- // only handle methods in TestCase class
111- $ methodReflection = $ classReflection ->getNativeMethod ($ methodName );
112- if (! in_array (
113- $ methodReflection ->getDeclaringClass ()
114- ->getName (),
115- [PHPUnitClassName::TEST_CASE , PHPUnitClassName::ASSERT ]
116- )) {
117- return null ;
118- }
119-
120- $ hasChanged = true ;
121- return $ this ->nodeFactory ->createStaticCall ('self ' , $ methodName , $ node ->getArgs ());
122- }
123-
124- return null ;
95+ // if (! str_starts_with($methodName, 'assert') && ! in_array($methodName, NonAssertStaticableMethods::ALL)) {
96+ // return null;
97+ // }
98+ //
99+ // if (! $this->isName($node->var, 'this')) {
100+ // return null;
101+ // }
102+ //
103+ // if (! $this->isObjectType($node->var, new ObjectType(ClassName::TEST_CASE_CLASS))) {
104+ // return null;
105+ //// }
106+ //
107+ // $classReflection = $this->reflectionResolver->resolveClassReflection($node);
108+ // if ($classReflection instanceof ClassReflection && $classReflection->hasNativeMethod($methodName)) {
109+ // // only handle methods in TestCase class
110+ // $methodReflection = $classReflection->getNativeMethod($methodName);
111+ // if (! in_array(
112+ // $methodReflection->getDeclaringClass()
113+ // ->getName(),
114+ // [PHPUnitClassName::TEST_CASE, PHPUnitClassName::ASSERT]
115+ // )) {
116+ // return null;
117+ // }
118+ //
119+ //
120+ // }
121+
122+ $ hasChanged = true ;
123+ return $ this ->nodeFactory ->createStaticCall ('self ' , $ methodName , $ node ->getArgs ());
125124 });
126125
127126 if ($ hasChanged ) {
0 commit comments