-
Notifications
You must be signed in to change notification settings - Fork 3
/
GenerateExcludes.php
187 lines (152 loc) · 5.64 KB
/
GenerateExcludes.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
<?php
declare(strict_types=1);
namespace Snicco\PhpScoperExcludes\Console;
use PhpParser\ParserFactory;
use PhpParser\Lexer\Emulative;
use Snicco\PhpScoperExcludes\Option;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Snicco\PhpScoperExcludes\ExclusionListGenerator;
use Symfony\Component\Console\Output\OutputInterface;
use function count;
use function getcwd;
use function is_file;
use function gettype;
use function sprintf;
use function is_array;
use function basename;
use function is_string;
use function array_map;
use function filter_var;
use function is_iterable;
use function array_merge;
use function array_values;
use function iterator_to_array;
use const FILTER_VALIDATE_BOOLEAN;
final class GenerateExcludes extends Command
{
protected static $defaultName = 'run';
private string $repository_root;
public function __construct(string $repository_root)
{
$this->repository_root = $repository_root;
parent::__construct();
$this->addOption(
'json',
null,
InputOption::VALUE_NONE,
'Dump the excludes as json.',
);
$this->addOption(
'exclude-empty',
null,
InputOption::VALUE_NONE,
'Require at least one type of(interface,class,trait,constant,function) to be present in order for the file being dumped.',
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$io->title("Generating exclusion lists.");
$config = $this->repository_root.'/generate-excludes.inc.php';
if ( ! is_file($config)) {
$io->error([
"Configuration file not found at path [$config].",
'Please run "vendor/bin/generate-excludes generate-config"',
]);
return Command::FAILURE;
}
$config = require_once $config;
$files = $config[Option::FILES];
if ( ! is_array($files)) {
$io->error([
'Option::Files has to be an array of string|iterable.',
'Got:'.gettype($files),
]);
return Command::FAILURE;
}
$io->note("Normalizing files...");
$files = $this->normalizeFiles($files);
$count = count($files);
if ( ! $count) {
$io->note('No files found. Aborting...');
return Command::SUCCESS;
}
$io->info(
sprintf(
'%s %s found. Starting to generate excludes...',
$count,
$count > 1 ? 'files' : 'file'
)
);
$generator = $this->newGenerator(
$config,
$output_dir = $config[Option::OUTPUT_DIR] ?? getcwd()
);
$progress_bar = $this->newProgressBar($output, $count);
$progress_bar->setMessage(basename($files[0]));
$progress_bar->start();
$json = filter_var($input->getOption('json'), FILTER_VALIDATE_BOOLEAN);
$exclude_empty = filter_var($input->getOption('exclude-empty'), FILTER_VALIDATE_BOOLEAN);
foreach ($files as $file) {
$progress_bar->setMessage(basename($file));
if ($json) {
$generator->dumpAsJson($file, !$exclude_empty);
}
else {
$generator->dumpAsPhpArray($file, !$exclude_empty);
}
$progress_bar->advance();
}
$progress_bar->finish();
$io->newLine(2);
$io->success(
sprintf(
"Generated exclusion list for %s %s in directory %s.",
count($files),
count($files) > 1 ? 'files' : 'file',
$output_dir,
)
);
return Command::SUCCESS;
}
private function newGenerator(array $config, string $output_dir) :ExclusionListGenerator
{
$prefer = $config[Option::PREFER_PHP_VERSION] ?? ParserFactory::PREFER_PHP7;
$emulate_version = $config[Option::EMULATE_PHP_VERSION] ?? Option::PHP_8_0;
$lexer = new Emulative(['phpVersion' => $emulate_version]);
$parser = (new ParserFactory())->create($prefer, $lexer);
return new ExclusionListGenerator($parser, $output_dir);
}
/**
* @return string[]
*/
private function normalizeFiles(array $files) :array
{
$_files = [];
foreach ($files as $file) {
if (is_string($file)) {
$_files = array_merge($_files, [$file]);
continue;
}
if (is_iterable($file)) {
$_files = array_merge($_files, iterator_to_array($file));
}
}
return array_values(array_map(fn($file) => (string) $file, $_files));
}
private function newProgressBar(OutputInterface $output, int $file_count) :ProgressBar
{
$progress_bar = new ProgressBar($output, $file_count);
$progress_bar->setFormat(
' %current%/%max% [%bar%] <info>%percent%%</info> %elapsed:6s% <info>(%message%)</info>'
);
$progress_bar->setRedrawFrequency(100);
$progress_bar->maxSecondsBetweenRedraws(0.2);
$progress_bar->minSecondsBetweenRedraws(0.2);
return $progress_bar;
}
}