From b951626d7e9f4d95c7ac86ad34875fe4d0123de8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BBa=D0=B4=D0=B8=D1=81=D0=BBa=D0=B2=20=D0=9D?= =?UTF-8?q?=D0=BE=D0=B2=D0=B8=D0=BA=D0=BE=D0=B2?= Date: Wed, 21 May 2025 18:45:25 +0300 Subject: [PATCH 1/4] Fix cs github-action Fix Psalm errors by refining null and empty checks and improving type safety chore: upgrade PHP to 8.1, add spiral/code-style, config and CI for code style, update psalm --- .github/workflows/cs.yml | 30 ++++++++++++------- .github/workflows/phpunit.yml | 2 +- .gitignore | 1 + .php-cs-fixer.dist.php | 22 +++++--------- README.md | 2 +- composer.json | 16 ++++++---- src/Exception/RequiredVersionException.php | 4 +-- .../RoadrunnerNotInstalledException.php | 4 +-- src/Exception/VersionCheckerException.php | 4 +-- src/Version/Installed.php | 12 ++++---- src/VersionChecker.php | 6 ++-- 11 files changed, 52 insertions(+), 51 deletions(-) diff --git a/.github/workflows/cs.yml b/.github/workflows/cs.yml index 30dcb45..6487966 100644 --- a/.github/workflows/cs.yml +++ b/.github/workflows/cs.yml @@ -1,14 +1,22 @@ -on: - pull_request: null - push: - branches: - - 1.x +name: PHP-CS-Fixer Check -name: coding-standards +on: [push, pull_request] jobs: - psalm: - uses: spiral/gh-actions/.github/workflows/cs.yml@master - with: - os: >- - ['ubuntu-latest'] + phpcsfixer: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.1' + + - name: Install dependencies + run: composer install --no-progress --no-suggest --prefer-dist + + - name: Run PHP-CS-Fixer dry run + run: | + vendor/bin/php-cs-fixer fix --dry-run -v --diff diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 5a77d0b..eb35453 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -13,6 +13,6 @@ jobs: os: >- ['ubuntu-latest'] php: >- - ['8.0', '8.1', '8.2', '8.3'] + ['8.1', '8.2', '8.3'] stability: >- ['prefer-lowest', 'prefer-stable'] diff --git a/.gitignore b/.gitignore index 2fe23be..11f00b1 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ docs vendor node_modules .php-cs-fixer.cache +/runtime/php-cs-fixer.cache diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 9b1d84a..3ff5ff8 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -2,19 +2,11 @@ declare(strict_types=1); -if (!file_exists(__DIR__.'/src')) { - exit(0); -} +use Spiral\CodeStyle\Builder; -return (new PhpCsFixer\Config()) - ->setRules([ - '@PSR12' => true, - 'ternary_operator_spaces' => false, - ]) - ->setRiskyAllowed(true) - ->setFinder( - (new PhpCsFixer\Finder()) - ->in(__DIR__.'/src') - ->append([__FILE__]) - ) - ->setCacheFile('.php-cs-fixer.cache'); +require_once 'vendor/autoload.php'; + +return Builder::create() + ->include(__DIR__ . '/src') + ->include(__FILE__) + ->build(); diff --git a/README.md b/README.md index dba2cfd..f8b6bb6 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Make sure that your server is configured with following PHP version and extensions: -- PHP 8.0+ +- PHP 8.1+ ## Installation diff --git a/composer.json b/composer.json index 22dd13c..1627fc6 100644 --- a/composer.json +++ b/composer.json @@ -9,15 +9,17 @@ "homepage": "https://github.com/roadrunner-php/version-checker", "license": "MIT", "require": { - "php": "^8.0", - "symfony/process": "^5.4 || ^6.0 || ^7.0", + "php": "^8.1", "composer-runtime-api": "^2.0", - "composer/semver": "^3.3" + "composer/semver": "^3.3", + "symfony/process": "^5.4 || ^6.0 || ^7.0" }, "require-dev": { + "friendsofphp/php-cs-fixer": "^3.8", + "phpcompatibility/php-compatibility": "^9.3", "phpunit/phpunit": "^9.6 || ^10.0", - "vimeo/psalm": "^5.9", - "friendsofphp/php-cs-fixer": "^3.8" + "spiral/code-style": "^2.2", + "vimeo/psalm": "^5.9" }, "autoload": { "psr-4": { @@ -32,7 +34,9 @@ "scripts": { "test": "vendor/bin/phpunit", "psalm": "vendor/bin/psalm --config=psalm.xml ./src", - "cs": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php -vvv --dry-run --using-cache=no" + "cs": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php -vvv --dry-run --using-cache=no", + "cs:diff": "php-cs-fixer fix --dry-run -v --diff", + "cs:fix": "php-cs-fixer fix -v" }, "config": { "sort-packages": true diff --git a/src/Exception/RequiredVersionException.php b/src/Exception/RequiredVersionException.php index d741df7..9346254 100644 --- a/src/Exception/RequiredVersionException.php +++ b/src/Exception/RequiredVersionException.php @@ -4,6 +4,4 @@ namespace RoadRunner\VersionChecker\Exception; -final class RequiredVersionException extends VersionCheckerException -{ -} +final class RequiredVersionException extends VersionCheckerException {} diff --git a/src/Exception/RoadrunnerNotInstalledException.php b/src/Exception/RoadrunnerNotInstalledException.php index 24dfdd0..1d947c6 100644 --- a/src/Exception/RoadrunnerNotInstalledException.php +++ b/src/Exception/RoadrunnerNotInstalledException.php @@ -4,6 +4,4 @@ namespace RoadRunner\VersionChecker\Exception; -final class RoadrunnerNotInstalledException extends VersionCheckerException -{ -} +final class RoadrunnerNotInstalledException extends VersionCheckerException {} diff --git a/src/Exception/VersionCheckerException.php b/src/Exception/VersionCheckerException.php index 9cccac3..9b0a47e 100644 --- a/src/Exception/VersionCheckerException.php +++ b/src/Exception/VersionCheckerException.php @@ -4,6 +4,4 @@ namespace RoadRunner\VersionChecker\Exception; -abstract class VersionCheckerException extends \Exception -{ -} +abstract class VersionCheckerException extends \Exception {} diff --git a/src/Version/Installed.php b/src/Version/Installed.php index 89d3645..8a7f56d 100644 --- a/src/Version/Installed.php +++ b/src/Version/Installed.php @@ -42,16 +42,18 @@ public function __construct( */ public function getInstalledVersion(): string { - if (!empty(self::$cachedVersion)) { + if (self::$cachedVersion != null) { return self::$cachedVersion; } - if (!empty(self::$cachedVersion = $this->getVersionFromEnv())) { - return self::$cachedVersion; + $version = $this->getVersionFromEnv(); + if ($version != null) { + return self::$cachedVersion = $version; } - if (!empty(self::$cachedVersion = $this->getVersionFromConsoleCommand())) { - return self::$cachedVersion; + $version = $this->getVersionFromConsoleCommand(); + if ($version != null) { + return self::$cachedVersion = $version; } throw new RoadrunnerNotInstalledException('Unable to determine RoadRunner version.'); diff --git a/src/VersionChecker.php b/src/VersionChecker.php index 66534df..5981072 100644 --- a/src/VersionChecker.php +++ b/src/VersionChecker.php @@ -39,11 +39,11 @@ public function __construct( */ public function greaterThan(?string $version = null): void { - if (empty($version)) { + if ($version == null) { $version = $this->requiredVersion->getRequiredVersion(); } - if (empty($version)) { + if ($version == null) { throw new RequiredVersionException( 'Unable to determine required RoadRunner version.' . ' Please specify the required version in the `$version` parameter.', @@ -110,7 +110,7 @@ private function getFormattedMessage(string $message, string $installedVersion, { \preg_match('/\bv?(\d+)\.(\d+)\.(\d+)\b/', $version, $matches); - if (!empty($matches[0])) { + if (isset($matches[0]) && $matches[0] != null) { $version = $matches[1] . '.' . $matches[2] . '.' . $matches[3]; } From 62b7093793936fd82b26f2949ecd5f8db574bf9e Mon Sep 17 00:00:00 2001 From: Aleksei Gagarin Date: Wed, 21 May 2025 20:44:25 +0400 Subject: [PATCH 2/4] Apply suggestions from code review --- .github/workflows/phpunit.yml | 2 +- .gitignore | 2 +- composer.json | 4 +--- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index eb35453..ac19e88 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -13,6 +13,6 @@ jobs: os: >- ['ubuntu-latest'] php: >- - ['8.1', '8.2', '8.3'] + ['8.1', '8.2', '8.3', '8.4'] stability: >- ['prefer-lowest', 'prefer-stable'] diff --git a/.gitignore b/.gitignore index 11f00b1..3963707 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,4 @@ docs vendor node_modules .php-cs-fixer.cache -/runtime/php-cs-fixer.cache +/runtime diff --git a/composer.json b/composer.json index 1627fc6..55ed2c5 100644 --- a/composer.json +++ b/composer.json @@ -15,11 +15,10 @@ "symfony/process": "^5.4 || ^6.0 || ^7.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.8", "phpcompatibility/php-compatibility": "^9.3", "phpunit/phpunit": "^9.6 || ^10.0", "spiral/code-style": "^2.2", - "vimeo/psalm": "^5.9" + "vimeo/psalm": "^6.0" }, "autoload": { "psr-4": { @@ -34,7 +33,6 @@ "scripts": { "test": "vendor/bin/phpunit", "psalm": "vendor/bin/psalm --config=psalm.xml ./src", - "cs": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php -vvv --dry-run --using-cache=no", "cs:diff": "php-cs-fixer fix --dry-run -v --diff", "cs:fix": "php-cs-fixer fix -v" }, From f18f7b0dde95a437ebb403dff29b4f76a23007e1 Mon Sep 17 00:00:00 2001 From: Aleksei Gagarin Date: Thu, 22 May 2025 00:19:45 +0400 Subject: [PATCH 3/4] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 55ed2c5..b5e96d9 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "homepage": "https://github.com/roadrunner-php/version-checker", "license": "MIT", "require": { - "php": "^8.1", + "php": ">=8.1", "composer-runtime-api": "^2.0", "composer/semver": "^3.3", "symfony/process": "^5.4 || ^6.0 || ^7.0" From 632c2270566391a2279e6e110c105f66a4edc8fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BBa=D0=B4=D0=B8=D1=81=D0=BBa=D0=B2=20=D0=9D?= =?UTF-8?q?=D0=BE=D0=B2=D0=B8=D0=BA=D0=BE=D0=B2?= Date: Thu, 22 May 2025 20:14:29 +0300 Subject: [PATCH 4/4] =?UTF-8?q?Fix:=20addressed=20MR=20comments=20?= =?UTF-8?q?=E2=80=94=20fixed=20Psalm=20issues=20and=20re-ran=20PHP-CS-Fixe?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cs.yml | 28 ++++++------------- .github/workflows/phpunit.yml | 2 +- .gitignore | 2 +- .php-cs-fixer.dist.php | 7 ++++- README.md | 6 ---- composer.json | 4 +-- psalm.xml | 7 ----- src/Composer/Package.php | 26 ++++++++++++----- src/Environment/Native.php | 1 + src/Exception/UnsupportedVersionException.php | 16 ----------- src/Process/Process.php | 1 + src/Version/Comparator.php | 3 ++ src/Version/Installed.php | 10 ++++--- src/Version/Required.php | 1 + src/VersionChecker.php | 1 - 15 files changed, 49 insertions(+), 66 deletions(-) diff --git a/.github/workflows/cs.yml b/.github/workflows/cs.yml index 6487966..518b472 100644 --- a/.github/workflows/cs.yml +++ b/.github/workflows/cs.yml @@ -1,22 +1,12 @@ -name: PHP-CS-Fixer Check +on: + push: + branches: + - '*' -on: [push, pull_request] +name: Fix Code Style jobs: - phpcsfixer: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: '8.1' - - - name: Install dependencies - run: composer install --no-progress --no-suggest --prefer-dist - - - name: Run PHP-CS-Fixer dry run - run: | - vendor/bin/php-cs-fixer fix --dry-run -v --diff + cs-fix: + permissions: + contents: write + uses: spiral/gh-actions/.github/workflows/cs-fix.yml@master diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index eb35453..ac19e88 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -13,6 +13,6 @@ jobs: os: >- ['ubuntu-latest'] php: >- - ['8.1', '8.2', '8.3'] + ['8.1', '8.2', '8.3', '8.4'] stability: >- ['prefer-lowest', 'prefer-stable'] diff --git a/.gitignore b/.gitignore index 11f00b1..3963707 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,4 @@ docs vendor node_modules .php-cs-fixer.cache -/runtime/php-cs-fixer.cache +/runtime diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 3ff5ff8..4b418a0 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -6,7 +6,12 @@ require_once 'vendor/autoload.php'; + return Builder::create() ->include(__DIR__ . '/src') ->include(__FILE__) - ->build(); + ->build()->setRules([ + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + 'fully_qualified_strict_types' => true, + 'no_unused_imports' => true, + ]); diff --git a/README.md b/README.md index f8b6bb6..e3a8eef 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,6 @@ [![Total Downloads](https://poser.pugx.org/roadrunner-php/version-checker/downloads)](https://packagist.org/roadrunner-php/version-checker/phpunit) -## Requirements - -Make sure that your server is configured with following PHP version and extensions: - -- PHP 8.1+ - ## Installation You can install the package via composer: diff --git a/composer.json b/composer.json index 1627fc6..55ed2c5 100644 --- a/composer.json +++ b/composer.json @@ -15,11 +15,10 @@ "symfony/process": "^5.4 || ^6.0 || ^7.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.8", "phpcompatibility/php-compatibility": "^9.3", "phpunit/phpunit": "^9.6 || ^10.0", "spiral/code-style": "^2.2", - "vimeo/psalm": "^5.9" + "vimeo/psalm": "^6.0" }, "autoload": { "psr-4": { @@ -34,7 +33,6 @@ "scripts": { "test": "vendor/bin/phpunit", "psalm": "vendor/bin/psalm --config=psalm.xml ./src", - "cs": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php -vvv --dry-run --using-cache=no", "cs:diff": "php-cs-fixer fix --dry-run -v --diff", "cs:fix": "php-cs-fixer fix -v" }, diff --git a/psalm.xml b/psalm.xml index 967944d..dab13ea 100644 --- a/psalm.xml +++ b/psalm.xml @@ -13,11 +13,4 @@ - - - - - - - diff --git a/src/Composer/Package.php b/src/Composer/Package.php index f2824c8..93a45a7 100644 --- a/src/Composer/Package.php +++ b/src/Composer/Package.php @@ -11,8 +11,12 @@ final class Package implements PackageInterface { /** * @param non-empty-string $packageName - * @return non-empty-string[] + * + * @return string[] + * + * @psalm-return list */ + #[\Override] public function getRequiredVersions(string $packageName): array { $versions = []; @@ -20,13 +24,21 @@ public function getRequiredVersions(string $packageName): array $path = InstalledVersions::getInstallPath($package); if ($path !== null && \file_exists($path . '/composer.json')) { /** @var array{require?: array} $composerJson */ - $composerJson = \json_decode(\file_get_contents($path . '/composer.json'), true); + $fileContent = \file_get_contents($path . '/composer.json'); + /** @var array|null $composerJson */ + $composerJson = $fileContent === false ? null : \json_decode($fileContent, true); + + if (isset($composerJson['require'][$packageName])) { + /** @var mixed $rawPackage */ + $rawPackage = $composerJson['require'][$packageName]; + + if (is_string($rawPackage) && strlen($rawPackage) > 0) { + assert($rawPackage !== ''); - if ( - isset($composerJson['require'][$packageName]) && - $this->isSupportedVersion($composerJson['require'][$packageName]) - ) { - $versions[] = $this->getMinVersion($composerJson['require'][$packageName]); + if ($this->isSupportedVersion($rawPackage)) { + $versions[] = $this->getMinVersion($rawPackage); + } + } } } } diff --git a/src/Environment/Native.php b/src/Environment/Native.php index 53daee7..bf3fe32 100644 --- a/src/Environment/Native.php +++ b/src/Environment/Native.php @@ -15,6 +15,7 @@ public function __construct( /** * @param non-empty-string $name */ + #[\Override] public function get(string $name, mixed $default = null): mixed { return $this->values[$name] ?? $default; diff --git a/src/Exception/UnsupportedVersionException.php b/src/Exception/UnsupportedVersionException.php index 68b4ec4..ad351bb 100644 --- a/src/Exception/UnsupportedVersionException.php +++ b/src/Exception/UnsupportedVersionException.php @@ -18,20 +18,4 @@ public function __construct( ) { parent::__construct($message); } - - /** - * @return non-empty-string - */ - public function getInstalledVersion(): string - { - return $this->installed; - } - - /** - * @return non-empty-string - */ - public function getRequestedVersion(): string - { - return $this->requested; - } } diff --git a/src/Process/Process.php b/src/Process/Process.php index 1d88c16..c987e40 100644 --- a/src/Process/Process.php +++ b/src/Process/Process.php @@ -8,6 +8,7 @@ final class Process implements ProcessInterface { + #[\Override] public function exec(array $command): string { $process = new \Symfony\Component\Process\Process($command); diff --git a/src/Version/Comparator.php b/src/Version/Comparator.php index a3e6a13..f40387b 100644 --- a/src/Version/Comparator.php +++ b/src/Version/Comparator.php @@ -20,6 +20,7 @@ public function __construct(?VersionParser $parser = null) * @param non-empty-string $requested * @param non-empty-string $installed */ + #[\Override] public function greaterThan(string $requested, string $installed): bool { return SemverComparator::greaterThanOrEqualTo( @@ -32,6 +33,7 @@ public function greaterThan(string $requested, string $installed): bool * @param non-empty-string $requested * @param non-empty-string $installed */ + #[\Override] public function lessThan(string $requested, string $installed): bool { return SemverComparator::lessThanOrEqualTo( @@ -44,6 +46,7 @@ public function lessThan(string $requested, string $installed): bool * @param non-empty-string $requested * @param non-empty-string $installed */ + #[\Override] public function equal(string $requested, string $installed): bool { return SemverComparator::equalTo( diff --git a/src/Version/Installed.php b/src/Version/Installed.php index 8a7f56d..595da77 100644 --- a/src/Version/Installed.php +++ b/src/Version/Installed.php @@ -40,6 +40,7 @@ public function __construct( * * @throws RoadrunnerNotInstalledException */ + #[\Override] public function getInstalledVersion(): string { if (self::$cachedVersion != null) { @@ -60,9 +61,9 @@ public function getInstalledVersion(): string } /** - * @return non-empty-string|null + * @return null|string */ - private function getVersionFromEnv(): ?string + private function getVersionFromEnv(): string|null { /** @var string|null $version */ $version = $this->environment->get(self::ENV_VARIABLE); @@ -75,10 +76,11 @@ private function getVersionFromEnv(): ?string } /** - * @return non-empty-string|null + * @return null|string + * * @throws RoadrunnerNotInstalledException */ - private function getVersionFromConsoleCommand(): ?string + private function getVersionFromConsoleCommand(): string|null { try { $output = $this->process->exec([$this->executablePath, '--version']); diff --git a/src/Version/Required.php b/src/Version/Required.php index 6827eef..2752158 100644 --- a/src/Version/Required.php +++ b/src/Version/Required.php @@ -27,6 +27,7 @@ public function __construct(?PackageInterface $package = null) /** * @return non-empty-string|null */ + #[\Override] public function getRequiredVersion(): ?string { if (self::$cachedVersion !== null) { diff --git a/src/VersionChecker.php b/src/VersionChecker.php index 5981072..ab7e448 100644 --- a/src/VersionChecker.php +++ b/src/VersionChecker.php @@ -114,7 +114,6 @@ private function getFormattedMessage(string $message, string $installedVersion, $version = $matches[1] . '.' . $matches[2] . '.' . $matches[3]; } - /** @var non-empty-string $msg */ $msg = \sprintf($message, $installedVersion, $version); return $msg;