1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Tools\Installer\Operation; |
4
|
|
|
|
5
|
|
|
use ApiClients\Tools\Installer\Filesystem; |
6
|
|
|
use ApiClients\Tools\Installer\OperationInterface; |
7
|
|
|
use BetterReflection\Reflector\ClassReflector; |
8
|
|
|
use BetterReflection\SourceLocator\Type\StringSourceLocator; |
9
|
|
|
use Composer\Factory; |
10
|
|
|
use PhpParser\Node; |
11
|
|
|
use PhpParser\ParserFactory; |
12
|
|
|
use PhpParser\PrettyPrinter\Standard; |
13
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
14
|
|
|
|
15
|
|
|
final class UpdateNamespaces implements OperationInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Filesystem |
19
|
|
|
*/ |
20
|
|
|
private $filesystem; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @internal |
24
|
|
|
* @param Filesystem $filesystem |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Filesystem $filesystem) |
27
|
|
|
{ |
28
|
|
|
$this->filesystem = $filesystem; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function create(): OperationInterface |
32
|
|
|
{ |
33
|
|
|
return new self(new Filesystem()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function operate(array $replacements, array $environment, SymfonyStyle $style) |
37
|
|
|
{ |
38
|
|
|
$classes = []; |
39
|
|
|
foreach ([ |
40
|
|
|
'path_src' => ['ns_vendor', 'current_ns'], |
41
|
|
|
'path_tests' => ['ns_tests_vendor', 'current_ns_tests'], |
42
|
|
|
] as $dirIndex => list($namespaceIndex, $currentNamespace)) { |
43
|
|
|
$oldNamespace = $environment[$currentNamespace] ?? ''; |
44
|
|
|
$newNamespace = $replacements[$namespaceIndex] . '\\' . $replacements['ns_project']; |
45
|
|
|
$path = str_replace( |
46
|
|
|
'composer.json', |
47
|
|
|
$replacements[$dirIndex], |
48
|
|
|
Factory::getComposerFile() |
49
|
|
|
); |
50
|
|
|
$path = rtrim($path, '/'); |
51
|
|
|
$path .= '/'; |
52
|
|
|
foreach ($this->filesystem->ls($path) as $fileName) { |
53
|
|
|
$fileContents = $this->filesystem->read($fileName); |
54
|
|
|
$reflector = new ClassReflector(new StringSourceLocator($fileContents)); |
55
|
|
|
foreach ($reflector->getAllClasses() as $class) { |
56
|
|
|
$className = $class->getName(); |
57
|
|
|
$classes[$className] = str_replace($oldNamespace, $newNamespace, $className); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
foreach ([ |
63
|
|
|
'path_src' => ['ns_vendor', 'current_ns'], |
64
|
|
|
'path_tests' => ['ns_tests_vendor', 'current_ns_tests'], |
65
|
|
|
] as $dirIndex => list($namespaceIndex, $currentNamespace)) { |
66
|
|
|
$oldNamespace = $environment[$currentNamespace] ?? ''; |
67
|
|
|
$newNamespace = $replacements[$namespaceIndex] . '\\' . $replacements['ns_project']; |
68
|
|
|
$path = str_replace( |
69
|
|
|
'composer.json', |
70
|
|
|
$replacements[$dirIndex], |
71
|
|
|
Factory::getComposerFile() |
72
|
|
|
); |
73
|
|
|
$path = rtrim($path, '/'); |
74
|
|
|
$path .= '/'; |
75
|
|
|
foreach ($this->filesystem->ls($path) as $fileName) { |
76
|
|
|
$style->text(' * Updating ' . $fileName); |
77
|
|
|
$stmts = $this->parseFile($fileName); |
78
|
|
|
$stmts = $this->updateNamespaces($stmts, $newNamespace, $oldNamespace, $classes); |
79
|
|
|
$this->filesystem->write($fileName, (new Standard())->prettyPrintFile($stmts) . PHP_EOL); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$style->success('Namespaces updated'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function updateNamespaces(array $stmts, string $namespace, string $currentNamespace, $classes) |
87
|
|
|
{ |
88
|
|
|
if ($stmts === null) { |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
foreach ($stmts as $index => $node) { |
93
|
|
|
if (!($node instanceof Node\Stmt\Namespace_)) { |
94
|
|
|
continue; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$suffix = str_replace($currentNamespace, '', (string)$node->name); |
98
|
|
|
|
99
|
|
|
$stmts[$index] = new Node\Stmt\Namespace_( |
100
|
|
|
new Node\Name( |
101
|
|
|
$namespace . $suffix |
102
|
|
|
), |
103
|
|
|
$this->updateUses($node->stmts, $classes), |
|
|
|
|
104
|
|
|
$node->getAttributes() |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
break; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $stmts; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function updateUses(array $stmts, array $classes) |
114
|
|
|
{ |
115
|
|
|
if ($stmts === null) { |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
foreach ($stmts as $index => $node) { |
120
|
|
|
if (!($node instanceof Node\Stmt\Use_)) { |
121
|
|
|
continue; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($node->type !== Node\Stmt\Use_::TYPE_NORMAL) { |
125
|
|
|
continue; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
foreach ($node->uses as $useIndex => $useNode) { |
129
|
|
|
if (!($useNode instanceof Node\Stmt\UseUse)) { |
130
|
|
|
continue; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (!isset($classes[(string)$useNode->name])) { |
134
|
|
|
continue; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$stmts[$index]->uses[$useIndex]->name = new Node\Name( |
138
|
|
|
$classes[(string)$useNode->name] |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $stmts; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
private function parseFile(string $fileName) |
147
|
|
|
{ |
148
|
|
|
return (new ParserFactory())->create(ParserFactory::ONLY_PHP7)->parse($this->filesystem->read($fileName)); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: