From 75155713ce2da3387ddbd8de549823f30a661319 Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Fri, 20 Dec 2024 14:54:25 +0100 Subject: [PATCH] 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 {