-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathMatcherInvocationCountMethodCallNodeFactory.php
More file actions
61 lines (48 loc) · 1.79 KB
/
MatcherInvocationCountMethodCallNodeFactory.php
File metadata and controls
61 lines (48 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\NodeFactory;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
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 ReflectionProvider $reflectionProvider,
) {
}
public function create(): MethodCall
{
$invocationMethodName = $this->getInvocationMethodName();
$matcherVariable = new Variable(ConsecutiveVariable::MATCHER);
return new MethodCall($matcherVariable, new Identifier($invocationMethodName));
}
private function getInvocationMethodName(): string
{
if (! $this->reflectionProvider->hasClass(PHPUnitClassName::INVOCATION_ORDER)) {
// fallback to PHPUnit 9
return self::GET_INVOCATION_COUNT;
}
$invocationOrderClassReflection = $this->reflectionProvider->getClass(PHPUnitClassName::INVOCATION_ORDER);
// phpunit 10 naming
if ($invocationOrderClassReflection->hasNativeMethod(self::GET_INVOCATION_COUNT)) {
return self::GET_INVOCATION_COUNT;
}
// phpunit 9 naming
return self::NUMBER_OF_INVOCATIONS;
}
}