-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMigrationReport.php
More file actions
152 lines (123 loc) · 4.24 KB
/
MigrationReport.php
File metadata and controls
152 lines (123 loc) · 4.24 KB
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
<?php
declare(strict_types=1);
namespace KaririCode\Devkit\ValueObject;
/**
* Immutable snapshot of redundant items detected in a project
* that the devkit replaces.
*
* @since 1.0.0
*/
final readonly class MigrationReport
{
public bool $hasRedundancies;
public int $totalItems;
/**
* @param array<string, string> $redundantPackages Package name → version constraint
* @param list<string> $redundantConfigFiles Filenames relative to project root
* @param list<string> $redundantCachePaths Cache paths relative to project root
*/
public function __construct(
public string $projectRoot,
public array $redundantPackages,
public array $redundantConfigFiles,
public array $redundantCachePaths,
) {
$this->totalItems = \count($redundantPackages)
+ \count($redundantConfigFiles)
+ \count($redundantCachePaths);
$this->hasRedundancies = $this->totalItems > 0;
}
public function hasPackages(): bool
{
return [] !== $this->redundantPackages;
}
public function hasConfigFiles(): bool
{
return [] !== $this->redundantConfigFiles;
}
public function hasCachePaths(): bool
{
return [] !== $this->redundantCachePaths;
}
/** Remove redundant config files and cache paths from disk. */
public function removeFiles(): int
{
$removed = 0;
foreach ([...$this->redundantConfigFiles, ...$this->redundantCachePaths] as $relative) {
$fullPath = $this->projectRoot . \DIRECTORY_SEPARATOR . $relative;
if (is_dir($fullPath)) {
$this->removeRecursive($fullPath);
++$removed;
} elseif (is_file($fullPath)) {
unlink($fullPath);
++$removed;
}
}
return $removed;
}
/**
* Remove redundant packages from composer.json require-dev.
*
* Rewrites composer.json in place preserving JSON formatting.
*
* @return list<string> Package names actually removed.
*/
public function removePackagesFromComposer(): array
{
$composerPath = $this->projectRoot . \DIRECTORY_SEPARATOR . 'composer.json';
if (! is_file($composerPath)) {
return [];
}
$raw = file_get_contents($composerPath);
if (false === $raw) {
return [];
}
/** @var array<string, mixed> $composer */
$composer = json_decode($raw, true, 512, \JSON_THROW_ON_ERROR);
$removed = [];
/** @var array<string, string> $requireDev */
$requireDev = \is_array($composer['require-dev'] ?? null) ? $composer['require-dev'] : [];
foreach (array_keys($this->redundantPackages) as $package) {
if (isset($requireDev[$package])) {
unset($requireDev[$package]);
$removed[] = $package;
}
}
if ([] === $removed) {
return [];
}
// Write back the updated require-dev (or remove the key if empty)
if ([] === $requireDev) {
unset($composer['require-dev']);
} else {
$composer['require-dev'] = $requireDev;
}
// Detect indentation: 4-space (default) or tab
$jsonFlags = \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE;
$encoded = json_encode($composer, $jsonFlags);
if (false === $encoded) {
return [];
}
// Re-apply tab indentation if original used tabs
if (str_contains($raw, "\t")) {
$encoded = str_replace(' ', "\t", $encoded);
}
file_put_contents(
$composerPath,
$encoded . \PHP_EOL,
);
return $removed;
}
private function removeRecursive(string $dir): void
{
$items = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST,
);
foreach ($items as $item) {
/** @var \SplFileInfo $item */
$item->isDir() ? rmdir($item->getPathname()) : unlink($item->getPathname());
}
rmdir($dir);
}
}