diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index beae321..af8390f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,13 +19,12 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['8.0', '8.1'] prefer-lowest: [ '', '--prefer-lowest' ] steps: - uses: actions/checkout@master - uses: shivammathur/setup-php@master with: - php-version: ${{ matrix.php-versions }} + php-version: '8.1' - name: Install dependencies run: composer update -n --prefer-dist ${{ matrix.prefer-lowest }} - name: Run PHPUnit unit tests @@ -37,7 +36,7 @@ jobs: - uses: actions/checkout@master - uses: shivammathur/setup-php@master with: - php-version: '8.0' + php-version: '8.1' - name: Install dependencies run: composer update - name: Run PHP-CS-Fixer diff --git a/.gitignore b/.gitignore index 4106ec5..04e130b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ composer.phar /vendor/ composer.lock .phpunit.result.cache -.php_cs.cache +.php-cs-fixer.cache diff --git a/.php_cs b/.php-cs-fixer.php similarity index 100% rename from .php_cs rename to .php-cs-fixer.php diff --git a/composer.json b/composer.json index 973192c..c7833ab 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "minimum-stability": "stable", "bin": ["bin/license-checker"], "require": { - "php": "^8.0", + "php": "^8.1", "symfony/console": "^4.0 || ^5.0 || ^6.0", "symfony/process": "^4.0 || ^5.0 || ^6.0", "symfony/yaml": "^4.0 || ^5.0 || ^6.0" @@ -21,7 +21,7 @@ "phpunit/phpunit": "^9.5", "vimeo/psalm": "^4.4", "psalm/plugin-phpunit": "^0.15.1", - "friendsofphp/php-cs-fixer": "^2.18" + "friendsofphp/php-cs-fixer": "^3.8" }, "autoload": { "psr-4": { diff --git a/src/Commands/CheckLicenses.php b/src/Commands/CheckLicenses.php index 62dd042..44fdb38 100644 --- a/src/Commands/CheckLicenses.php +++ b/src/Commands/CheckLicenses.php @@ -67,11 +67,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int $dependencyChecks = []; foreach ($dependencies as $dependency) { - $dependencyCheck = new DependencyCheck($dependency->getName(), $dependency->getLicense()); + $dependencyCheck = new DependencyCheck($dependency); foreach ($notAllowedLicenses as $notAllowedLicense) { $packagesUsingThisLicense = $this->usedLicensesParser->getPackagesWithLicense($notAllowedLicense, (bool)$input->getOption('no-dev')); foreach ($packagesUsingThisLicense as $packageUsingThisLicense) { - if ($dependency->hasDependency($packageUsingThisLicense) || $dependency->getName() === $packageUsingThisLicense) { + if ($dependency->hasDependency($packageUsingThisLicense) || $dependency->is($packageUsingThisLicense)) { $dependencyCheck = $dependencyCheck->addFailedDependency($packageUsingThisLicense, $notAllowedLicense); } } diff --git a/src/Commands/Output/CauseOfFailure.php b/src/Commands/Output/CauseOfFailure.php deleted file mode 100644 index b19f21d..0000000 --- a/src/Commands/Output/CauseOfFailure.php +++ /dev/null @@ -1,24 +0,0 @@ -name; - } - - public function getLicense(): string - { - return $this->license; - } -} diff --git a/src/Commands/Output/DependencyCheck.php b/src/Commands/Output/DependencyCheck.php index 883a9e0..f26bb7d 100644 --- a/src/Commands/Output/DependencyCheck.php +++ b/src/Commands/Output/DependencyCheck.php @@ -4,18 +4,19 @@ namespace LicenseChecker\Commands\Output; +use LicenseChecker\Dependency; + final class DependencyCheck { private bool $isAllowed = true; /** - * @var CauseOfFailure[] + * @var Dependency[] */ private array $causedBy = []; public function __construct( - private string $name, - private string $license, + public readonly Dependency $dependency, ) { } @@ -23,18 +24,14 @@ public function addFailedDependency(string $dependency, string $license): self { $dependencyCheck = clone $this; $dependencyCheck->isAllowed = false; - $dependencyCheck->causedBy[] = new CauseOfFailure($dependency, $license); + $dependencyCheck->causedBy[] = new Dependency($dependency, $license); return $dependencyCheck; } public function renderNameWithLicense(): string { - if (empty($this->license)) { - return $this->name; - } - - return $this->name . ' [' . $this->license . ']'; + return $this->dependency->renderNameWithLicense(); } public function isAllowed(): bool @@ -43,7 +40,7 @@ public function isAllowed(): bool } /** - * @return CauseOfFailure[] + * @return Dependency[] */ public function getCausedBy(): array { diff --git a/src/Commands/Output/TableRenderer.php b/src/Commands/Output/TableRenderer.php index b4aceba..14cf23f 100644 --- a/src/Commands/Output/TableRenderer.php +++ b/src/Commands/Output/TableRenderer.php @@ -4,6 +4,7 @@ namespace LicenseChecker\Commands\Output; +use LicenseChecker\Dependency; use Symfony\Component\Console\Style\SymfonyStyle; class TableRenderer @@ -119,24 +120,24 @@ private function renderAllowedLineWithEmptyFailureCause(DependencyCheck $depende */ private function renderFailedLineWithCauseOfFailure( DependencyCheck $dependencyCheck, - CauseOfFailure $causeOfFailure + Dependency $causeOfFailure ): array { return [ $this->renderBoolean($dependencyCheck->isAllowed()), $dependencyCheck->renderNameWithLicense(), - $causeOfFailure->getName() . ' [' . $causeOfFailure->getLicense() . ']', + $causeOfFailure->renderNameWithLicense(), ]; } /** * @return string[] */ - private function renderAdditionalCauseOfFailure(CauseOfFailure $causeOfFailure): array + private function renderAdditionalCauseOfFailure(Dependency $causeOfFailure): array { return [ '', '', - $causeOfFailure->getName() . ' [' . $causeOfFailure->getLicense() . ']', + $causeOfFailure->renderNameWithLicense(), ]; } } diff --git a/src/Configuration/ConfigurationExists.php b/src/Configuration/ConfigurationExists.php index bd02bd5..48bd4ed 100644 --- a/src/Configuration/ConfigurationExists.php +++ b/src/Configuration/ConfigurationExists.php @@ -6,6 +6,6 @@ use RuntimeException; -class ConfigurationExists extends RuntimeException +final class ConfigurationExists extends RuntimeException { } diff --git a/src/Dependency.php b/src/Dependency.php index 50952f7..e9a1ffd 100644 --- a/src/Dependency.php +++ b/src/Dependency.php @@ -4,7 +4,7 @@ namespace LicenseChecker; -class Dependency +final class Dependency { /** * @var string[] @@ -12,11 +12,25 @@ class Dependency private array $subDependencies = []; public function __construct( - private string $name, - private string $license, + private readonly string $name, + private readonly string $license, ) { } + public function is(string $name): bool + { + return $this->name === $name; + } + + public function renderNameWithLicense(): string + { + if (empty($this->license)) { + return $this->name; + } + + return $this->name . ' [' . $this->license . ']'; + } + public function addDependency(string $dependency): self { if (!$this->hasDependency($dependency)) { @@ -38,14 +52,4 @@ public function hasDependency(string $dependency): bool { return array_search($dependency, $this->getDependencies(), true) !== false; } - - public function getName(): string - { - return $this->name; - } - - public function getLicense(): string - { - return $this->license; - } } diff --git a/tests/Commands/Output/TableRendererTest.php b/tests/Commands/Output/TableRendererTest.php index bfbd3a2..114d37e 100644 --- a/tests/Commands/Output/TableRendererTest.php +++ b/tests/Commands/Output/TableRendererTest.php @@ -6,6 +6,7 @@ use LicenseChecker\Commands\Output\DependencyCheck; use LicenseChecker\Commands\Output\TableRenderer; +use LicenseChecker\Dependency; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Style\SymfonyStyle; @@ -38,8 +39,8 @@ public function itWillRenderTwoColumnsOnSuccess(): void ); $dependencyChecks = [ - new DependencyCheck('foo', 'license'), - new DependencyCheck('bar', 'license'), + new DependencyCheck(new Dependency('foo', 'license')), + new DependencyCheck(new Dependency('bar', 'license')), ]; $this->tableRenderer->renderDependencyChecks( @@ -63,8 +64,8 @@ public function itWillListCausingPackagesOnFailure(): void ); $dependencyChecks = [ - (new DependencyCheck('foo', 'license'))->addFailedDependency('baz', 'license')->addFailedDependency('baz2', 'license'), - (new DependencyCheck('bar', 'license'))->addFailedDependency('baz', 'license'), + (new DependencyCheck(new Dependency('foo', 'license')))->addFailedDependency('baz', 'license')->addFailedDependency('baz2', 'license'), + (new DependencyCheck(new Dependency('bar', 'license')))->addFailedDependency('baz', 'license'), ]; $this->tableRenderer->renderDependencyChecks( @@ -87,8 +88,8 @@ public function itWillRenderFailingDependenciesFirst(): void ); $dependencyChecks = [ - new DependencyCheck('bar', 'license'), - (new DependencyCheck('foo', 'license'))->addFailedDependency('baz', 'license'), + new DependencyCheck(new Dependency('bar', 'license')), + (new DependencyCheck(new Dependency('foo', 'license')))->addFailedDependency('baz', 'license'), ]; $this->tableRenderer->renderDependencyChecks( diff --git a/tests/DependencyTest.php b/tests/DependencyTest.php index d6453a4..b240fe1 100644 --- a/tests/DependencyTest.php +++ b/tests/DependencyTest.php @@ -12,19 +12,10 @@ class DependencyTest extends TestCase /** * @test */ - public function itKnowsItsName(): void + public function itCanRenderNameWithLicense(): void { $dependency = new Dependency('foo', 'license'); - $this->assertEquals('foo', $dependency->getName()); - } - - /** - * @test - */ - public function itKnowsItsLicense(): void - { - $dependency = new Dependency('foo', 'license'); - $this->assertEquals('license', $dependency->getLicense()); + $this->assertEquals('foo [license]', $dependency->renderNameWithLicense()); } /**