Skip to content

Commit

Permalink
Merge pull request #39 from madewithlove/php81
Browse files Browse the repository at this point in the history
Upgrade to PHP 8.1 and some internal cleanup
  • Loading branch information
jdrieghe authored May 10, 2022
2 parents c232524 + 7d1b0f6 commit 579e4e8
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 77 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ composer.phar
/vendor/
composer.lock
.phpunit.result.cache
.php_cs.cache
.php-cs-fixer.cache
File renamed without changes.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/CheckLicenses.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
24 changes: 0 additions & 24 deletions src/Commands/Output/CauseOfFailure.php

This file was deleted.

17 changes: 7 additions & 10 deletions src/Commands/Output/DependencyCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,34 @@

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,
) {
}

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
Expand All @@ -43,7 +40,7 @@ public function isAllowed(): bool
}

/**
* @return CauseOfFailure[]
* @return Dependency[]
*/
public function getCausedBy(): array
{
Expand Down
9 changes: 5 additions & 4 deletions src/Commands/Output/TableRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace LicenseChecker\Commands\Output;

use LicenseChecker\Dependency;
use Symfony\Component\Console\Style\SymfonyStyle;

class TableRenderer
Expand Down Expand Up @@ -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(),
];
}
}
2 changes: 1 addition & 1 deletion src/Configuration/ConfigurationExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

use RuntimeException;

class ConfigurationExists extends RuntimeException
final class ConfigurationExists extends RuntimeException
{
}
30 changes: 17 additions & 13 deletions src/Dependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,33 @@

namespace LicenseChecker;

class Dependency
final class Dependency
{
/**
* @var string[]
*/
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)) {
Expand All @@ -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;
}
}
13 changes: 7 additions & 6 deletions tests/Commands/Output/TableRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down
13 changes: 2 additions & 11 deletions tests/DependencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down

0 comments on commit 579e4e8

Please sign in to comment.