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

Added pattern exclusion support for "-implementation" packages #223

Merged
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
29 changes: 24 additions & 5 deletions src/Command/FilterDependencyCollectionCommand.php
Expand Up @@ -10,23 +10,34 @@

final class FilterDependencyCollectionCommand
{
private const GLOBAL_EXCLUSION = [
'composer-plugin-api',
private const GLOBAL_NAMED_EXCLUSION = [
'composer-plugin-api'
];

private const GLOBAL_PATTERN_EXCLUSION = [
'/-implementation$/i'
];

/** @var DependencyCollection */
private $requiredDependencyCollection;
/** @var array<string> */
private $namedExclusion;
/** @var array<string> */
private $patternExclusion;

/**
* @param DependencyCollection $requiredDependencyCollection
* @param array<string> $namedExclusion
* @param array<string> $patternExclusion
*/
public function __construct(DependencyCollection $requiredDependencyCollection, array $namedExclusion)
{
public function __construct(
DependencyCollection $requiredDependencyCollection,
array $namedExclusion = [],
array $patternExclusion = []
) {
$this->requiredDependencyCollection = $requiredDependencyCollection;
$this->namedExclusion = array_merge(self::GLOBAL_EXCLUSION, $namedExclusion);
$this->namedExclusion = array_merge(self::GLOBAL_NAMED_EXCLUSION, $namedExclusion);
$this->patternExclusion = array_merge(self::GLOBAL_PATTERN_EXCLUSION, $patternExclusion);
}

public function getRequiredDependencyCollection(): DependencyCollection
Expand All @@ -41,4 +52,12 @@ public function getNamedExclusion(): array
{
return $this->namedExclusion;
}

/**
* @return array<string>
*/
public function getPatternExclusion(): array
{
return $this->patternExclusion;
}
}
18 changes: 16 additions & 2 deletions src/Command/Handler/CollectFilteredDependenciesCommandHandler.php
Expand Up @@ -12,9 +12,23 @@ final class CollectFilteredDependenciesCommandHandler
public function collect(FilterDependencyCollectionCommand $command): DependencyCollection
{
$namedExclusion = $command->getNamedExclusion();
$patternExclusion = $command->getPatternExclusion();

return $command->getRequiredDependencyCollection()->filter(static function ($dependency) use ($namedExclusion) {
return !in_array($dependency->getName(), $namedExclusion);
return $command->getRequiredDependencyCollection()->filter(static function ($dependency) use (
$namedExclusion,
$patternExclusion
) {
if (in_array($dependency->getName(), $namedExclusion)) {
return false;
}

foreach ($patternExclusion as $exclusion) {
if (preg_match($exclusion, $dependency->getName())) {
return false;
}
}

return true;
});
}
}
4 changes: 2 additions & 2 deletions src/Console/Command/UnusedCommand.php
Expand Up @@ -97,7 +97,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
new CollectConsumedSymbolsCommand(
$baseDir,
$rootPackage
// TODO add excludes
)
);

Expand All @@ -112,7 +111,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$requiredDependencyCollection = $this->collectFilteredDependenciesCommandHandler->collect(
new FilterDependencyCollectionCommand(
$unfilteredRequiredDependencyCollection,
$input->getOption('excludePackage')
$input->getOption('excludePackage'),
[] // TODO use pattern exclude option from command line
)
);

Expand Down
17 changes: 17 additions & 0 deletions tests/Integration/UnusedCommandTest.php
Expand Up @@ -138,4 +138,21 @@ public function itShouldNotReportExcludedPackages(): void

self::assertStringNotContainsString('dummy/test-package', $output->fetch());
}

/**
* @test
*/
public function itShouldNotReportPatternExcludedPackages(): void
{
chdir(__DIR__ . '/../assets/TestProjects/IgnorePatternPackages');

$output = new BufferedOutput();

$this->getApplication()->run(
new ArrayInput(['unused']),
$output
);

self::assertStringNotContainsString('-implementation', $output->fetch());
}
}
8 changes: 8 additions & 0 deletions tests/assets/TestProjects/IgnorePatternPackages/composer.json
@@ -0,0 +1,8 @@
{
"name": "ns/lib",
"require": {
"dummy/test-package": "1.0",
"psr/log-implementation": "1.0.0",
"dummy/ff-implementation": "1.0.0"
}
}