From 9b4b8851fb5d59fd0eed00fbe9c22cfc328e0187 Mon Sep 17 00:00:00 2001 From: Gert de Pagter Date: Sat, 30 May 2020 15:56:56 +0200 Subject: [PATCH] Add a UsageOfDeprecatedCastRule --- rules.neon | 1 + .../UsageOfDeprecatedCastRule.php | 51 +++++++++++++++++++ .../UsageOfDeprecatedCastRuleTest.php | 30 +++++++++++ .../data/usage-of-deprecated-cast.php | 26 ++++++++++ 4 files changed, 108 insertions(+) create mode 100644 src/Rules/Deprecations/UsageOfDeprecatedCastRule.php create mode 100644 tests/Rules/Deprecations/UsageOfDeprecatedCastRuleTest.php create mode 100644 tests/Rules/Deprecations/data/usage-of-deprecated-cast.php diff --git a/rules.neon b/rules.neon index c89787d..a8f89b8 100644 --- a/rules.neon +++ b/rules.neon @@ -17,4 +17,5 @@ rules: - PHPStan\Rules\Deprecations\TypeHintDeprecatedInClassMethodSignatureRule - PHPStan\Rules\Deprecations\TypeHintDeprecatedInClosureSignatureRule - PHPStan\Rules\Deprecations\TypeHintDeprecatedInFunctionSignatureRule + - PHPStan\Rules\Deprecations\UsageOfDeprecatedCastRule - PHPStan\Rules\Deprecations\UsageOfDeprecatedTraitRule diff --git a/src/Rules/Deprecations/UsageOfDeprecatedCastRule.php b/src/Rules/Deprecations/UsageOfDeprecatedCastRule.php new file mode 100644 index 0000000..d42673b --- /dev/null +++ b/src/Rules/Deprecations/UsageOfDeprecatedCastRule.php @@ -0,0 +1,51 @@ + + */ +class UsageOfDeprecatedCastRule implements \PHPStan\Rules\Rule +{ + + public function getNodeType(): string + { + return Cast::class; + } + + public function processNode(Node $node, Scope $scope): array + { + if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + return []; + } + + $castedType = $scope->getType($node->expr); + if (! $castedType->hasMethod('__toString')->yes()) { + return []; + } + $method = $castedType->getMethod('__toString', $scope); + + if (! $method->isDeprecated()->yes()) { + return []; + } + $description = $method->getDeprecatedDescription(); + if ($description === null) { + return [sprintf( + 'Casting class %s to string is deprecated.', + $method->getDeclaringClass()->getName() + )]; + } + + return [sprintf( + "Casting class %s to string is deprecated.:\n%s", + $method->getDeclaringClass()->getName(), + $description + )]; + } + +} diff --git a/tests/Rules/Deprecations/UsageOfDeprecatedCastRuleTest.php b/tests/Rules/Deprecations/UsageOfDeprecatedCastRuleTest.php new file mode 100644 index 0000000..fd205f0 --- /dev/null +++ b/tests/Rules/Deprecations/UsageOfDeprecatedCastRuleTest.php @@ -0,0 +1,30 @@ + + */ +class UsageOfDeprecatedCastRuleTest extends \PHPStan\Testing\RuleTestCase +{ + + protected function getRule(): \PHPStan\Rules\Rule + { + return new UsageOfDeprecatedCastRule(); + } + + public function testUsageOfDeprecatedTrait(): void + { + require_once __DIR__ . '/data/usage-of-deprecated-cast.php'; + $this->analyse( + [__DIR__ . '/data/usage-of-deprecated-cast.php'], + [ + [ + 'Casting class UsageOfDeprecatedCast\Foo to string is deprecated.', + 17, + ], + ] + ); + } + +} diff --git a/tests/Rules/Deprecations/data/usage-of-deprecated-cast.php b/tests/Rules/Deprecations/data/usage-of-deprecated-cast.php new file mode 100644 index 0000000..1bb3444 --- /dev/null +++ b/tests/Rules/Deprecations/data/usage-of-deprecated-cast.php @@ -0,0 +1,26 @@ +