-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStandardsCommand.php
More file actions
160 lines (139 loc) · 5.77 KB
/
StandardsCommand.php
File metadata and controls
160 lines (139 loc) · 5.77 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
153
154
155
156
157
158
159
160
<?php
declare(strict_types=1);
/**
* Fast Forward Development Tools for PHP projects.
*
* This file is part of fast-forward/dev-tools project.
*
* @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
* @license https://opensource.org/licenses/MIT MIT License
*
* @see https://github.com/php-fast-forward/
* @see https://github.com/php-fast-forward/dev-tools
* @see https://github.com/php-fast-forward/dev-tools/issues
* @see https://php-fast-forward.github.io/dev-tools/
* @see https://datatracker.ietf.org/doc/html/rfc2119
*/
namespace FastForward\DevTools\Console\Command;
use FastForward\DevTools\Console\Command\Traits\LogsCommandResults;
use Composer\Command\BaseCommand;
use FastForward\DevTools\Console\Input\HasCacheOption;
use FastForward\DevTools\Console\Input\HasJsonOption;
use FastForward\DevTools\Path\ManagedWorkspace;
use FastForward\DevTools\Process\ProcessBuilderInterface;
use FastForward\DevTools\Process\ProcessQueueInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Executes the full suite of Fast Forward code standard checks.
* This class MUST NOT be modified through inheritance and SHALL streamline code validation workflows.
*/
#[AsCommand(name: 'standards', description: 'Runs Fast Forward code standards checks.')]
final class StandardsCommand extends BaseCommand implements LoggerAwareCommandInterface
{
use HasCacheOption;
use HasJsonOption;
use LogsCommandResults;
/**
* @param ProcessBuilderInterface $processBuilder
* @param ProcessQueueInterface $processQueue
* @param LoggerInterface $logger
*/
public function __construct(
private readonly ProcessBuilderInterface $processBuilder,
private readonly ProcessQueueInterface $processQueue,
private readonly LoggerInterface $logger,
) {
parent::__construct();
}
/**
* Configures constraints and arguments for the collective standard runner.
*
* @return void
*/
protected function configure(): void
{
$this->setHelp(
'This command runs all Fast Forward code standards checks, including code refactoring, PHPDoc'
. ' validation, code style checks, documentation generation, and tests execution.'
);
$this
->addJsonOption()
->addCacheOption('Whether to enable cache writes in nested cache-aware standards commands.')
->addCacheDirOption(
description: 'Base cache directory used for nested cache-aware standards commands.',
default: ManagedWorkspace::getCacheDirectory('standards'),
)
->addOption(
name: 'progress',
mode: InputOption::VALUE_NONE,
description: 'Whether to enable progress output from nested standards commands.'
)
->addOption(
name: 'fix',
shortcut: 'f',
mode: InputOption::VALUE_NONE,
description: 'Automatically fix code standards issues.'
);
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$jsonOutput = $this->isJsonOutput($input);
$prettyJsonOutput = $this->isPrettyJsonOutput($input);
$progress = ! $jsonOutput && (bool) $input->getOption('progress');
$cacheArgument = $this->resolveCacheArgument($input);
$cacheDirEnabled = '--no-cache' !== $cacheArgument;
$commandOutput = $jsonOutput ? new BufferedOutput() : $output;
$commands = [];
$fix = (bool) $input->getOption('fix');
$this->logger->info('Running code standards checks...', [
'input' => $input,
]);
foreach (['refactor', 'phpdoc', 'code-style', 'reports'] as $command) {
$commands[] = $command;
$processBuilder = $this->processBuilder;
if ($progress) {
$processBuilder = $processBuilder->withArgument('--progress');
}
if ('reports' !== $command && $fix) {
$processBuilder = $processBuilder->withArgument('--fix');
}
if (\in_array($command, ['phpdoc', 'reports'], true) && null !== $cacheArgument) {
$processBuilder = $processBuilder->withArgument($cacheArgument);
}
if (
$cacheDirEnabled
&& \in_array($command, ['phpdoc', 'reports'], true)
&& null !== $cacheDir = $this->resolveCacheDirArgument($input, $command)
) {
$processBuilder = $processBuilder->withArgument('--cache-dir', $cacheDir);
}
if ($jsonOutput) {
$processBuilder = $processBuilder->withArgument('--json');
}
if ($prettyJsonOutput) {
$processBuilder = $processBuilder->withArgument('--pretty-json');
}
$this->processQueue->add($processBuilder->build('composer dev-tools ' . $command . ' --'));
}
$result = $this->processQueue->run($commandOutput);
if (self::FAILURE === $result) {
return $this->failure('Code standards checks failed.', $input, [
'output' => $commandOutput,
'commands' => $commands,
]);
}
return $this->success('Code standards checks completed successfully.', $input, [
'output' => $commandOutput,
'commands' => $commands,
]);
}
}