diff --git a/src/Composer/ProjectPackageVersionResolver.php b/src/Composer/ProjectPackageVersionResolver.php deleted file mode 100644 index 12b85a39..00000000 --- a/src/Composer/ProjectPackageVersionResolver.php +++ /dev/null @@ -1,36 +0,0 @@ -installedPackageResolver = new InstalledPackageResolver(getcwd()); - } - - public function findPackageVersion(string $packageName): ?string - { - $rootProjectInstalledPackages = $this->installedPackageResolver->resolve(); - - foreach ($rootProjectInstalledPackages as $rootProjectInstalledPackage) { - if ($rootProjectInstalledPackage->getName() === $packageName) { - return $rootProjectInstalledPackage->getVersion(); - } - } - - return null; - } -} diff --git a/src/Enum/PHPUnitClassName.php b/src/Enum/PHPUnitClassName.php index d3fa809e..a33cb9fc 100644 --- a/src/Enum/PHPUnitClassName.php +++ b/src/Enum/PHPUnitClassName.php @@ -20,4 +20,9 @@ final class PHPUnitClassName * @var string */ public const ASSERT = 'PHPUnit\Framework\Assert'; + + /** + * @var string + */ + public const INVOCATION_ORDER = 'PHPUnit\Framework\MockObject\Rule\InvocationOrder'; } diff --git a/src/NodeFactory/MatcherInvocationCountMethodCallNodeFactory.php b/src/NodeFactory/MatcherInvocationCountMethodCallNodeFactory.php index 128be1e1..66750e5d 100644 --- a/src/NodeFactory/MatcherInvocationCountMethodCallNodeFactory.php +++ b/src/NodeFactory/MatcherInvocationCountMethodCallNodeFactory.php @@ -7,13 +7,28 @@ use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; -use Rector\PHPUnit\Composer\ProjectPackageVersionResolver; +use PHPStan\Reflection\ReflectionProvider; use Rector\PHPUnit\Enum\ConsecutiveVariable; +use Rector\PHPUnit\Enum\PHPUnitClassName; +/** + * Handle silent rename "getInvocationCount()" to "numberOfInvocations()" in PHPUnit 10 + * https://github.com/sebastianbergmann/phpunit/commit/2ba8b7fded44a1a75cf5712a3b7310a8de0b6bb8#diff-3b464bb32b9187dd2d047fd1c21773aa32c19b20adebc083e1a49267c524a980 + */ final readonly class MatcherInvocationCountMethodCallNodeFactory { + /** + * @var string + */ + private const GET_INVOCATION_COUNT = 'getInvocationCount'; + + /** + * @var string + */ + private const NUMBER_OF_INVOCATIONS = 'numberOfInvocations'; + public function __construct( - private ProjectPackageVersionResolver $projectPackageVersionResolver + private ReflectionProvider $reflectionProvider, ) { } @@ -28,14 +43,19 @@ public function create(): MethodCall private function getInvocationMethodName(): string { - $projectPHPUnitVersion = $this->projectPackageVersionResolver->findPackageVersion('phpunit/phpunit'); + if (! $this->reflectionProvider->hasClass(PHPUnitClassName::INVOCATION_ORDER)) { + // fallback to PHPUnit 9 + return self::GET_INVOCATION_COUNT; + } + + $invocationOrderClassReflection = $this->reflectionProvider->getClass(PHPUnitClassName::INVOCATION_ORDER); - if ($projectPHPUnitVersion === null || version_compare($projectPHPUnitVersion, '10.0', '>=')) { - // phpunit 10 naming - return 'numberOfInvocations'; + // phpunit 10 naming + if ($invocationOrderClassReflection->hasNativeMethod(self::GET_INVOCATION_COUNT)) { + return self::GET_INVOCATION_COUNT; } // phpunit 9 naming - return 'getInvocationCount'; + return self::NUMBER_OF_INVOCATIONS; } }