|
| 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\Output\OutputInterface; |
| 23 | +use Symfony\Component\Process\Process; |
| 24 | + |
| 25 | +/** |
| 26 | + * Orchestrates dependency analysis across the supported Composer analyzers. |
| 27 | + * This command MUST report missing and unused dependencies using a single, |
| 28 | + * deterministic report that is friendly for local development and CI runs. |
| 29 | + */ |
| 30 | +final class DependenciesCommand extends AbstractCommand |
| 31 | +{ |
| 32 | + /** |
| 33 | + * Configures the dependency analysis command metadata. |
| 34 | + * |
| 35 | + * The command MUST expose the `dependencies` name so it can run via both |
| 36 | + * Composer and the local `dev-tools` binary. |
| 37 | + * |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + protected function configure(): void |
| 41 | + { |
| 42 | + $this |
| 43 | + ->setName('dependencies') |
| 44 | + ->setAliases(['deps']) |
| 45 | + ->setDescription('Analyzes missing and unused Composer dependencies.') |
| 46 | + ->setHelp( |
| 47 | + 'This command runs composer-dependency-analyser and composer-unused to report ' |
| 48 | + . 'missing and unused Composer dependencies.' |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Executes the dependency analysis workflow. |
| 54 | + * |
| 55 | + * The command MUST verify the required binaries before executing the tools, |
| 56 | + * SHOULD normalize their machine-readable output into a unified report, and |
| 57 | + * SHALL return a non-zero exit code when findings or execution failures exist. |
| 58 | + * |
| 59 | + * @param InputInterface $input the runtime command input |
| 60 | + * @param OutputInterface $output the console output stream |
| 61 | + * |
| 62 | + * @return int the command execution status code |
| 63 | + */ |
| 64 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 65 | + { |
| 66 | + $output->writeln('<info>Running dependency analysis...</info>'); |
| 67 | + |
| 68 | + $composerJson = $this->getConfigFile('composer.json'); |
| 69 | + |
| 70 | + $results[] = $this->runProcess( |
| 71 | + new Process(['vendor/bin/composer-unused', $composerJson, '--no-progress']), |
| 72 | + $output |
| 73 | + ); |
| 74 | + $results[] = $this->runProcess(new Process([ |
| 75 | + 'vendor/bin/composer-dependency-analyser', |
| 76 | + '--composer-json=' . $composerJson, |
| 77 | + '--ignore-unused-deps', |
| 78 | + '--ignore-prod-only-in-dev-deps', |
| 79 | + ]), $output); |
| 80 | + |
| 81 | + return \in_array(self::FAILURE, $results, true) ? self::FAILURE : self::SUCCESS; |
| 82 | + } |
| 83 | +} |
0 commit comments