Skip to content

Commit

Permalink
Solve #219: Add required-by output after scan
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Frömer <andreas.froemer@check24.de>
  • Loading branch information
Andreas Frömer authored and icanhazstring committed Jan 3, 2022
1 parent 477de8f commit 44ca239
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 8 deletions.
3 changes: 1 addition & 2 deletions Makefile
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions src/Console/Command/UnusedCommand.php
Expand Up @@ -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;
}
Expand Down Expand Up @@ -178,14 +178,26 @@ static function (DependencyInterface $dependency) {
$io->newLine();
$io->text('<fg=green>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(
' (<fg=cyan>required by: %s</>)',
implode(', ', $requiredByNames)
);
}

$io->writeln(
sprintf(
' <fg=green>%s</> %s',
' <fg=green>%s</> %s%s',
"\u{2713}",
$usedDependency->getName()
$usedDependency->getName(),
$requiredBy
)
);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Dependency/DependencyInterface.php
Expand Up @@ -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<DependencyInterface>
*/
public function getRequiredBy(): array;
}
9 changes: 9 additions & 0 deletions src/Dependency/InvalidDependency.php
Expand Up @@ -49,4 +49,13 @@ public function suggests(DependencyInterface $dependency): bool
{
return false;
}

public function requiredBy(DependencyInterface $dependency): void
{
}

public function getRequiredBy(): array
{
return [];
}
}
12 changes: 12 additions & 0 deletions src/Dependency/RequiredDependency.php
Expand Up @@ -18,6 +18,8 @@ final class RequiredDependency implements DependencyInterface
private $package;
/** @var SymbolListInterface */
private $symbols;
/** @var array<DependencyInterface> */
private $requiredBy = [];

public function __construct(PackageInterface $package, SymbolListInterface $symbols)
{
Expand Down Expand Up @@ -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;
}
}
1 change: 0 additions & 1 deletion tests/Integration/UnusedCommandTest.php
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/Dependency/RequiredDependencyTest.php
Expand Up @@ -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;
Expand Down

0 comments on commit 44ca239

Please sign in to comment.