forked from composer-unused/composer-unused
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnusedCommand.php
225 lines (190 loc) · 8.07 KB
/
UnusedCommand.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
declare(strict_types=1);
namespace Icanhazstring\Composer\Unused\Console\Command;
use Composer\Command\BaseCommand;
use Icanhazstring\Composer\Unused\Command\CollectConsumedSymbolsCommand;
use Icanhazstring\Composer\Unused\Command\FilterDependencyCollectionCommand;
use Icanhazstring\Composer\Unused\Command\Handler\CollectConsumedSymbolsCommandHandler;
use Icanhazstring\Composer\Unused\Command\Handler\CollectFilteredDependenciesCommandHandler;
use Icanhazstring\Composer\Unused\Command\Handler\CollectRequiredDependenciesCommandHandler;
use Icanhazstring\Composer\Unused\Command\LoadRequiredDependenciesCommand;
use Icanhazstring\Composer\Unused\Dependency\DependencyCollection;
use Icanhazstring\Composer\Unused\Dependency\DependencyInterface;
use Icanhazstring\Composer\Unused\Dependency\InvalidDependency;
use Icanhazstring\Composer\Unused\Dependency\RequiredDependency;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function sprintf;
use const DIRECTORY_SEPARATOR;
final class UnusedCommand extends BaseCommand
{
/** @var CollectConsumedSymbolsCommandHandler */
private $collectConsumedSymbolsCommandHandler;
/** @var CollectRequiredDependenciesCommandHandler */
private $collectRequiredDependenciesCommandHandler;
/** @var CollectFilteredDependenciesCommandHandler */
private $collectFilteredDependenciesCommandHandler;
public function __construct(
CollectConsumedSymbolsCommandHandler $collectConsumedSymbolsCommandHandler,
CollectRequiredDependenciesCommandHandler $collectRequiredDependenciesCommandHandler,
CollectFilteredDependenciesCommandHandler $collectFilteredDependenciesCommandHandler
) {
parent::__construct('unused');
$this->collectConsumedSymbolsCommandHandler = $collectConsumedSymbolsCommandHandler;
$this->collectRequiredDependenciesCommandHandler = $collectRequiredDependenciesCommandHandler;
$this->collectFilteredDependenciesCommandHandler = $collectFilteredDependenciesCommandHandler;
}
protected function configure(): void
{
$this->setDescription(
'Show unused packages by scanning and comparing package namespaces against your source.'
);
$this->addOption(
'excludeDir',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'Provide one or more folders to exclude from usage scan',
[]
);
$this->addOption(
'excludePackage',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'Provide one or more packages that should be ignored during scan',
[]
);
$this->addOption(
'ignore-exit-code',
null,
InputOption::VALUE_NONE,
'Ignore exit codes so there are no "failure" exit codes'
);
$this->addOption(
'no-progress',
null,
InputOption::VALUE_NONE,
'Show no progress bar'
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$composer = $this->getComposer();
if ($composer === null) {
// TODO IO Error
return 1;
}
$baseDir = dirname($composer->getConfig()->getConfigSource()->getName());
$rootPackage = $composer->getPackage();
$consumedSymbols = $this->collectConsumedSymbolsCommandHandler->collect(
new CollectConsumedSymbolsCommand(
$baseDir,
$rootPackage
// TODO add excludes
)
);
$unfilteredRequiredDependencyCollection = $this->collectRequiredDependenciesCommandHandler->collect(
new LoadRequiredDependenciesCommand(
$baseDir . DIRECTORY_SEPARATOR . $composer->getConfig()->get('vendor-dir'),
$rootPackage->getRequires(),
$composer->getRepositoryManager()->getLocalRepository()
)
);
$requiredDependencyCollection = $this->collectFilteredDependenciesCommandHandler->collect(
new FilterDependencyCollectionCommand(
$unfilteredRequiredDependencyCollection,
$input->getOption('excludePackage')
)
);
$io = new SymfonyStyle($input, $output);
foreach ($consumedSymbols as $symbol) {
/** @var RequiredDependency $requiredDependency */
foreach ($requiredDependencyCollection as $requiredDependency) {
if ($requiredDependency->inState($requiredDependency::STATE_USED)) {
continue;
}
if ($requiredDependency->getName() === 'php' || $requiredDependency->provides($symbol)) {
$requiredDependency->markUsed();
continue;
}
/** @var RequiredDependency $secondRequiredDependency */
foreach ($requiredDependencyCollection as $secondRequiredDependency) {
if ($requiredDependency === $secondRequiredDependency) {
continue;
}
if ($secondRequiredDependency->requires($requiredDependency)) {
// TODO add "required by" in output
$requiredDependency->markUsed();
continue 2;
}
if ($secondRequiredDependency->suggests($requiredDependency)) {
// TODO add "suggested by" in output
$requiredDependency->markUsed();
continue 2;
}
}
}
}
[$usedDependencyCollection, $unusedDependencyCollection] = $requiredDependencyCollection->partition(
static function (DependencyInterface $dependency) {
return $dependency->inState($dependency::STATE_USED);
}
);
/** @var DependencyCollection<InvalidDependency> $invalidDependencyCollection */
[$invalidDependencyCollection, $unusedDependencyCollection] = $unusedDependencyCollection->partition(
static function (DependencyInterface $dependency) {
return $dependency->inState($dependency::STATE_INVALID);
}
);
$io->section('Results');
$io->writeln(
sprintf(
'Found <fg=green>%d used</>, <fg=red>%d unused</> and <fg=yellow>%d ignored</> packages',
count($usedDependencyCollection),
count($unusedDependencyCollection),
count($invalidDependencyCollection)
)
);
$io->newLine();
$io->text('<fg=green>Used packages</>');
foreach ($usedDependencyCollection as $usedDependency) {
// TODO add required by dependency
// TODO add suggest by dependency
$io->writeln(
sprintf(
' <fg=green>%s</> %s',
"\u{2713}",
$usedDependency->getName()
)
);
}
$io->newLine();
$io->text('<fg=red>Unused packages</>');
foreach ($unusedDependencyCollection as $dependency) {
$io->writeln(
sprintf(
' <fg=red>%s</> %s',
"\u{2717}",
$dependency->getName()
)
);
}
$io->newLine();
$io->text('<fg=yellow>Ignored packages</>');
foreach ($invalidDependencyCollection as $dependency) {
$io->writeln(
sprintf(
' <fg=yellow>%s</> %s (<fg=cyan>%s</>)',
"\u{25CB}",
$dependency->getName(),
$dependency->getReason()
)
);
}
if ($unusedDependencyCollection->count() > 0) {
return 1;
}
return 0;
}
}