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 support for the excludePackage option #222

Merged
merged 2 commits into from Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 3 additions & 0 deletions config/service_manager.php
Expand Up @@ -3,8 +3,10 @@
declare(strict_types=1);

use Icanhazstring\Composer\Unused\Command\Handler\CollectConsumedSymbolsCommandHandler;
use Icanhazstring\Composer\Unused\Command\Handler\CollectFilteredDependenciesCommandHandler;
use Icanhazstring\Composer\Unused\Command\Handler\CollectRequiredDependenciesCommandHandler;
use Icanhazstring\Composer\Unused\Command\Handler\Factory\CollectConsumedSymbolsCommandHandlerFactory;
use Icanhazstring\Composer\Unused\Command\Handler\Factory\CollectFilteredDependenciesCommandHandlerFactory;
use Icanhazstring\Composer\Unused\Command\Handler\Factory\CollectRequiredDependenciesCommandHandlerFactory;
use Icanhazstring\Composer\Unused\Console\Command\UnusedCommand;
use Icanhazstring\Composer\Unused\Console\Command\UnusedCommandFactory;
Expand All @@ -18,6 +20,7 @@
UnusedCommand::class => UnusedCommandFactory::class,
CollectConsumedSymbolsCommandHandler::class => CollectConsumedSymbolsCommandHandlerFactory::class,
CollectRequiredDependenciesCommandHandler::class => CollectRequiredDependenciesCommandHandlerFactory::class,
CollectFilteredDependenciesCommandHandler::class => CollectFilteredDependenciesCommandHandlerFactory::class,
ConsumedSymbolLoaderBuilder::class => InvokableFactory::class,
ProvidedSymbolLoaderBuilder::class => InvokableFactory::class,
PackageResolver::class => InvokableFactory::class,
Expand Down
44 changes: 44 additions & 0 deletions src/Command/FilterDependencyCollectionCommand.php
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Icanhazstring\Composer\Unused\Command;

use Icanhazstring\Composer\Unused\Dependency\DependencyCollection;

use function array_merge;

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

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

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

public function getRequiredDependencyCollection(): DependencyCollection
{
return $this->requiredDependencyCollection;
}

/**
* @return array<string>
*/
public function getNamedExclusion(): array
{
return $this->namedExclusion;
}
}
25 changes: 25 additions & 0 deletions src/Command/Handler/CollectFilteredDependenciesCommandHandler.php
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Icanhazstring\Composer\Unused\Command\Handler;

use Icanhazstring\Composer\Unused\Command\FilterDependencyCollectionCommand;
use Icanhazstring\Composer\Unused\Dependency\DependencyCollection;

final class CollectFilteredDependenciesCommandHandler
{
public function collect(FilterDependencyCollectionCommand $command): DependencyCollection
{
$dependencyCollection = new DependencyCollection();
$namedExclusion = $command->getNamedExclusion();

foreach ($command->getRequiredDependencyCollection() as $dependency) {
verenaroe marked this conversation as resolved.
Show resolved Hide resolved
if (!in_array($dependency->getName(), $namedExclusion)) {
$dependencyCollection->add($dependency);
}
}

return $dependencyCollection;
}
}
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Icanhazstring\Composer\Unused\Command\Handler\Factory;

use Icanhazstring\Composer\Unused\Command\Handler\CollectFilteredDependenciesCommandHandler;
use Psr\Container\ContainerInterface;

final class CollectFilteredDependenciesCommandHandlerFactory
{
public function __invoke(ContainerInterface $container): CollectFilteredDependenciesCommandHandler
{
return new CollectFilteredDependenciesCommandHandler();
}
}
17 changes: 15 additions & 2 deletions src/Console/Command/UnusedCommand.php
Expand Up @@ -6,7 +6,9 @@

use Composer\Command\BaseCommand;
use Icanhazstring\Composer\Unused\Command\CollectConsumedSymbolsCommand;
use Icanhazstring\Composer\Unused\Command\FilterDependencyCollectionCommand;
use Icanhazstring\Composer\Unused\Command\Handler\CollectConsumedSymbolsCommandHandler;
use Icanhazstring\Composer\Unused\Command\Handler\CollectFilteredDependenciesCommandHandler;
use Icanhazstring\Composer\Unused\Command\Handler\CollectRequiredDependenciesCommandHandler;
use Icanhazstring\Composer\Unused\Command\LoadRequiredDependenciesCommand;
use Icanhazstring\Composer\Unused\Dependency\DependencyCollection;
Expand All @@ -28,14 +30,18 @@ final class UnusedCommand extends BaseCommand
private $collectConsumedSymbolsCommandHandler;
/** @var CollectRequiredDependenciesCommandHandler */
private $collectRequiredDependenciesCommandHandler;
/** @var CollectFilteredDependenciesCommandHandler */
private $collectFilteredDependenciesCommandHandler;

