diff --git a/Makefile b/Makefile index b061cfbb..7317ad50 100644 --- a/Makefile +++ b/Makefile @@ -21,8 +21,7 @@ require: ## Run `composer require` remove: ## Run `composer remove` docker compose run php$(PHP_VERSION) composer remove $(filter-out $@, $(MAKECMDGOALS)) -check: ## Run all checks in succession (phpcs, phpunit, phpstan) - cs analyse phpunit +check: cs analyse phpunit ## Run all checks in succession (phpcs, phpunit, phpstan) phpunit: ## Run phpunit tests docker compose run php$(PHP_VERSION) vendor/bin/phpunit diff --git a/src/Console/Command/UnusedCommand.php b/src/Console/Command/UnusedCommand.php index 1aa8221e..33874f03 100644 --- a/src/Console/Command/UnusedCommand.php +++ b/src/Console/Command/UnusedCommand.php @@ -137,7 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } if ($secondRequiredDependency->requires($requiredDependency)) { - // TODO add "required by" in output + $requiredDependency->requiredBy($secondRequiredDependency); $requiredDependency->markUsed(); continue 2; } @@ -178,14 +178,26 @@ static function (DependencyInterface $dependency) { $io->newLine(); $io->text('Used packages'); foreach ($usedDependencyCollection as $usedDependency) { - // TODO add required by dependency + $requiredBy = ''; // TODO add suggest by dependency + if (!empty($usedDependency->getRequiredBy())) { + $requiredByNames = array_map(static function (DependencyInterface $dependency) { + return $dependency->getName(); + }, $usedDependency->getRequiredBy()); + + $requiredBy = sprintf( + ' (required by: %s)', + implode(', ', $requiredByNames) + ); + } + $io->writeln( sprintf( - ' %s %s', + ' %s %s%s', "\u{2713}", - $usedDependency->getName() + $usedDependency->getName(), + $requiredBy ) ); } diff --git a/src/Dependency/DependencyInterface.php b/src/Dependency/DependencyInterface.php index 7595ad71..7b6fa4b7 100644 --- a/src/Dependency/DependencyInterface.php +++ b/src/Dependency/DependencyInterface.php @@ -21,4 +21,11 @@ public function provides(SymbolInterface $symbol): bool; public function requires(DependencyInterface $dependency): bool; public function suggests(DependencyInterface $dependency): bool; + + public function requiredBy(DependencyInterface $dependency): void; + + /** + * @return array + */ + public function getRequiredBy(): array; } diff --git a/src/Dependency/InvalidDependency.php b/src/Dependency/InvalidDependency.php index f3f0db99..de572a6f 100644 --- a/src/Dependency/InvalidDependency.php +++ b/src/Dependency/InvalidDependency.php @@ -49,4 +49,13 @@ public function suggests(DependencyInterface $dependency): bool { return false; } + + public function requiredBy(DependencyInterface $dependency): void + { + } + + public function getRequiredBy(): array + { + return []; + } } diff --git a/src/Dependency/RequiredDependency.php b/src/Dependency/RequiredDependency.php index 84734d0f..64b0a250 100644 --- a/src/Dependency/RequiredDependency.php +++ b/src/Dependency/RequiredDependency.php @@ -18,6 +18,8 @@ final class RequiredDependency implements DependencyInterface private $package; /** @var SymbolListInterface */ private $symbols; + /** @var array */ + private $requiredBy = []; public function __construct(PackageInterface $package, SymbolListInterface $symbols) { @@ -60,4 +62,14 @@ public function suggests(DependencyInterface $dependency): bool { return array_key_exists($dependency->getName(), $this->package->getSuggests()); } + + public function requiredBy(DependencyInterface $dependency): void + { + $this->requiredBy[$dependency->getName()] = $dependency; + } + + public function getRequiredBy(): array + { + return $this->requiredBy; + } } diff --git a/tests/Integration/UnusedCommandTest.php b/tests/Integration/UnusedCommandTest.php index 0a077467..d987b28b 100644 --- a/tests/Integration/UnusedCommandTest.php +++ b/tests/Integration/UnusedCommandTest.php @@ -7,7 +7,6 @@ use Composer\Composer; use Composer\Console\Application; use Composer\IO\IOInterface; -use Icanhazstring\Composer\Unused\Command\UnusedCommandLegacy; use Icanhazstring\Composer\Unused\Console\Command\UnusedCommand; use Icanhazstring\Composer\Unused\Di\ServiceContainer; use PHPUnit\Framework\TestCase; diff --git a/tests/Unit/Dependency/RequiredDependencyTest.php b/tests/Unit/Dependency/RequiredDependencyTest.php index 5b790f42..5762bc45 100644 --- a/tests/Unit/Dependency/RequiredDependencyTest.php +++ b/tests/Unit/Dependency/RequiredDependencyTest.php @@ -8,7 +8,6 @@ use Composer\Package\Package; use Composer\Package\PackageInterface; use Composer\Semver\Constraint\ConstraintInterface; -use Icanhazstring\Composer\Unused\Dependency\Dependency; use Icanhazstring\Composer\Unused\Dependency\RequiredDependency; use ComposerUnused\SymbolParser\Symbol\Symbol; use ComposerUnused\SymbolParser\Symbol\SymbolList;