Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| CodeStyleCommand | |
100.00% |
21 / 21 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| configure | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * This file is part of fast-forward/dev-tools. |
| 7 | * |
| 8 | * This source file is subject to the license bundled |
| 9 | * with this source code in the file LICENSE. |
| 10 | * |
| 11 | * @copyright Copyright (c) 2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 12 | * @license https://opensource.org/licenses/MIT MIT License |
| 13 | * |
| 14 | * @see https://github.com/php-fast-forward/dev-tools |
| 15 | * @see https://github.com/php-fast-forward |
| 16 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | */ |
| 18 | |
| 19 | namespace FastForward\DevTools\Command; |
| 20 | |
| 21 | use Symfony\Component\Console\Input\InputInterface; |
| 22 | use Symfony\Component\Console\Input\InputOption; |
| 23 | use Symfony\Component\Console\Output\OutputInterface; |
| 24 | use Symfony\Component\Process\Process; |
| 25 | |
| 26 | /** |
| 27 | * Represents the command responsible for checking and fixing code style issues. |
| 28 | * This class MUST NOT be overridden and SHALL rely on external tools like ECS and Composer Normalize. |
| 29 | */ |
| 30 | final class CodeStyleCommand extends AbstractCommand |
| 31 | { |
| 32 | /** |
| 33 | * @var string the default configuration file used for EasyCodingStandard |
| 34 | */ |
| 35 | public const string CONFIG = 'ecs.php'; |
| 36 | |
| 37 | /** |
| 38 | * Configures the current command. |
| 39 | * |
| 40 | * This method MUST define the name, description, help text, and options for the command. |
| 41 | * It SHALL register the `--fix` option to allow automatic resolutions of style issues. |
| 42 | * |
| 43 | * @return void |
| 44 | */ |
| 45 | protected function configure(): void |
| 46 | { |
| 47 | $this |
| 48 | ->setName('code-style') |
| 49 | ->setDescription('Checks and fixes code style issues using EasyCodingStandard and Composer Normalize.') |
| 50 | ->setHelp('This command runs EasyCodingStandard and Composer Normalize to check and fix code style issues.') |
| 51 | ->addOption( |
| 52 | name: 'fix', |
| 53 | shortcut: 'f', |
| 54 | mode: InputOption::VALUE_NONE, |
| 55 | description: 'Automatically fix code style issues.' |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Executes the code style checks and fixes block. |
| 61 | * |
| 62 | * The method MUST execute `composer update --lock`, `composer normalize`, and ECS using secure processes. |
| 63 | * It SHALL return `self::SUCCESS` if all commands succeed, or `self::FAILURE` otherwise. |
| 64 | * |
| 65 | * @param InputInterface $input the input interface to retrieve options |
| 66 | * @param OutputInterface $output the output interface to log messages |
| 67 | * |
| 68 | * @return int the status code of the command |
| 69 | */ |
| 70 | protected function execute(InputInterface $input, OutputInterface $output): int |
| 71 | { |
| 72 | $output->writeln('<info>Running code style checks and fixes...</info>'); |
| 73 | |
| 74 | $command = new Process(['composer', 'update', '--lock', '--quiet']); |
| 75 | |
| 76 | parent::runProcess($command, $output); |
| 77 | |
| 78 | $command = new Process(['composer', 'normalize', $input->getOption('fix') ? '--quiet' : '--dry-run']); |
| 79 | |
| 80 | parent::runProcess($command, $output); |
| 81 | |
| 82 | $command = new Process([ |
| 83 | $this->getAbsolutePath('vendor/bin/ecs'), |
| 84 | '--config=' . parent::getConfigFile(self::CONFIG), |
| 85 | $input->getOption('fix') ? '--fix' : '--clear-cache', |
| 86 | ]); |
| 87 | |
| 88 | return parent::runProcess($command, $output); |
| 89 | } |
| 90 | } |