-
Notifications
You must be signed in to change notification settings - Fork 74
/
EntityCreateRector.php
86 lines (71 loc) · 2.39 KB
/
EntityCreateRector.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
<?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 EntityCreateRector extends AbstractRector
{
/**
* {@inheritdoc}
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixes deprecated entity_create() calls', [
new CodeSample(
<<<'CODE_BEFORE'
entity_create('node', ['bundle' => 'page', 'title' => 'Hello world']);
CODE_BEFORE
,
<<<'CODE_AFTER'
\Drupal::service('entity_type.manager)->getStorage('node')->create(['bundle' => 'page', 'title' => 'Hello world']);
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_create') {
$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_name = new Node\Identifier('create');
// 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];
}
$new_node = new Node\Expr\MethodCall($getStorage_node, $create_method_name, [$entity_values]);
return $new_node;
}
return null;
}
}