From 25f0baef8dce5bc1fab7ff1446483125f5127ac1 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Tue, 1 Oct 2024 14:14:48 +0200 Subject: [PATCH] Use phpstan-deprecation-rules --- composer.json | 1 + composer.lock | 49 ++++++++++++++++++- phpstan.neon.dist | 1 + .../EnforceClosureParamNativeTypehintRule.php | 2 +- ...EnforceIteratorToArrayPreserveKeysRule.php | 2 +- .../ForbidCheckedExceptionInCallableRule.php | 6 +-- ...idCheckedExceptionInYieldingMethodRule.php | 2 +- src/Rule/ForbidNotNormalizedTypeRule.php | 6 +-- src/Rule/ForbidProtectedEnumMethodRule.php | 2 +- .../ForbidReturnValueInYieldingMethodRule.php | 2 +- src/Rule/RequirePreviousExceptionPassRule.php | 2 +- ...UselessPrivatePropertyDefaultValueRule.php | 2 +- .../UselessPrivatePropertyNullabilityRule.php | 2 +- 13 files changed, 64 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index 44c9dc6..f83bf19 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ "editorconfig-checker/editorconfig-checker": "^10.6.0", "ergebnis/composer-normalize": "^2.28", "nette/neon": "^3.3.1", + "phpstan/phpstan-deprecation-rules": "^1.2", "phpstan/phpstan-phpunit": "^1.4.0", "phpstan/phpstan-strict-rules": "^1.6.0", "phpunit/phpunit": "^9.5.20", diff --git a/composer.lock b/composer.lock index 9aa5bad..7a57c68 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "00033ad4202097cc32b2c8047b71e707", + "content-hash": "c96a6052cb31cb9438f71055604c9630", "packages": [ { "name": "phpstan/phpstan", @@ -1303,6 +1303,53 @@ }, "time": "2024-05-06T12:04:23+00:00" }, + { + "name": "phpstan/phpstan-deprecation-rules", + "version": "1.2.1", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan-deprecation-rules.git", + "reference": "f94d246cc143ec5a23da868f8f7e1393b50eaa82" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan-deprecation-rules/zipball/f94d246cc143ec5a23da868f8f7e1393b50eaa82", + "reference": "f94d246cc143ec5a23da868f8f7e1393b50eaa82", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.12" + }, + "require-dev": { + "php-parallel-lint/php-parallel-lint": "^1.2", + "phpstan/phpstan-phpunit": "^1.0", + "phpunit/phpunit": "^9.5" + }, + "type": "phpstan-extension", + "extra": { + "phpstan": { + "includes": [ + "rules.neon" + ] + } + }, + "autoload": { + "psr-4": { + "PHPStan\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan rules for detecting usage of deprecated classes, methods, properties, constants and traits.", + "support": { + "issues": "https://github.com/phpstan/phpstan-deprecation-rules/issues", + "source": "https://github.com/phpstan/phpstan-deprecation-rules/tree/1.2.1" + }, + "time": "2024-09-11T15:52:35+00:00" + }, { "name": "phpstan/phpstan-phpunit", "version": "1.4.0", diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 5d98a82..4828ae6 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -4,6 +4,7 @@ includes: - ./vendor/phpstan/phpstan-strict-rules/rules.neon - ./vendor/phpstan/phpstan-phpunit/extension.neon - ./vendor/phpstan/phpstan-phpunit/rules.neon + - ./vendor/phpstan/phpstan-deprecation-rules/rules.neon - ./vendor/shipmonk/dead-code-detector/rules.neon - ./rules.neon diff --git a/src/Rule/EnforceClosureParamNativeTypehintRule.php b/src/Rule/EnforceClosureParamNativeTypehintRule.php index 7a6d583..6a00e57 100644 --- a/src/Rule/EnforceClosureParamNativeTypehintRule.php +++ b/src/Rule/EnforceClosureParamNativeTypehintRule.php @@ -74,7 +74,7 @@ public function processNode( $errors[] = RuleErrorBuilder::message("Missing parameter typehint for {$type} parameter \${$param->var->name}.") ->identifier('shipmonk.unknownClosureParamType') - ->line($param->getLine()) + ->line($param->getStartLine()) ->build(); } diff --git a/src/Rule/EnforceIteratorToArrayPreserveKeysRule.php b/src/Rule/EnforceIteratorToArrayPreserveKeysRule.php index 6bc396f..8a022da 100644 --- a/src/Rule/EnforceIteratorToArrayPreserveKeysRule.php +++ b/src/Rule/EnforceIteratorToArrayPreserveKeysRule.php @@ -52,7 +52,7 @@ public function processNode(Node $node, Scope $scope): array } return [RuleErrorBuilder::message('Calling iterator_to_array without 2nd parameter $preserve_keys. Default value true might cause failures or data loss.') - ->line($node->getLine()) + ->line($node->getStartLine()) ->identifier('shipmonk.iteratorToArrayWithoutPreserveKeys') ->build()]; } diff --git a/src/Rule/ForbidCheckedExceptionInCallableRule.php b/src/Rule/ForbidCheckedExceptionInCallableRule.php index 6b43c2c..e7b68c6 100644 --- a/src/Rule/ForbidCheckedExceptionInCallableRule.php +++ b/src/Rule/ForbidCheckedExceptionInCallableRule.php @@ -159,7 +159,7 @@ public function processFirstClassCallable( } $errors = []; - $line = $callNode->getLine(); + $line = $callNode->getStartLine(); if ($callNode instanceof MethodCall && $callNode->name instanceof Identifier) { $callerType = $scope->getType($callNode->var); @@ -208,7 +208,7 @@ public function processClosure( $errors[] = $this->buildError( $exceptionClass, 'closure', - $throwPoint->getNode()->getLine(), + $throwPoint->getNode()->getStartLine(), $this->callablesInArguments[$nodeHash] ?? null, ); } @@ -257,7 +257,7 @@ static function (): void { $errors[] = $this->buildError( $exceptionClass, 'arrow function', - $throwPoint->getNode()->getLine(), + $throwPoint->getNode()->getStartLine(), $this->callablesInArguments[$nodeHash] ?? null, ); } diff --git a/src/Rule/ForbidCheckedExceptionInYieldingMethodRule.php b/src/Rule/ForbidCheckedExceptionInYieldingMethodRule.php index 1ef8ef8..5bfd871 100644 --- a/src/Rule/ForbidCheckedExceptionInYieldingMethodRule.php +++ b/src/Rule/ForbidCheckedExceptionInYieldingMethodRule.php @@ -51,7 +51,7 @@ public function processNode( foreach ($throwPoint->getType()->getObjectClassNames() as $exceptionClass) { if ($this->exceptionTypeResolver->isCheckedException($exceptionClass, $throwPoint->getScope())) { $errors[] = RuleErrorBuilder::message("Throwing checked exception $exceptionClass in yielding method is denied as it gets thrown upon Generator iteration") - ->line($throwPoint->getNode()->getLine()) + ->line($throwPoint->getNode()->getStartLine()) ->identifier('shipmonk.checkedExceptionInYieldingMethod') ->build(); } diff --git a/src/Rule/ForbidNotNormalizedTypeRule.php b/src/Rule/ForbidNotNormalizedTypeRule.php index 6d8d799..b3105f7 100644 --- a/src/Rule/ForbidNotNormalizedTypeRule.php +++ b/src/Rule/ForbidNotNormalizedTypeRule.php @@ -530,7 +530,7 @@ private function processMultiTypePhpParserNode( if ($typeA->isSuperTypeOf($typeB)->yes()) { $errors[] = RuleErrorBuilder::message("Found non-normalized type {$multiTypeNodeString} for {$identification}: {$typeNodeBString} is a subtype of {$typeNodeAString}.") - ->line($multiTypeNode->getLine()) + ->line($multiTypeNode->getStartLine()) ->identifier('shipmonk.nonNormalizedType') ->build(); continue; @@ -538,7 +538,7 @@ private function processMultiTypePhpParserNode( if ($typeB->isSuperTypeOf($typeA)->yes()) { $errors[] = RuleErrorBuilder::message("Found non-normalized type {$multiTypeNodeString} for {$identification}: {$typeNodeAString} is a subtype of {$typeNodeBString}.") - ->line($multiTypeNode->getLine()) + ->line($multiTypeNode->getStartLine()) ->identifier('shipmonk.nonNormalizedType') ->build(); } @@ -645,7 +645,7 @@ private function getPhpDocLine(PhpParserNode $node, PhpDocRootNode $phpDocNode): $phpDoc = $node->getDocComment(); if ($phpDocTagLine === null || $phpDoc === null) { - return $node->getLine(); + return $node->getStartLine(); } return $phpDoc->getStartLine() + $phpDocTagLine - 1; diff --git a/src/Rule/ForbidProtectedEnumMethodRule.php b/src/Rule/ForbidProtectedEnumMethodRule.php index 636987c..5e50d06 100644 --- a/src/Rule/ForbidProtectedEnumMethodRule.php +++ b/src/Rule/ForbidProtectedEnumMethodRule.php @@ -38,7 +38,7 @@ public function processNode(Node $node, Scope $scope): array && !$classMethod->isDeclaredInTrait() ) { $errors[] = RuleErrorBuilder::message('Protected methods within enum makes no sense as you cannot extend them anyway.') - ->line($classMethod->getLine()) + ->line($classMethod->getStartLine()) ->identifier('shipmonk.protectedEnumMethod') ->build(); } diff --git a/src/Rule/ForbidReturnValueInYieldingMethodRule.php b/src/Rule/ForbidReturnValueInYieldingMethodRule.php index b379353..5e1d45f 100644 --- a/src/Rule/ForbidReturnValueInYieldingMethodRule.php +++ b/src/Rule/ForbidReturnValueInYieldingMethodRule.php @@ -73,7 +73,7 @@ public function processNode( : 'function'; $errors[] = RuleErrorBuilder::message("Returned value from yielding $callType can be accessed only via Generator::getReturn, $suffix.") - ->line($returnNode->getLine()) + ->line($returnNode->getStartLine()) ->identifier('shipmonk.returnValueFromYieldingMethod') ->build(); } diff --git a/src/Rule/RequirePreviousExceptionPassRule.php b/src/Rule/RequirePreviousExceptionPassRule.php index 6d8cc19..171d9b9 100644 --- a/src/Rule/RequirePreviousExceptionPassRule.php +++ b/src/Rule/RequirePreviousExceptionPassRule.php @@ -142,7 +142,7 @@ private function processExceptionCreation( if (!$passed && $accepts) { $exceptionName = $caughtExceptionVariableName === null ? "({$caughtExceptionType->describe(VerbosityLevel::typeOnly())})" : "\${$caughtExceptionVariableName}"; $error = RuleErrorBuilder::message("Exception {$exceptionName} not passed as previous to {$this->printer->prettyPrintExpr($node)}") - ->line($node->getLine()) + ->line($node->getStartLine()) ->identifier('shipmonk.missingPreviousException') ->build(); return [$error]; diff --git a/src/Rule/UselessPrivatePropertyDefaultValueRule.php b/src/Rule/UselessPrivatePropertyDefaultValueRule.php index 9db73c8..a5d73e0 100644 --- a/src/Rule/UselessPrivatePropertyDefaultValueRule.php +++ b/src/Rule/UselessPrivatePropertyDefaultValueRule.php @@ -70,7 +70,7 @@ public function processNode(Node $node, Scope $scope): array if (isset($noDefaultValueNeededProperties[$propertyName])) { $errors[] = RuleErrorBuilder::message("Property {$className}::{$propertyName} has useless default value (overwritten in constructor)") - ->line($property->getLine()) + ->line($property->getStartLine()) ->identifier('shipmonk.uselessPrivatePropertyDefaultValue') ->build(); } diff --git a/src/Rule/UselessPrivatePropertyNullabilityRule.php b/src/Rule/UselessPrivatePropertyNullabilityRule.php index 6786be2..28212dc 100644 --- a/src/Rule/UselessPrivatePropertyNullabilityRule.php +++ b/src/Rule/UselessPrivatePropertyNullabilityRule.php @@ -93,7 +93,7 @@ public function processNode(Node $node, Scope $scope): array if ($definitionHasTypehint && $definitionIsNullable && !$nullIsAssigned && !$hasNullDefaultValue && !$isUninitialized) { $errors[] = RuleErrorBuilder::message("Property {$className}::{$propertyName} is defined as nullable, but null is never assigned") - ->line($property->getLine()) + ->line($property->getStartLine()) ->identifier('shipmonk.uselessPrivatePropertyNullability') ->build(); }