-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathGetMockRector.php
123 lines (105 loc) · 3.74 KB
/
GetMockRector.php
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
namespace DrupalRector\Drupal8\Rector\Deprecation;
use DrupalRector\Drupal8\Rector\ValueObject\GetMockConfiguration;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* Replaces deprecated getMock() calls in classes.
*
* See https://www.drupal.org/node/2907725 for change record.
*
* What is covered:
* - Checks the class being extended.
*/
class GetMockRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var ParentClassScopeResolver
*/
protected ParentClassScopeResolver $parentClassScopeResolver;
/**
* @var GetMockConfiguration[]
*/
private array $configuration;
public function __construct(ParentClassScopeResolver $parentClassScopeResolver)
{
$this->parentClassScopeResolver = $parentClassScopeResolver;
}
/**
* {@inheritdoc}
*/
public function getNodeTypes(): array
{
return [
Node\Expr\MethodCall::class,
];
}
public function configure(array $configuration): void
{
foreach ($configuration as $value) {
if (!($value instanceof GetMockConfiguration)) {
throw new \InvalidArgumentException(sprintf('Each configuration item must be an instance of "%s"', GetMockConfiguration::class));
}
}
$this->configuration = $configuration;
}
/**
* {@inheritdoc}
*/
public function refactor(Node $node): ?Node
{
assert($node instanceof Node\Expr\MethodCall);
$scope = $node->getAttribute(AttributeKey::SCOPE);
if (!$scope instanceof Scope) {
return null;
}
$parentClassName = $this->parentClassScopeResolver->resolveParentClassName($scope);
// This checks for a method call with the method name of `getMock` and that
// the variable calling `getMock` is `$this`, not some other variable call,
// such as `$myOtherService->getMock` and have unintended consequences.
if ($this->getName($node->name) !== 'getMock'
|| !($node->var instanceof Node\Expr\Variable)
|| $this->getName($node->var) !== 'this'
) {
return null;
}
foreach ($this->configuration as $configuration) {
if ($parentClassName !== $configuration->getFullyQualifiedClassName()) {
continue;
}
// Build the arguments.
$method_arguments = $node->args;
// Get the updated method name.
$method_name = new Node\Identifier('createMock');
return new Node\Expr\MethodCall($node->var, $method_name, $method_arguments);
}
return null;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixes deprecated getMock() calls', [
new ConfiguredCodeSample(
<<<'CODE_BEFORE'
$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
CODE_BEFORE
,
<<<'CODE_AFTER'
$this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
CODE_AFTER
,
[
new GetMockConfiguration('Drupal\Tests\BrowserTestBase'),
new GetMockConfiguration('Drupal\KernelTests\KernelTestBase'),
new GetMockConfiguration('Drupal\Tests\UnitTestCase'),
]
),
]);
}
}