From a0bac15a7e1cb6f06b7ec3fd5be6dc91f997484d Mon Sep 17 00:00:00 2001 From: jdrieghe Date: Tue, 10 May 2022 12:10:24 +0200 Subject: [PATCH] Privatize Dependency properties --- src/Commands/CheckLicenses.php | 2 +- src/Commands/Output/DependencyCheck.php | 6 +----- src/Commands/Output/TableRenderer.php | 4 ++-- src/Dependency.php | 18 ++++++++++++++++-- tests/DependencyTest.php | 13 ++----------- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/Commands/CheckLicenses.php b/src/Commands/CheckLicenses.php index 2983b00..44fdb38 100644 --- a/src/Commands/CheckLicenses.php +++ b/src/Commands/CheckLicenses.php @@ -71,7 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int foreach ($notAllowedLicenses as $notAllowedLicense) { $packagesUsingThisLicense = $this->usedLicensesParser->getPackagesWithLicense($notAllowedLicense, (bool)$input->getOption('no-dev')); foreach ($packagesUsingThisLicense as $packageUsingThisLicense) { - if ($dependency->hasDependency($packageUsingThisLicense) || $dependency->name === $packageUsingThisLicense) { + if ($dependency->hasDependency($packageUsingThisLicense) || $dependency->is($packageUsingThisLicense)) { $dependencyCheck = $dependencyCheck->addFailedDependency($packageUsingThisLicense, $notAllowedLicense); } } diff --git a/src/Commands/Output/DependencyCheck.php b/src/Commands/Output/DependencyCheck.php index f00fd26..f26bb7d 100644 --- a/src/Commands/Output/DependencyCheck.php +++ b/src/Commands/Output/DependencyCheck.php @@ -31,11 +31,7 @@ public function addFailedDependency(string $dependency, string $license): self public function renderNameWithLicense(): string { - if (empty($this->dependency->license)) { - return $this->dependency->name; - } - - return $this->dependency->name . ' [' . $this->dependency->license . ']'; + return $this->dependency->renderNameWithLicense(); } public function isAllowed(): bool diff --git a/src/Commands/Output/TableRenderer.php b/src/Commands/Output/TableRenderer.php index 70f8e73..14cf23f 100644 --- a/src/Commands/Output/TableRenderer.php +++ b/src/Commands/Output/TableRenderer.php @@ -125,7 +125,7 @@ private function renderFailedLineWithCauseOfFailure( return [ $this->renderBoolean($dependencyCheck->isAllowed()), $dependencyCheck->renderNameWithLicense(), - $causeOfFailure->name . ' [' . $causeOfFailure->license . ']', + $causeOfFailure->renderNameWithLicense(), ]; } @@ -137,7 +137,7 @@ private function renderAdditionalCauseOfFailure(Dependency $causeOfFailure): arr return [ '', '', - $causeOfFailure->name . ' [' . $causeOfFailure->license . ']', + $causeOfFailure->renderNameWithLicense(), ]; } } diff --git a/src/Dependency.php b/src/Dependency.php index e36aefb..e9a1ffd 100644 --- a/src/Dependency.php +++ b/src/Dependency.php @@ -12,11 +12,25 @@ final class Dependency private array $subDependencies = []; public function __construct( - public readonly string $name, - public readonly 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)) { diff --git a/tests/DependencyTest.php b/tests/DependencyTest.php index af3811e..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->name); - } - - /** - * @test - */ - public function itKnowsItsLicense(): void - { - $dependency = new Dependency('foo', 'license'); - $this->assertEquals('license', $dependency->license); + $this->assertEquals('foo [license]', $dependency->renderNameWithLicense()); } /**