forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReservedObjectRector.php
121 lines (104 loc) · 3.45 KB
/
ReservedObjectRector.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
<?php
declare(strict_types=1);
namespace Rector\Php71\Rector\Name;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Namespace_;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://wiki.php.net/rfc/object-typehint
* @see https://github.com/cebe/yii2/commit/9548a212ecf6e50fcdb0e5ba6daad88019cfc544
* @see \Rector\Tests\Php71\Rector\Name\ReservedObjectRector\ReservedObjectRectorTest
*/
final class ReservedObjectRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string
*/
public const RESERVED_KEYWORDS_TO_REPLACEMENTS = 'reserved_keywords_to_replacements';
/**
* @var string[]
*/
private $reservedKeywordsToReplacements = [];
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Changes reserved "Object" name to "<Smart>Object" where <Smart> can be configured',
[
new ConfiguredCodeSample(<<<'CODE_SAMPLE'
class Object
{
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SmartObject
{
}
CODE_SAMPLE
,
[
self::RESERVED_KEYWORDS_TO_REPLACEMENTS => [
'ReservedObject' => 'SmartObject',
'Object' => 'AnotherSmartObject',
],
]
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Identifier::class, Name::class];
}
/**
* @param Identifier|Name $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof Identifier) {
return $this->processIdentifier($node);
}
return $this->processName($node);
}
public function configure(array $configuration): void
{
$this->reservedKeywordsToReplacements = $configuration[self::RESERVED_KEYWORDS_TO_REPLACEMENTS] ?? [];
}
private function processIdentifier(Identifier $identifier): Identifier
{
foreach ($this->reservedKeywordsToReplacements as $reservedKeyword => $replacement) {
if (! $this->isName($identifier, $reservedKeyword)) {
continue;
}
$identifier->name = $replacement;
return $identifier;
}
return $identifier;
}
private function processName(Name $name): Name
{
// we look for "extends <Name>"
$parentNode = $name->getAttribute(AttributeKey::PARENT_NODE);
// "Object" can part of namespace name
if ($parentNode instanceof Namespace_) {
return $name;
}
// process lass part
foreach ($this->reservedKeywordsToReplacements as $reservedKeyword => $replacement) {
if (strtolower($name->getLast()) === strtolower($reservedKeyword)) {
$name->parts[count($name->parts) - 1] = $replacement;
// invoke override
$name->setAttribute(AttributeKey::ORIGINAL_NODE, null);
}
}
return $name;
}
}