diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d41594d..1290530 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,23 +6,24 @@ jobs: name: Psalm runs-on: ubuntu-latest steps: - - name: Checkout code - uses: actions/checkout@master - - name: Psalm - uses: docker://vimeo/psalm-github-actions + - uses: actions/checkout@master + - uses: shivammathur/setup-php@master + with: + php-version: '8.0' + - name: Install dependencies + run: composer update + - name: Run Psalm + run: vendor/bin/psalm phpunit: runs-on: ubuntu-latest - strategy: - matrix: - php-versions: ['7.2', '7.3', '7.4'] steps: - uses: actions/checkout@master - - uses: shivammathur/setup-php@v1 + - uses: shivammathur/setup-php@master with: - php-version: ${{ matrix.php-versions }} + php-version: '8.0' - name: Install dependencies - run: composer install -n --prefer-dist + run: composer update - name: Run PHPUnit unit tests run: vendor/bin/phpunit @@ -30,10 +31,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@master - - uses: shivammathur/setup-php@v1 + - uses: shivammathur/setup-php@master with: - php-version: '7.4' + php-version: '8.0' - name: Install dependencies - run: composer install -n --prefer-dist + run: composer update - name: Run License Checker run: bin/license-checker check diff --git a/.gitignore b/.gitignore index e26f45e..6db987f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ composer.phar /vendor/ composer.lock +.phpunit.result.cache diff --git a/Dockerfile b/Dockerfile index fb186f7..12cde7a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM php:cli-alpine +FROM php:8-cli-alpine ENV COMPOSER_HOME /composer ENV COMPOSER_ALLOW_SUPERUSER 1 diff --git a/README.md b/README.md index ebbc534..ea32387 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ pipeline to block merging when a non-verified license is being introduced to the ## Installation Installing should be a breeze thanks to `composer`: +Note that you need PHP 8 to install the latest version (1.x). +If you are using an older version of PHP (7.x), older versions can be installed (0.x). ``` composer require madewithlove/license-checker diff --git a/composer.json b/composer.json index fc999b5..2b13143 100644 --- a/composer.json +++ b/composer.json @@ -12,13 +12,14 @@ "minimum-stability": "stable", "bin": ["bin/license-checker"], "require": { + "php": "^8.0", "symfony/console": "^4.0 || ^5.0", "symfony/process": "^4.0 || ^5.0", "symfony/yaml": "^4.0 || ^5.0" }, "require-dev": { - "phpunit/phpunit": "^8.5 || ^9.0", - "vimeo/psalm": "^3.9" + "phpunit/phpunit": "^9.5", + "vimeo/psalm": "^4.4" }, "autoload": { "psr-4": { diff --git a/src/Commands/CheckLicenses.php b/src/Commands/CheckLicenses.php index 01b5878..b19a014 100644 --- a/src/Commands/CheckLicenses.php +++ b/src/Commands/CheckLicenses.php @@ -18,37 +18,13 @@ class CheckLicenses extends Command { protected static $defaultName = 'check'; - /** - * @var UsedLicensesParser - */ - private $usedLicenseParser; - - /** - * @var AllowedLicensesParser - */ - private $allowedLicensesParser; - - /** - * @var DependencyTree - */ - private $dependencyTree; - - /** - * @var TableRenderer - */ - private $tableRenderer; - public function __construct( - UsedLicensesParser $usedLicensesParser, - AllowedLicensesParser $allowedLicensesParser, - DependencyTree $dependencyTree, - TableRenderer $tableRenderer + private UsedLicensesParser $usedLicensesParser, + private AllowedLicensesParser $allowedLicensesParser, + private DependencyTree $dependencyTree, + private TableRenderer $tableRenderer ) { parent::__construct(); - $this->usedLicenseParser = $usedLicensesParser; - $this->allowedLicensesParser = $allowedLicensesParser; - $this->dependencyTree = $dependencyTree; - $this->tableRenderer = $tableRenderer; } protected function configure(): void @@ -56,12 +32,12 @@ protected function configure(): void $this->setDescription('Check licenses of composer dependencies'); } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); try { - $usedLicenses = $this->usedLicenseParser->parseLicenses(); + $usedLicenses = $this->usedLicensesParser->parseLicenses(); } catch (ProcessFailedException $e) { $output->writeln($e->getMessage()); return 1; @@ -81,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output) foreach ($dependencies as $dependency) { $dependencyCheck = new DependencyCheck($dependency->getName()); foreach ($notAllowedLicenses as $notAllowedLicense) { - $packagesUsingThisLicense = $this->usedLicenseParser->getPackagesWithLicense($notAllowedLicense); + $packagesUsingThisLicense = $this->usedLicensesParser->getPackagesWithLicense($notAllowedLicense); foreach ($packagesUsingThisLicense as $packageUsingThisLicense) { if ($dependency->hasDependency($packageUsingThisLicense) || $dependency->getName() === $packageUsingThisLicense) { $dependencyCheck = $dependencyCheck->addFailedDependency($packageUsingThisLicense, $notAllowedLicense); diff --git a/src/Commands/CountUsedLicenses.php b/src/Commands/CountUsedLicenses.php index bfd1f4f..386fa4e 100644 --- a/src/Commands/CountUsedLicenses.php +++ b/src/Commands/CountUsedLicenses.php @@ -3,7 +3,6 @@ namespace LicenseChecker\Commands; use LicenseChecker\Composer\UsedLicensesParser; -use LicenseChecker\Composer\UsedLicensesRetriever; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -14,16 +13,10 @@ class CountUsedLicenses extends Command { protected static $defaultName = 'count'; - /** - * @var UsedLicensesParser - */ - private $usedLicensesParser; - public function __construct( - UsedLicensesParser $usedLicensesParser + private UsedLicensesParser $usedLicensesParser ) { parent::__construct(); - $this->usedLicensesParser = $usedLicensesParser; } protected function configure(): void @@ -31,7 +24,7 @@ protected function configure(): void $this->setDescription('Count number of dependencies for each license'); } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); $rows = []; diff --git a/src/Commands/GenerateConfig.php b/src/Commands/GenerateConfig.php index a7412f8..a6dbc89 100644 --- a/src/Commands/GenerateConfig.php +++ b/src/Commands/GenerateConfig.php @@ -15,23 +15,11 @@ class GenerateConfig extends Command { protected static $defaultName = 'generate-config'; - /** - * @var AllowedLicensesParser - */ - private $allowedLicensesParser; - - /** - * @var UsedLicensesParser - */ - private $usedLicensesParser; - public function __construct( - AllowedLicensesParser $allowedLicensesParser, - UsedLicensesParser $usedLicensesParser + private AllowedLicensesParser $allowedLicensesParser, + private UsedLicensesParser $usedLicensesParser ) { parent::__construct(); - $this->allowedLicensesParser = $allowedLicensesParser; - $this->usedLicensesParser = $usedLicensesParser; } protected function configure(): void @@ -39,7 +27,7 @@ protected function configure(): void $this->setDescription('Generates allowed licenses config based on used licenses'); } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); diff --git a/src/Commands/ListAllowedLicenses.php b/src/Commands/ListAllowedLicenses.php index b923c86..40c3bee 100644 --- a/src/Commands/ListAllowedLicenses.php +++ b/src/Commands/ListAllowedLicenses.php @@ -12,24 +12,18 @@ class ListAllowedLicenses extends Command { protected static $defaultName = 'allowed'; - /** - * @var AllowedLicensesParser - */ - private $allowedLicensesParser; - - public function __construct(AllowedLicensesParser $allowedLicensesParser) - { + public function __construct( + private AllowedLicensesParser $allowedLicensesParser + ) { parent::__construct(); - $this->allowedLicensesParser = $allowedLicensesParser; } - protected function configure(): void { $this->setDescription('List used licenses of composer dependencies'); } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { try { $allowedLicenses = $this->allowedLicensesParser->getAllowedLicenses(getcwd()); diff --git a/src/Commands/ListUsedLicenses.php b/src/Commands/ListUsedLicenses.php index af2dfd7..66ba764 100644 --- a/src/Commands/ListUsedLicenses.php +++ b/src/Commands/ListUsedLicenses.php @@ -13,16 +13,10 @@ class ListUsedLicenses extends Command { protected static $defaultName = 'used'; - /** - * @var UsedLicensesParser - */ - private $usedLicensesParser; - public function __construct( - UsedLicensesParser $usedLicensesParser + private UsedLicensesParser $usedLicensesParser ) { parent::__construct(); - $this->usedLicensesParser = $usedLicensesParser; } protected function configure(): void @@ -30,7 +24,7 @@ protected function configure(): void $this->setDescription('List used licenses of composer dependencies'); } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { try { $usedLicenses = $this->usedLicensesParser->parseLicenses(); diff --git a/src/Commands/Output/CauseOfFailure.php b/src/Commands/Output/CauseOfFailure.php index 90d3689..4908b64 100644 --- a/src/Commands/Output/CauseOfFailure.php +++ b/src/Commands/Output/CauseOfFailure.php @@ -4,21 +4,10 @@ final class CauseOfFailure { - /** - * @var string - */ - private $name; - - /** - * @var string - */ - private $license; - - public function __construct(string $name, string $license) - { - $this->name = $name; - $this->license = $license; - } + public function __construct( + private string $name, + private string $license + ) {} public function getName(): string { diff --git a/src/Commands/Output/DependencyCheck.php b/src/Commands/Output/DependencyCheck.php index 932a267..829c107 100644 --- a/src/Commands/Output/DependencyCheck.php +++ b/src/Commands/Output/DependencyCheck.php @@ -4,30 +4,18 @@ final class DependencyCheck { - /** - * @var string - */ - private $name; - - /** - * @var bool - */ - private $isAllowed = true; + private bool $isAllowed = true; /** * @var CauseOfFailure[] */ - private $causedBy = []; + private array $causedBy = []; - /** - * @param string $name - */ - public function __construct(string $name) - { - $this->name = $name; - } + public function __construct( + private string $name + ) {} - public function addFailedDependency($dependency, $license): self + public function addFailedDependency(string $dependency, string $license): self { $dependencyCheck = clone $this; $dependencyCheck->isAllowed = false; diff --git a/src/Commands/Output/TableRenderer.php b/src/Commands/Output/TableRenderer.php index 17f5221..e2040d7 100644 --- a/src/Commands/Output/TableRenderer.php +++ b/src/Commands/Output/TableRenderer.php @@ -8,11 +8,10 @@ class TableRenderer { /** * @param DependencyCheck[] $dependencyChecks - * @param SymfonyStyle $io */ - public function renderDependencyChecks(array $dependencyChecks, SymfonyStyle $io) + public function renderDependencyChecks(array $dependencyChecks, SymfonyStyle $io): void { - usort($dependencyChecks, function (DependencyCheck $dependencyCheck, DependencyCheck $other) { + usort($dependencyChecks, function (DependencyCheck $dependencyCheck, DependencyCheck $other): int { return $dependencyCheck->isAllowed() <=> $other->isAllowed(); }); @@ -24,7 +23,6 @@ public function renderDependencyChecks(array $dependencyChecks, SymfonyStyle $io /** * @param DependencyCheck[] $dependencyChecks - * @return bool */ private function hasFailures(array $dependencyChecks): bool { @@ -103,7 +101,6 @@ private function renderAllOkay(array $dependencyChecks): array } /** - * @param DependencyCheck $dependencyCheck * @return string[] */ private function renderAllowedLineWithEmptyFailureCause(DependencyCheck $dependencyCheck): array @@ -116,8 +113,6 @@ private function renderAllowedLineWithEmptyFailureCause(DependencyCheck $depende } /** - * @param DependencyCheck $dependencyCheck - * @param CauseOfFailure $causeOfFailure * @return string[] */ private function renderFailedLineWithCauseOfFailure( @@ -132,7 +127,6 @@ private function renderFailedLineWithCauseOfFailure( } /** - * @param CauseOfFailure $causeOfFailure * @return string[] */ private function renderAdditionalCauseOfFailure(CauseOfFailure $causeOfFailure): array diff --git a/src/Commands/Output/TableRendererTest.php b/src/Commands/Output/TableRendererTest.php index 2c2bd3c..7d311a0 100644 --- a/src/Commands/Output/TableRendererTest.php +++ b/src/Commands/Output/TableRendererTest.php @@ -8,15 +8,8 @@ class TableRendererTest extends TestCase { - /** - * @var SymfonyStyle | MockObject - */ - private $io; - - /** - * @var TableRenderer - */ - private $tableRenderer; + private SymfonyStyle|MockObject $io; + private TableRenderer $tableRenderer; protected function setUp(): void { @@ -27,7 +20,7 @@ protected function setUp(): void /** * @test */ - public function itWillRenderTwoColumnsOnSuccess() + public function itWillRenderTwoColumnsOnSuccess(): void { $this->io->expects($this->once())->method('table')->with( ['','dependency'], @@ -51,7 +44,7 @@ public function itWillRenderTwoColumnsOnSuccess() /** * @test */ - public function itWillListCausingPackagesOnFailure() + public function itWillListCausingPackagesOnFailure(): void { $this->io->expects($this->once())->method('table')->with( ['', 'dependency', 'caused by'], @@ -76,7 +69,7 @@ public function itWillListCausingPackagesOnFailure() /** * @test */ - public function itWillRenderFailingDependenciesFirst() + public function itWillRenderFailingDependenciesFirst(): void { $this->io->expects($this->once())->method('table')->with( ['', 'dependency', 'caused by'], diff --git a/src/Composer/DependencyTree.php b/src/Composer/DependencyTree.php index 74d81ed..736c12c 100644 --- a/src/Composer/DependencyTree.php +++ b/src/Composer/DependencyTree.php @@ -6,15 +6,9 @@ class DependencyTree { - /** - * @var DependencyTreeRetriever - */ - private $retriever; - - public function __construct(DependencyTreeRetriever $dependencyTreeRetriever) - { - $this->retriever = $dependencyTreeRetriever; - } + public function __construct( + private DependencyTreeRetriever $retriever) + {} /** * @return Dependency[] @@ -37,7 +31,6 @@ public function getDependencies(): array } /** - * @param array $subTree * @return string[] */ private function getSubDependencies(array $subTree): array diff --git a/src/Composer/DependencyTreeRetriever.php b/src/Composer/DependencyTreeRetriever.php index 7ca8880..6c2b724 100644 --- a/src/Composer/DependencyTreeRetriever.php +++ b/src/Composer/DependencyTreeRetriever.php @@ -7,11 +7,11 @@ class DependencyTreeRetriever { - private static $output; + private static string $output = ''; public function getDependencyTree(): string { - if (!is_null(self::$output)) { + if (!empty(self::$output)) { return self::$output; } diff --git a/src/Composer/DependencyTreeTest.php b/src/Composer/DependencyTreeTest.php index 3f1f2b8..e0636f2 100644 --- a/src/Composer/DependencyTreeTest.php +++ b/src/Composer/DependencyTreeTest.php @@ -8,15 +8,8 @@ class DependencyTreeTest extends TestCase { - /** - * @var DependencyTreeRetriever | MockObject - */ - private $retriever; - - /** - * @var DependencyTree - */ - private $dependencyTree; + private DependencyTreeRetriever|MockObject $retriever; + private DependencyTree $dependencyTree; protected function setUp(): void { diff --git a/src/Composer/UsedLicenseParserTest.php b/src/Composer/UsedLicenseParserTest.php index 735ff06..486ce6f 100644 --- a/src/Composer/UsedLicenseParserTest.php +++ b/src/Composer/UsedLicenseParserTest.php @@ -7,15 +7,8 @@ class UsedLicenseParserTest extends TestCase { - /** - * @var UsedLicensesParser - */ - private $usedLicensesParser; - - /** - * @var UsedLicensesRetriever | MockObject - */ - private $licenseRetriever; + private UsedLicensesParser $usedLicensesParser; + private UsedLicensesRetriever|MockObject $licenseRetriever; protected function setUp(): void { diff --git a/src/Composer/UsedLicensesParser.php b/src/Composer/UsedLicensesParser.php index 99c2408..6dc2365 100644 --- a/src/Composer/UsedLicensesParser.php +++ b/src/Composer/UsedLicensesParser.php @@ -4,15 +4,9 @@ class UsedLicensesParser { - /** - * @var UsedLicensesRetriever - */ - private $retriever; - - public function __construct(UsedLicensesRetriever $retriever) - { - $this->retriever = $retriever; - } + public function __construct( + private UsedLicensesRetriever $retriever + ) {} /** * @return string[] @@ -33,7 +27,6 @@ public function parseLicenses(): array } /** - * @param string $license * @return string[] */ public function getPackagesWithLicense(string $license): array diff --git a/src/Composer/UsedLicensesRetriever.php b/src/Composer/UsedLicensesRetriever.php index 3742b88..9a56c6d 100644 --- a/src/Composer/UsedLicensesRetriever.php +++ b/src/Composer/UsedLicensesRetriever.php @@ -7,11 +7,11 @@ class UsedLicensesRetriever { - private static $output; + private static string $output = ''; public function getComposerLicenses(): string { - if (!is_null(self::$output)) { + if (!empty(self::$output)) { return self::$output; } diff --git a/src/Configuration/AllowedLicensesParser.php b/src/Configuration/AllowedLicensesParser.php index 2dbe303..1c85d3a 100644 --- a/src/Configuration/AllowedLicensesParser.php +++ b/src/Configuration/AllowedLicensesParser.php @@ -9,7 +9,6 @@ class AllowedLicensesParser private const CONFIG_FILE_NAME = '.allowed-licenses'; /** - * @param string $pathToConfigurationFile * @return string[] */ public function getAllowedLicenses(string $pathToConfigurationFile): array @@ -23,7 +22,7 @@ public function getAllowedLicenses(string $pathToConfigurationFile): array /** * @param string[] $allowedLicenses */ - public function writeConfiguration(array $allowedLicenses) + public function writeConfiguration(array $allowedLicenses): void { if ($this->configurationExists(getcwd())) { throw new ConfigurationExists(); diff --git a/src/Dependency.php b/src/Dependency.php index 35c7e84..eef8616 100644 --- a/src/Dependency.php +++ b/src/Dependency.php @@ -4,20 +4,14 @@ class Dependency { - /** - * @var string - */ - private $name; - /** * @var string[] */ - private $subDependencies = []; + private array $subDependencies = []; - public function __construct(string $name) - { - $this->name = $name; - } + public function __construct( + private string $name + ) {} public function addDependency(string $dependency): self {