|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Symfony\Component\Console\Input\ArrayInput; |
| 6 | +use Symfony\Component\Console\Output\ConsoleOutput; |
| 7 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 8 | + |
| 9 | +require __DIR__ . '/../vendor/autoload.php'; |
| 10 | + |
| 11 | +$symfonyStyle = new SymfonyStyle(new ArrayInput([]), new ConsoleOutput()); |
| 12 | + |
| 13 | +// 1. run the same workflow as CI "Detect composer dependency issues", in machine-readable format |
| 14 | +exec(__DIR__ . '/../vendor/bin/composer-dependency-analyser --format junit 2>/dev/null', $outputLines); |
| 15 | +$junitXml = implode("\n", $outputLines); |
| 16 | + |
| 17 | +$simpleXml = @simplexml_load_string($junitXml); |
| 18 | +if (! $simpleXml instanceof SimpleXMLElement) { |
| 19 | + $symfonyStyle->error('Failed to parse composer-dependency-analyser output'); |
| 20 | + exit(1); |
| 21 | +} |
| 22 | + |
| 23 | +// 2. collect unused dependencies from the "unused dependencies" testsuite |
| 24 | +$unusedDependencies = []; |
| 25 | +foreach ($simpleXml->testsuite as $testsuite) { |
| 26 | + if ((string) $testsuite['name'] !== 'unused dependencies') { |
| 27 | + continue; |
| 28 | + } |
| 29 | + |
| 30 | + foreach ($testsuite->testcase as $testcase) { |
| 31 | + $unusedDependencies[] = (string) $testcase['name']; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +if ($unusedDependencies === []) { |
| 36 | + $symfonyStyle->success('No unused dependencies found'); |
| 37 | + exit(0); |
| 38 | +} |
| 39 | + |
| 40 | +$symfonyStyle->listing($unusedDependencies); |
| 41 | + |
| 42 | +// 3. remove unused dependencies from composer.json (composer auto-detects require/require-dev) |
| 43 | +// the autocommit to the branch is handled by the workflow's git-auto-commit-action |
| 44 | +foreach ($unusedDependencies as $unusedDependency) { |
| 45 | + exec(sprintf('composer remove %s --no-update --no-interaction 2>&1', escapeshellarg($unusedDependency))); |
| 46 | +} |
| 47 | + |
| 48 | +$symfonyStyle->success(sprintf('Removed %d unused dependency(ies) from composer.json', count($unusedDependencies))); |
0 commit comments