77use PhpParser \Node \Expr \MethodCall ;
88use PhpParser \Node \Expr \Variable ;
99use PhpParser \Node \Identifier ;
10- use Rector \ PHPUnit \ Composer \ ProjectPackageVersionResolver ;
10+ use PHPStan \ Reflection \ ReflectionProvider ;
1111use Rector \PHPUnit \Enum \ConsecutiveVariable ;
12+ use Rector \PHPUnit \Enum \PHPUnitClassName ;
1213
14+ /**
15+ * Handle silent rename "getInvocationCount()" to "numberOfInvocations()" in PHPUnit 10
16+ * https://github.com/sebastianbergmann/phpunit/commit/2ba8b7fded44a1a75cf5712a3b7310a8de0b6bb8#diff-3b464bb32b9187dd2d047fd1c21773aa32c19b20adebc083e1a49267c524a980
17+ */
1318final readonly class MatcherInvocationCountMethodCallNodeFactory
1419{
20+ /**
21+ * @var string
22+ */
23+ private const GET_INVOCATION_COUNT = 'getInvocationCount ' ;
24+
25+ /**
26+ * @var string
27+ */
28+ private const NUMBER_OF_INVOCATIONS = 'numberOfInvocations ' ;
29+
1530 public function __construct (
16- private ProjectPackageVersionResolver $ projectPackageVersionResolver
31+ private ReflectionProvider $ reflectionProvider ,
1732 ) {
1833 }
1934
@@ -28,14 +43,19 @@ public function create(): MethodCall
2843
2944 private function getInvocationMethodName (): string
3045 {
31- $ projectPHPUnitVersion = $ this ->projectPackageVersionResolver ->findPackageVersion ('phpunit/phpunit ' );
46+ if (! $ this ->reflectionProvider ->hasClass (PHPUnitClassName::INVOCATION_ORDER )) {
47+ // fallback to PHPUnit 9
48+ return self ::GET_INVOCATION_COUNT ;
49+ }
50+
51+ $ invocationOrderClassReflection = $ this ->reflectionProvider ->getClass (PHPUnitClassName::INVOCATION_ORDER );
3252
33- if ( $ projectPHPUnitVersion === null || version_compare ( $ projectPHPUnitVersion , ' 10.0 ' , ' >= ' )) {
34- // phpunit 10 naming
35- return ' numberOfInvocations ' ;
53+ // phpunit 10 naming
54+ if ( $ invocationOrderClassReflection -> hasNativeMethod ( self :: GET_INVOCATION_COUNT )) {
55+ return self :: GET_INVOCATION_COUNT ;
3656 }
3757
3858 // phpunit 9 naming
39- return ' getInvocationCount ' ;
59+ return self :: NUMBER_OF_INVOCATIONS ;
4060 }
4161}
0 commit comments