Skip to content

Commit

Permalink
Added pattern exclusion support for "-implementation" packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Verena Röösli authored and icanhazstring committed Oct 21, 2021
1 parent 7dbc4c3 commit d52df84
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 9 deletions.
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"
}
}

0 comments on commit d52df84

Please sign in to comment.