public function __construct(
CollectConsumedSymbolsCommandHandler $collectConsumedSymbolsCommandHandler,
CollectRequiredDependenciesCommandHandler $collectRequiredDependenciesCommandHandler
CollectRequiredDependenciesCommandHandler $collectRequiredDependenciesCommandHandler,
CollectFilteredDependenciesCommandHandler $collectFilteredDependenciesCommandHandler
) {
parent::__construct('unused');
$this->collectConsumedSymbolsCommandHandler = $collectConsumedSymbolsCommandHandler;
$this->collectRequiredDependenciesCommandHandler = $collectRequiredDependenciesCommandHandler;
$this->collectFilteredDependenciesCommandHandler = $collectFilteredDependenciesCommandHandler;
}

protected function configure(): void
Expand Down Expand Up @@ -95,14 +101,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
)
);

$requiredDependencyCollection = $this->collectRequiredDependenciesCommandHandler->collect(
$unfilteredRequiredDependencyCollection = $this->collectRequiredDependenciesCommandHandler->collect(
new LoadRequiredDependenciesCommand(
$baseDir . DIRECTORY_SEPARATOR . $composer->getConfig()->get('vendor-dir'),
$rootPackage->getRequires(),
$composer->getRepositoryManager()->getLocalRepository()
)
);

$requiredDependencyCollection = $this->collectFilteredDependenciesCommandHandler->collect(
new FilterDependencyCollectionCommand(
$unfilteredRequiredDependencyCollection,
$input->getOption('excludePackage')
)
);

$io = new SymfonyStyle($input, $output);

foreach ($consumedSymbols as $symbol) {
Expand Down
4 changes: 3 additions & 1 deletion src/Console/Command/UnusedCommandFactory.php
Expand Up @@ -5,6 +5,7 @@
namespace Icanhazstring\Composer\Unused\Console\Command;

use Icanhazstring\Composer\Unused\Command\Handler\CollectConsumedSymbolsCommandHandler;
use Icanhazstring\Composer\Unused\Command\Handler\CollectFilteredDependenciesCommandHandler;
use Icanhazstring\Composer\Unused\Command\Handler\CollectRequiredDependenciesCommandHandler;
use Psr\Container\ContainerInterface;

Expand All @@ -14,7 +15,8 @@ public function __invoke(ContainerInterface $container): UnusedCommand
{
return new UnusedCommand(
$container->get(CollectConsumedSymbolsCommandHandler::class),
$container->get(CollectRequiredDependenciesCommandHandler::class)
$container->get(CollectRequiredDependenciesCommandHandler::class),
$container->get(CollectFilteredDependenciesCommandHandler::class),
);
}
}
35 changes: 35 additions & 0 deletions tests/Integration/UnusedCommandTest.php
Expand Up @@ -12,6 +12,7 @@
use Icanhazstring\Composer\Unused\Di\ServiceContainer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\NullOutput;

class UnusedCommandTest extends TestCase
Expand Down Expand Up @@ -103,4 +104,38 @@ public function itShouldNoReportUnusedWithAutoloadFilesWithRequire(): void
)
);
}

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

$output = new BufferedOutput();

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

self::assertStringNotContainsString('composer-plugin-api', $output->fetch());
}

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

$output = new BufferedOutput();

$this->getApplication()->run(
new ArrayInput(['unused', '--excludePackage' => ['dummy/test-package']]),
$output
);

self::assertStringNotContainsString('dummy/test-package', $output->fetch());
}
}
@@ -0,0 +1,7 @@
{
"name": "ns/lib",
"require": {
"dummy/test-package": "1.0",
"composer-plugin-api": "^2.0"
}
}
6 changes: 6 additions & 0 deletions tests/assets/TestProjects/IgnoreSpecialPackages/composer.json
@@ -0,0 +1,6 @@
{
"name": "ns/lib",
"require": {
"composer-plugin-api": "^2.0"
}
}