1212use PhpParser \Node \Scalar \Float_ ;
1313use PhpParser \Node \Scalar \Int_ ;
1414use PhpParser \Node \Scalar \String_ ;
15+ use PHPStan \Reflection \ClassReflection ;
1516use PHPStan \Type \IntegerType ;
1617use PHPStan \Type \StringType ;
1718use PHPStan \Type \Type ;
1819use PHPStan \Type \TypeCombinator ;
1920use Rector \PHPUnit \CodeQuality \Reflection \MethodParametersAndReturnTypesResolver ;
21+ use Rector \PHPUnit \Enum \BehatClassName ;
2022use Rector \PHPUnit \NodeAnalyzer \TestsNodeAnalyzer ;
2123use Rector \Rector \AbstractRector ;
24+ use Rector \Reflection \ReflectionResolver ;
2225use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
2326use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
2427
@@ -30,6 +33,7 @@ final class ScalarArgumentToExpectedParamTypeRector extends AbstractRector
3033 public function __construct (
3134 private readonly TestsNodeAnalyzer $ testsNodeAnalyzer ,
3235 private readonly MethodParametersAndReturnTypesResolver $ methodParametersAndReturnTypesResolver ,
36+ private readonly ReflectionResolver $ reflectionResolver ,
3337 ) {
3438 }
3539
@@ -154,7 +158,7 @@ public function refactor(Node $node): ?Node
154158
155159 private function shouldSkipCall (StaticCall |MethodCall $ call ): bool
156160 {
157- if (! $ this ->testsNodeAnalyzer -> isInTestClass ($ call )) {
161+ if (! $ this ->isInTestClass ($ call )) {
158162 return true ;
159163 }
160164
@@ -187,4 +191,17 @@ private function hasStringOrNumberArguments(StaticCall|MethodCall $call): bool
187191
188192 return false ;
189193 }
194+
195+ private function isInTestClass (StaticCall |MethodCall $ call ): bool
196+ {
197+ $ callerClassReflection = $ this ->reflectionResolver ->resolveClassReflection ($ call );
198+ if (!$ callerClassReflection instanceof ClassReflection) {
199+ return $ this ->testsNodeAnalyzer ->isInTestClass ($ call );
200+ }
201+ if ($ callerClassReflection ->is (BehatClassName::CONTEXT )) {
202+ return true ;
203+ }
204+
205+ return $ this ->testsNodeAnalyzer ->isInTestClass ($ call );
206+ }
190207}
0 commit comments