-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
TolerantUpdater.php
172 lines (141 loc) · 6.67 KB
/
TolerantUpdater.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace Phpactor\CodeBuilder\Adapter\TolerantParser;
use Microsoft\PhpParser\Node\Expression\ObjectCreationExpression;
use Microsoft\PhpParser\Node\SourceFileNode;
use Microsoft\PhpParser\Node\Statement\ClassDeclaration;
use Microsoft\PhpParser\Node\Statement\TraitDeclaration;
use Microsoft\PhpParser\Node\Statement\EnumDeclaration;
use Microsoft\PhpParser\Node\Statement\InlineHtml;
use Microsoft\PhpParser\Node\Statement\NamespaceDefinition;
use Microsoft\PhpParser\Parser;
use Phpactor\CodeBuilder\Adapter\TolerantParser\Updater\EnumUpdater;
use Phpactor\CodeBuilder\Adapter\TolerantParser\Updater\UseStatementUpdater;
use Phpactor\CodeBuilder\Domain\Code;
use Phpactor\CodeBuilder\Domain\Prototype\NamespaceName;
use Phpactor\CodeBuilder\Domain\Prototype\Prototype;
use Phpactor\CodeBuilder\Domain\Prototype\SourceCode;
use Phpactor\CodeBuilder\Domain\Renderer;
use Phpactor\CodeBuilder\Domain\Updater;
use Phpactor\CodeBuilder\Util\TextFormat;
use Phpactor\CodeBuilder\Adapter\TolerantParser\Updater\ClassUpdater;
use Microsoft\PhpParser\Node\Statement\InterfaceDeclaration;
use Phpactor\CodeBuilder\Adapter\TolerantParser\Updater\InterfaceUpdater;
use Phpactor\CodeBuilder\Adapter\TolerantParser\Updater\TraitUpdater;
use Phpactor\TextDocument\TextEdits;
use Phpactor\WorseReflection\Core\Util\NodeUtil;
use function Amp\Promise\wait;
use Microsoft\PhpParser\Token;
class TolerantUpdater implements Updater
{
private Parser $parser;
private TextFormat $textFormat;
private ClassUpdater $classUpdater;
private InterfaceUpdater $interfaceUpdater;
private TraitUpdater $traitUpdater;
private EnumUpdater $enumUpdater;
private UseStatementUpdater $useStatementUpdater;
public function __construct(
private Renderer $renderer,
TextFormat $textFormat = null,
Parser $parser = null
) {
$this->parser = $parser ?: new Parser();
$this->textFormat = $textFormat ?: new TextFormat();
$this->classUpdater = new ClassUpdater($renderer);
$this->interfaceUpdater = new InterfaceUpdater($renderer);
$this->traitUpdater = new TraitUpdater($renderer);
$this->enumUpdater = new EnumUpdater($renderer);
$this->useStatementUpdater = new UseStatementUpdater();
}
public function textEditsFor(Prototype $prototype, Code $code): TextEdits
{
$edits = new Edits($this->textFormat);
$node = $this->parser->parseSourceFile((string) $code);
$this->updateNamespace($edits, $prototype, $node);
$this->useStatementUpdater->updateUseStatements($edits, $prototype, $node);
$this->updateClasses($edits, $prototype, $node);
return $edits->textEdits();
}
private function updateNamespace(Edits $edits, SourceCode $prototype, SourceFileNode $node): void
{
$namespaceNode = $node->getFirstChildNode(NamespaceDefinition::class);
if (null !== $namespaceNode && NamespaceName::root() == $prototype->namespace()) {
return;
}
/** @var $namespaceNode NamespaceDefinition */
if ($namespaceNode && $namespaceNode->name->getText() == (string) $prototype->namespace()) {
return;
}
if (((string) $prototype->namespace()) === '') {
return;
}
if ($namespaceNode) {
$edits->replace($namespaceNode, 'namespace ' . (string) $prototype->namespace() . ';');
return;
}
$startTag = $node->getFirstChildNode(InlineHtml::class);
$edits->after($startTag, 'namespace ' . (string) $prototype->namespace() . ';' . "\n"."\n");
}
private function updateClasses(Edits $edits, SourceCode $prototype, SourceFileNode $node): void
{
$classNodes = [];
$traitNodes = [];
$interfaceNodes = [];
$enumNodes = [];
$lastStatement = null;
foreach ($node->statementList as $classNode) {
$lastStatement = $classNode;
if ($classNode instanceof ClassDeclaration) {
$name = $classNode->name->getText($node->getFileContents());
$classNodes[$name] = $classNode;
}
if ($classNode instanceof InterfaceDeclaration) {
$name = $classNode->name->getText($node->getFileContents());
$interfaceNodes[$name] = $classNode;
}
if ($classNode instanceof TraitDeclaration) {
$name = $classNode->name->getText($node->getFileContents());
$traitNodes[$name] = $classNode;
}
if ($classNode instanceof EnumDeclaration) {
$name = $classNode->name->getText($node->getFileContents());
$enumNodes[$name] = $classNode;
}
}
foreach($node->getDescendantNodes() as $classNode) {
if ($classNode instanceof ObjectCreationExpression && $classNode->classTypeDesignator instanceof Token) {
$name = NodeUtil::nameFromTokenOrNode($classNode, $classNode);
$classNodes[$name] = $classNode;
}
}
foreach ($prototype->classes()->in(array_keys($classNodes)) as $classPrototype) {
$this->classUpdater->updateClass($edits, $classPrototype, $classNodes[$classPrototype->name()]);
}
foreach ($prototype->interfaces()->in(array_keys($interfaceNodes)) as $classPrototype) {
$this->interfaceUpdater->updateInterface($edits, $classPrototype, $interfaceNodes[$classPrototype->name()]);
}
foreach ($prototype->traits()->in(array_keys($traitNodes)) as $traitPrototype) {
$this->traitUpdater->updateTrait($edits, $traitPrototype, $traitNodes[$traitPrototype->name()]);
}
foreach ($prototype->enums()->in(array_keys($enumNodes)) as $enumPrototype) {
$this->enumUpdater->updateEnum($edits, $enumPrototype, $enumNodes[$enumPrototype->name()]);
}
$classes = array_merge(
iterator_to_array($prototype->classes()->notIn(array_keys($classNodes))),
iterator_to_array($prototype->interfaces()->notIn(array_keys($interfaceNodes))),
iterator_to_array($prototype->traits()->notIn(array_keys($traitNodes))),
iterator_to_array($prototype->enums()->notIn(array_keys($enumNodes)))
);
$index = 0;
foreach ($classes as $classPrototype) {
if (substr($lastStatement->getText(), -1) !== "\n") {
$edits->after($lastStatement, "\n");
}
if ($index > 0 && $index + 1 == count($classes)) {
$edits->after($lastStatement, "\n");
}
$edits->after($lastStatement, "\n" . $this->renderer->render($classPrototype));
$index++;
}
}
}