Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve #219: Add required-by output after scan #260

Merged
merged 1 commit into from Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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