77use PhpParser \Node \Expr \MethodCall ;
88use PhpParser \Node \Expr \Variable ;
99use PhpParser \Node \Identifier ;
10+ use PHPStan \Reflection \ReflectionProvider ;
1011use Rector \PHPUnit \Composer \ProjectPackageVersionResolver ;
1112use Rector \PHPUnit \Enum \ConsecutiveVariable ;
13+ use Rector \PHPUnit \Enum \PHPUnitClassName ;
1214
1315final readonly class MatcherInvocationCountMethodCallNodeFactory
1416{
17+ /**
18+ * @var string
19+ */
20+ private const GET_INVOCATION_COUNT = 'getInvocationCount ' ;
21+
22+ /**
23+ * @var string
24+ */
25+ private const NUMBER_OF_INVOCATIONS = 'numberOfInvocations ' ;
26+
1527 public function __construct (
16- private ProjectPackageVersionResolver $ projectPackageVersionResolver
28+ private ReflectionProvider $ reflectionProvider ,
1729 ) {
1830 }
1931
@@ -28,14 +40,19 @@ public function create(): MethodCall
2840
2941 private function getInvocationMethodName (): string
3042 {
31- $ projectPHPUnitVersion = $ this ->projectPackageVersionResolver ->findPackageVersion ('phpunit/phpunit ' );
43+ if (! $ this ->reflectionProvider ->hasClass (PHPUnitClassName::INVOCATION_ORDER )) {
44+ // fallback to PHPUnit 9
45+ return self ::GET_INVOCATION_COUNT ;
46+ }
47+
48+ $ invocationOrderClassReflection = $ this ->reflectionProvider ->getClass (PHPUnitClassName::INVOCATION_ORDER );
3249
33- if ( $ projectPHPUnitVersion === null || version_compare ( $ projectPHPUnitVersion , ' 10.0 ' , ' >= ' )) {
34- // phpunit 10 naming
35- return ' numberOfInvocations ' ;
50+ // phpunit 10 naming
51+ if ( $ invocationOrderClassReflection -> hasNativeMethod ( self :: GET_INVOCATION_COUNT )) {
52+ return self :: GET_INVOCATION_COUNT ;
3653 }
3754
3855 // phpunit 9 naming
39- return ' getInvocationCount ' ;
56+ return self :: NUMBER_OF_INVOCATIONS ;
4057 }
4158}
0 commit comments