From 3de03177361c919eb19086174cb32173aa7c6f0d Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Wed, 18 Dec 2024 16:29:54 +0100 Subject: [PATCH 1/5] Upgrade dependencies, drop PHP 8.2, Laravel 10, PHPStan 1 support --- composer.json | 21 ++++++++++----------- phpstan.neon.dist | 1 - 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index d32353a..036d3ff 100644 --- a/composer.json +++ b/composer.json @@ -16,22 +16,21 @@ } ], "require": { - "php": "^8.2", - "illuminate/support": "^10.0 || ^11.0", + "php": "^8.3", + "illuminate/support": "^11.0", "nesbot/carbon": "^2.0 || ^3.0", - "phpstan/phpstan": "^1.10.57" + "phpstan/phpstan": "^2.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.7", - "larastan/larastan": "^2.0.1", - "nunomaduro/collision": "^7.10 || ^8.0", - "orchestra/testbench": "^8.0 || ^9.0", - "pestphp/pest": "^2.24", - "pestphp/pest-plugin-laravel": "^2.2", + "larastan/larastan": "^3.0", + "nunomaduro/collision": "^8.0", + "orchestra/testbench": "^9.0", + "pestphp/pest": "^3.0", + "pestphp/pest-plugin-laravel": "^3.0", "phpstan/extension-installer": "^1.1", - "phpstan/phpstan-deprecation-rules": "^1.0", - "phpstan/phpstan-phpunit": "^1.0", - "phpunit/phpunit": "^10.4" + "phpstan/phpstan-deprecation-rules": "^2.0", + "phpstan/phpstan-phpunit": "^2.0" }, "autoload": { "psr-4": { diff --git a/phpstan.neon.dist b/phpstan.neon.dist index e862c6c..3aa3e30 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -7,5 +7,4 @@ parameters: - src checkOctaneCompatibility: true checkModelProperties: true - checkMissingIterableValueType: true From 75155713ce2da3387ddbd8de549823f30a661319 Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Fri, 20 Dec 2024 14:54:25 +0100 Subject: [PATCH 2/5] Upgrade to PHPStan 2.0 --- src/Rules/CarbonCopyRule.php | 8 +++-- src/Rules/NoAliasUseRule.php | 16 +++++++--- src/Rules/NoMutableDateTimeStaticCallRule.php | 20 ++++++++---- src/Rules/NoMutableDateTimeUseRule.php | 32 ++++++++++++------- src/Rules/NoNewMutableDateTimeRule.php | 20 ++++++++---- tests/ExecutesLarastan.php | 1 + 6 files changed, 64 insertions(+), 33 deletions(-) diff --git a/src/Rules/CarbonCopyRule.php b/src/Rules/CarbonCopyRule.php index 4da4760..67a223b 100644 --- a/src/Rules/CarbonCopyRule.php +++ b/src/Rules/CarbonCopyRule.php @@ -7,6 +7,8 @@ use PhpParser\Node\Expr\MethodCall; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; +use PHPStan\Rules\RuleError; +use PHPStan\Rules\RuleErrorBuilder; use PHPStan\Rules\RuleLevelHelper; use PHPStan\Type\ErrorType; use PHPStan\Type\ObjectType; @@ -30,7 +32,7 @@ public function getNodeType(): string /** * @param MethodCall $node - * @return array + * @return list */ public function processNode(Node $node, Scope $scope): array { @@ -67,7 +69,9 @@ static function (Type $type) use ($name): bool { } return [ - "Usage of \\Carbon\\CarbonInterface::{$name}() is prohibited. You should use CarbonImmutable and remove {$name}() call.", + RuleErrorBuilder::message("Usage of \\Carbon\\CarbonInterface::{$name}() is prohibited. You should use CarbonImmutable and remove {$name}() call.") + ->identifier('carbon.copy') + ->build(), ]; } } diff --git a/src/Rules/NoAliasUseRule.php b/src/Rules/NoAliasUseRule.php index 9fb1178..7ba5916 100644 --- a/src/Rules/NoAliasUseRule.php +++ b/src/Rules/NoAliasUseRule.php @@ -4,9 +4,11 @@ use Illuminate\Foundation\AliasLoader; use PhpParser\Node; -use PhpParser\Node\Stmt\UseUse; +use PhpParser\Node\UseItem; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; +use PHPStan\Rules\RuleError; +use PHPStan\Rules\RuleErrorBuilder; /** * @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\UseUse> @@ -23,12 +25,12 @@ public function __construct() public function getNodeType(): string { - return UseUse::class; + return UseItem::class; } /** - * @param UseUse $node - * @return array + * @param UseItem $node + * @return list */ public function processNode(Node $node, Scope $scope): array { @@ -40,6 +42,10 @@ public function processNode(Node $node, Scope $scope): array return []; } - return ["Usage of alias {$usedClass} is prohibited, prefer the use of {$aliasedClass}."]; + return [ + RuleErrorBuilder::message("Usage of alias {$usedClass} is prohibited, prefer the use of {$aliasedClass}.") + ->identifier('alias.use') + ->build(), + ]; } } diff --git a/src/Rules/NoMutableDateTimeStaticCallRule.php b/src/Rules/NoMutableDateTimeStaticCallRule.php index 0eeb81b..c489841 100644 --- a/src/Rules/NoMutableDateTimeStaticCallRule.php +++ b/src/Rules/NoMutableDateTimeStaticCallRule.php @@ -6,13 +6,21 @@ use PhpParser\Node; use PhpParser\Node\Expr\StaticCall; use PHPStan\Analyser\Scope; +use PHPStan\Reflection\ReflectionProvider; use PHPStan\Rules\Rule; +use PHPStan\Rules\RuleError; +use PHPStan\Rules\RuleErrorBuilder; /** * @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\StaticCall> */ class NoMutableDateTimeStaticCallRule implements Rule { + public function __construct( + private ReflectionProvider $reflectionProvider, + ) { + } + public function getNodeType(): string { return StaticCall::class; @@ -20,7 +28,7 @@ public function getNodeType(): string /** * @param StaticCall $node - * @return array + * @return list */ public function processNode(Node $node, Scope $scope): array { @@ -35,16 +43,14 @@ public function processNode(Node $node, Scope $scope): array } return [ - "Static calls of mutable DateTime is forbidden, currently using {$class}.", + RuleErrorBuilder::message("Static calls of mutable DateTime is forbidden, currently using {$class}.") + ->identifier('mutable.datetime.static.call') + ->build(), ]; } private function isAllowed(string $class): bool { - if ($class === DateTime::class) { - return false; - } - - return !is_subclass_of($class, DateTime::class); + return !$this->reflectionProvider->getClass($class)->is(DateTime::class); } } diff --git a/src/Rules/NoMutableDateTimeUseRule.php b/src/Rules/NoMutableDateTimeUseRule.php index b0e4cb2..5777766 100644 --- a/src/Rules/NoMutableDateTimeUseRule.php +++ b/src/Rules/NoMutableDateTimeUseRule.php @@ -4,41 +4,49 @@ use DateTime; use PhpParser\Node; -use PhpParser\Node\Stmt\UseUse; +use PhpParser\Node\UseItem; use PHPStan\Analyser\Scope; +use PHPStan\Reflection\ReflectionProvider; use PHPStan\Rules\Rule; +use PHPStan\Rules\RuleError; +use PHPStan\Rules\RuleErrorBuilder; /** * @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\UseUse> */ class NoMutableDateTimeUseRule implements Rule { + public function __construct( + private ReflectionProvider $reflectionProvider, + ) { + } + public function getNodeType(): string { - return UseUse::class; + return UseItem::class; } /** - * @param UseUse $node - * @return array + * @param UseItem $node + * @return list */ public function processNode(Node $node, Scope $scope): array { - $usedClass = $node->name->toString(); + $class = $node->name->toString(); - if ($this->isAllowed($usedClass)) { + if ($this->isAllowed($class)) { return []; } - return ["Usage of mutable DateTime is forbidden, currently using {$usedClass}."]; + return [ + RuleErrorBuilder::message("Usage of mutable DateTime is forbidden, currently using {$class}.") + ->identifier('mutable.datetime.use') + ->build(), + ]; } private function isAllowed(string $class): bool { - if ($class === DateTime::class) { - return false; - } - - return !is_subclass_of($class, DateTime::class); + return !$this->reflectionProvider->getClass($class)->is(DateTime::class); } } diff --git a/src/Rules/NoNewMutableDateTimeRule.php b/src/Rules/NoNewMutableDateTimeRule.php index 8c8117c..f7ceff0 100644 --- a/src/Rules/NoNewMutableDateTimeRule.php +++ b/src/Rules/NoNewMutableDateTimeRule.php @@ -6,13 +6,21 @@ use PhpParser\Node; use PhpParser\Node\Expr\New_; use PHPStan\Analyser\Scope; +use PHPStan\Reflection\ReflectionProvider; use PHPStan\Rules\Rule; +use PHPStan\Rules\RuleError; +use PHPStan\Rules\RuleErrorBuilder; /** * @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\New_> */ class NoNewMutableDateTimeRule implements Rule { + public function __construct( + private ReflectionProvider $reflectionProvider, + ) { + } + public function getNodeType(): string { return New_::class; @@ -20,7 +28,7 @@ public function getNodeType(): string /** * @param New_ $node - * @return array + * @return list */ public function processNode(Node $node, Scope $scope): array { @@ -35,16 +43,14 @@ public function processNode(Node $node, Scope $scope): array } return [ - "Instanciations of mutable DateTime is forbidden, currently using {$class}.", + RuleErrorBuilder::message("Instanciations of mutable DateTime is forbidden, currently using {$class}.") + ->identifier('mutable.datetime.new') + ->build(), ]; } private function isAllowed(string $class): bool { - if ($class === DateTime::class) { - return false; - } - - return !is_subclass_of($class, DateTime::class); + return !$this->reflectionProvider->getClass($class)->is(DateTime::class); } } diff --git a/tests/ExecutesLarastan.php b/tests/ExecutesLarastan.php index c122569..27ad47c 100644 --- a/tests/ExecutesLarastan.php +++ b/tests/ExecutesLarastan.php @@ -3,6 +3,7 @@ namespace Soyhuce\PhpstanExtension\Tests; use function dirname; +use function sprintf; trait ExecutesLarastan { From 6e0ba25ff9d3bbe12bb5a80d9aa49750d273cbeb Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Fri, 20 Dec 2024 14:55:39 +0100 Subject: [PATCH 3/5] Run tests and PHPStan on PHP 8.4 --- .github/workflows/phpstan.yml | 2 +- .github/workflows/run-tests.yml | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index d9306d6..b0cde6a 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -16,7 +16,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.3' + php-version: '8.4' coverage: none - name: Install composer dependencies diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index ea2fad5..962dea6 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -13,18 +13,16 @@ jobs: fail-fast: true matrix: os: [ ubuntu-latest ] - php: [ 8.2, 8.3 ] - laravel: [ 10.*, 11.* ] + php: [ 8.3, 8.4 ] + laravel: [ ^11.0 ] carbon: [ ^2.0, ^3.0 ] stability: [ prefer-lowest, prefer-stable ] include: - - laravel: 10.* - testbench: 8.* - - laravel: 11.* - testbench: 9.* + - laravel: ^11.0 + testbench: ^9.0 exclude: - - laravel: 10.* - carbon: ^3.0 + - php: 8.4 + stability: prefer-lowest name: P${{ matrix.php }} - L${{ matrix.laravel }} - C${{ matrix.carbon }} - ${{ matrix.stability }} - ${{ matrix.os }} From 450cf1a7b0ec735bdf319fd9f0f53c972c12c5e4 Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Fri, 20 Dec 2024 14:57:07 +0100 Subject: [PATCH 4/5] Fix badges --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 464cbce..6a1b6df 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # Extra rules for phpstan analysis [![Latest Version on Packagist](https://img.shields.io/packagist/v/soyhuce/phpstan-extension.svg?style=flat-square)](https://packagist.org/packages/soyhuce/phpstan-extension) -[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/soyhuce/phpstan-extension/run-tests?label=tests)](https://github.com/soyhuce/phpstan-extension/actions?query=workflow%3Arun-tests+branch%3Amain) -[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/soyhuce/phpstan-extension/Check%20&%20fix%20styling?label=code%20style)](https://github.com/soyhuce/phpstan-extension/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain) -[![GitHub PHPStan Action Status](https://img.shields.io/github/workflow/status/soyhuce/phpstan-extension/PHPStan?label=phpstan)](https://github.com/soyhuce/phpstan-extension/actions?query=workflow%3APHPStan+branch%3Amain) +[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/soyhuce/phpstan-extension/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/soyhuce/phpstan-extension/actions?query=workflow%3Arun-tests+branch%3Amain) +[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/soyhuce/phpstan-extension/php-cs-fixer.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/soyhuce/phpstan-extension/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) +[![GitHub PHPStan Action Status](https://img.shields.io/github/actions/workflow/status/soyhuce/phpstan-extension/phpstan.yml?branch=main&label=phpstan)](https://github.com/soyhuce/phpstan-extension/actions?query=workflow%3APHPStan+branch%3Amain) [![Total Downloads](https://img.shields.io/packagist/dt/soyhuce/phpstan-extension.svg?style=flat-square)](https://packagist.org/packages/soyhuce/phpstan-extension) Strict rules for PHPStan and helpers for Laravel From 68291f079797446637f70ab5df5fa79aa6754ec5 Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Fri, 20 Dec 2024 14:57:54 +0100 Subject: [PATCH 5/5] Exclude carbon 2 with php 8.4 --- .github/workflows/run-tests.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 962dea6..023619b 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -23,6 +23,9 @@ jobs: exclude: - php: 8.4 stability: prefer-lowest + - php: 8.4 + carbon: ^2.0 + name: P${{ matrix.php }} - L${{ matrix.laravel }} - C${{ matrix.carbon }} - ${{ matrix.stability }} - ${{ matrix.os }}