forked from palantirnet/drupal-rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityDeleteMultipleRector.php
87 lines (72 loc) · 2.61 KB
/
EntityDeleteMultipleRector.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
<?php
declare(strict_types=1);
namespace DrupalRector\Drupal8\Rector\Deprecation;
use PhpParser\Node;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* Replaces deprecated entity_create() calls.
*
* See https://www.drupal.org/node/2266845 for change record.
*
* What is covered:
* - Static replacement
*
* Improvement opportunities
* - Dependency injection
* - Using class ::create() methods like Node::create().
*/
final class EntityDeleteMultipleRector extends AbstractRector
{
/**
* {@inheritdoc}
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixes deprecated entity_delete_multiple() calls', [
new CodeSample(
<<<'CODE_BEFORE'
entity_delete_multiple('node', [1, 2, 42]);
CODE_BEFORE
,
<<<'CODE_AFTER'
\Drupal::service('entity_type.manager')->getStorage('node')->delete(\Drupal::service('entity_type.manager')->getStorage('node')->loadMultiple(1, 2, 42));
CODE_AFTER
),
]);
}
/**
* {@inheritdoc}
*/
public function getNodeTypes(): array
{
return [
Node\Expr\FuncCall::class,
];
}
/**
* {@inheritdoc}
*/
public function refactor(Node $node): ?Node
{
/** @var Node\Expr\FuncCall $node */
if ($this->getName($node->name) === 'entity_delete_multiple') {
$service = new Node\Expr\StaticCall(new Node\Name\FullyQualified('Drupal'), 'service', [new Node\Arg(new Node\Scalar\String_('entity_type.manager'))]);
$getStorage_method_name = new Node\Identifier('getStorage');
$entity_type = $node->args[0];
$getStorage_node = new Node\Expr\MethodCall($service, $getStorage_method_name, [$entity_type]);
$create_method_load_multiple = new Node\Identifier('loadMultiple');
$create_method_delete = new Node\Identifier('delete');
// Set the default argument to an empty array.
$entity_values = new Node\Arg(new Node\Expr\Array_());
// If we have values for the new entity, pass them to the create method.
if (count($node->args) === 2) {
$entity_values = $node->args[1];
}
$node_load_multiple = new Node\Expr\MethodCall($getStorage_node, $create_method_load_multiple, [$entity_values]);
return new Node\Expr\MethodCall($getStorage_node, $create_method_delete, [new Node\Arg($node_load_multiple)]);
}
return null;
}
}