diff --git a/rules.neon b/rules.neon index 9b63b38..704579b 100644 --- a/rules.neon +++ b/rules.neon @@ -2,8 +2,19 @@ parameters: deprecationRulesInstalled: true services: - - - class: PHPStan\Rules\Deprecations\DeprecatedClassHelper + - + class: PHPStan\Rules\Deprecations\DeprecatedClassHelper + + - + class: PHPStan\DependencyInjection\LazyDeprecatedScopeResolverProvider + - + class: PHPStan\Rules\Deprecations\DeprecatedScopeHelper + factory: @PHPStan\DependencyInjection\LazyDeprecatedScopeResolverProvider::get + + - + class: PHPStan\Rules\Deprecations\DefaultDeprecatedScopeResolver + tags: + - phpstan.deprecations.deprecatedScopeResolver rules: - PHPStan\Rules\Deprecations\AccessDeprecatedPropertyRule diff --git a/src/DependencyInjection/LazyDeprecatedScopeResolverProvider.php b/src/DependencyInjection/LazyDeprecatedScopeResolverProvider.php new file mode 100644 index 0000000..467ca51 --- /dev/null +++ b/src/DependencyInjection/LazyDeprecatedScopeResolverProvider.php @@ -0,0 +1,33 @@ +container = $container; + } + + public function get(): DeprecatedScopeHelper + { + if ($this->scopeHelper === null) { + $this->scopeHelper = new DeprecatedScopeHelper( + $this->container->getServicesByTag(self::EXTENSION_TAG) + ); + } + return $this->scopeHelper; + } + +} diff --git a/src/Rules/Deprecations/AccessDeprecatedPropertyRule.php b/src/Rules/Deprecations/AccessDeprecatedPropertyRule.php index 3e1f88b..a05512f 100644 --- a/src/Rules/Deprecations/AccessDeprecatedPropertyRule.php +++ b/src/Rules/Deprecations/AccessDeprecatedPropertyRule.php @@ -21,9 +21,13 @@ class AccessDeprecatedPropertyRule implements Rule /** @var ReflectionProvider */ private $reflectionProvider; - public function __construct(ReflectionProvider $reflectionProvider) + /** @var DeprecatedScopeHelper */ + private $deprecatedScopeHelper; + + public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper) { $this->reflectionProvider = $reflectionProvider; + $this->deprecatedScopeHelper = $deprecatedScopeHelper; } public function getNodeType(): string @@ -33,7 +37,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } diff --git a/src/Rules/Deprecations/AccessDeprecatedStaticPropertyRule.php b/src/Rules/Deprecations/AccessDeprecatedStaticPropertyRule.php index beb9891..224b48a 100644 --- a/src/Rules/Deprecations/AccessDeprecatedStaticPropertyRule.php +++ b/src/Rules/Deprecations/AccessDeprecatedStaticPropertyRule.php @@ -28,10 +28,14 @@ class AccessDeprecatedStaticPropertyRule implements Rule /** @var RuleLevelHelper */ private $ruleLevelHelper; - public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper) + /** @var DeprecatedScopeHelper */ + private $deprecatedScopeHelper; + + public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper, DeprecatedScopeHelper $deprecatedScopeHelper) { $this->reflectionProvider = $reflectionProvider; $this->ruleLevelHelper = $ruleLevelHelper; + $this->deprecatedScopeHelper = $deprecatedScopeHelper; } public function getNodeType(): string @@ -41,7 +45,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } diff --git a/src/Rules/Deprecations/CallToDeprecatedFunctionRule.php b/src/Rules/Deprecations/CallToDeprecatedFunctionRule.php index f8ccefe..857a02d 100644 --- a/src/Rules/Deprecations/CallToDeprecatedFunctionRule.php +++ b/src/Rules/Deprecations/CallToDeprecatedFunctionRule.php @@ -20,9 +20,13 @@ class CallToDeprecatedFunctionRule implements Rule /** @var ReflectionProvider */ private $reflectionProvider; - public function __construct(ReflectionProvider $reflectionProvider) + /** @var DeprecatedScopeHelper */ + private $deprecatedScopeHelper; + + public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper) { $this->reflectionProvider = $reflectionProvider; + $this->deprecatedScopeHelper = $deprecatedScopeHelper; } public function getNodeType(): string @@ -32,7 +36,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } diff --git a/src/Rules/Deprecations/CallToDeprecatedMethodRule.php b/src/Rules/Deprecations/CallToDeprecatedMethodRule.php index 6ec4e0c..998d428 100644 --- a/src/Rules/Deprecations/CallToDeprecatedMethodRule.php +++ b/src/Rules/Deprecations/CallToDeprecatedMethodRule.php @@ -21,9 +21,13 @@ class CallToDeprecatedMethodRule implements Rule /** @var ReflectionProvider */ private $reflectionProvider; - public function __construct(ReflectionProvider $reflectionProvider) + /** @var DeprecatedScopeHelper */ + private $deprecatedScopeHelper; + + public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper) { $this->reflectionProvider = $reflectionProvider; + $this->deprecatedScopeHelper = $deprecatedScopeHelper; } public function getNodeType(): string @@ -33,7 +37,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } diff --git a/src/Rules/Deprecations/CallToDeprecatedStaticMethodRule.php b/src/Rules/Deprecations/CallToDeprecatedStaticMethodRule.php index 8b7d995..6978d59 100644 --- a/src/Rules/Deprecations/CallToDeprecatedStaticMethodRule.php +++ b/src/Rules/Deprecations/CallToDeprecatedStaticMethodRule.php @@ -28,10 +28,14 @@ class CallToDeprecatedStaticMethodRule implements Rule /** @var RuleLevelHelper */ private $ruleLevelHelper; - public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper) + /** @var DeprecatedScopeHelper */ + private $deprecatedScopeHelper; + + public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper, DeprecatedScopeHelper $deprecatedScopeHelper) { $this->reflectionProvider = $reflectionProvider; $this->ruleLevelHelper = $ruleLevelHelper; + $this->deprecatedScopeHelper = $deprecatedScopeHelper; } public function getNodeType(): string @@ -41,7 +45,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } diff --git a/src/Rules/Deprecations/DefaultDeprecatedScopeResolver.php b/src/Rules/Deprecations/DefaultDeprecatedScopeResolver.php new file mode 100644 index 0000000..1480727 --- /dev/null +++ b/src/Rules/Deprecations/DefaultDeprecatedScopeResolver.php @@ -0,0 +1,30 @@ +getClassReflection(); + if ($class !== null && $class->isDeprecated()) { + return true; + } + + $trait = $scope->getTraitReflection(); + if ($trait !== null && $trait->isDeprecated()) { + return true; + } + + $function = $scope->getFunction(); + if ($function !== null && $function->isDeprecated()->yes()) { + return true; + } + + return false; + } + +} diff --git a/src/Rules/Deprecations/DeprecatedScopeHelper.php b/src/Rules/Deprecations/DeprecatedScopeHelper.php index 6b19151..dda3172 100644 --- a/src/Rules/Deprecations/DeprecatedScopeHelper.php +++ b/src/Rules/Deprecations/DeprecatedScopeHelper.php @@ -7,21 +7,23 @@ class DeprecatedScopeHelper { - public static function isScopeDeprecated(Scope $scope): bool - { - $class = $scope->getClassReflection(); - if ($class !== null && $class->isDeprecated()) { - return true; - } + /** @var DeprecatedScopeResolver[] */ + private $resolvers; - $trait = $scope->getTraitReflection(); - if ($trait !== null && $trait->isDeprecated()) { - return true; - } + /** + * @param DeprecatedScopeResolver[] $checkers + */ + public function __construct(array $checkers) + { + $this->resolvers = $checkers; + } - $function = $scope->getFunction(); - if ($function !== null && $function->isDeprecated()->yes()) { - return true; + public function isScopeDeprecated(Scope $scope): bool + { + foreach ($this->resolvers as $checker) { + if ($checker->isScopeDeprecated($scope)) { + return true; + } } return false; diff --git a/src/Rules/Deprecations/DeprecatedScopeResolver.php b/src/Rules/Deprecations/DeprecatedScopeResolver.php new file mode 100644 index 0000000..a6ba171 --- /dev/null +++ b/src/Rules/Deprecations/DeprecatedScopeResolver.php @@ -0,0 +1,12 @@ +reflectionProvider = $reflectionProvider; $this->ruleLevelHelper = $ruleLevelHelper; + $this->deprecatedScopeHelper = $deprecatedScopeHelper; } public function getNodeType(): string @@ -41,7 +45,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } diff --git a/src/Rules/Deprecations/FetchingDeprecatedConstRule.php b/src/Rules/Deprecations/FetchingDeprecatedConstRule.php index 407f904..8b77ce1 100644 --- a/src/Rules/Deprecations/FetchingDeprecatedConstRule.php +++ b/src/Rules/Deprecations/FetchingDeprecatedConstRule.php @@ -19,12 +19,16 @@ class FetchingDeprecatedConstRule implements Rule /** @var ReflectionProvider */ private $reflectionProvider; + /** @var DeprecatedScopeHelper */ + private $deprecatedScopeHelper; + /** @var array */ private $deprecatedConstants = []; - public function __construct(ReflectionProvider $reflectionProvider) + public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper) { $this->reflectionProvider = $reflectionProvider; + $this->deprecatedScopeHelper = $deprecatedScopeHelper; // phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed if (PHP_VERSION_ID >= 70300) { @@ -40,7 +44,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } diff --git a/src/Rules/Deprecations/ImplementationOfDeprecatedInterfaceRule.php b/src/Rules/Deprecations/ImplementationOfDeprecatedInterfaceRule.php index 3b3cda0..cec9187 100644 --- a/src/Rules/Deprecations/ImplementationOfDeprecatedInterfaceRule.php +++ b/src/Rules/Deprecations/ImplementationOfDeprecatedInterfaceRule.php @@ -19,9 +19,13 @@ class ImplementationOfDeprecatedInterfaceRule implements Rule /** @var ReflectionProvider */ private $reflectionProvider; - public function __construct(ReflectionProvider $reflectionProvider) + /** @var DeprecatedScopeHelper */ + private $deprecatedScopeHelper; + + public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper) { $this->reflectionProvider = $reflectionProvider; + $this->deprecatedScopeHelper = $deprecatedScopeHelper; } public function getNodeType(): string @@ -31,7 +35,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } diff --git a/src/Rules/Deprecations/InheritanceOfDeprecatedClassRule.php b/src/Rules/Deprecations/InheritanceOfDeprecatedClassRule.php index 772cb1e..62119a1 100644 --- a/src/Rules/Deprecations/InheritanceOfDeprecatedClassRule.php +++ b/src/Rules/Deprecations/InheritanceOfDeprecatedClassRule.php @@ -19,9 +19,13 @@ class InheritanceOfDeprecatedClassRule implements Rule /** @var ReflectionProvider */ private $reflectionProvider; - public function __construct(ReflectionProvider $reflectionProvider) + /** @var DeprecatedScopeHelper */ + private $deprecatedScopeHelper; + + public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper) { $this->reflectionProvider = $reflectionProvider; + $this->deprecatedScopeHelper = $deprecatedScopeHelper; } public function getNodeType(): string @@ -31,7 +35,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } diff --git a/src/Rules/Deprecations/InstantiationOfDeprecatedClassRule.php b/src/Rules/Deprecations/InstantiationOfDeprecatedClassRule.php index 6450a10..e65564a 100644 --- a/src/Rules/Deprecations/InstantiationOfDeprecatedClassRule.php +++ b/src/Rules/Deprecations/InstantiationOfDeprecatedClassRule.php @@ -26,10 +26,14 @@ class InstantiationOfDeprecatedClassRule implements Rule /** @var RuleLevelHelper */ private $ruleLevelHelper; - public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper) + /** @var DeprecatedScopeHelper */ + private $deprecatedScopeHelper; + + public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper, DeprecatedScopeHelper $deprecatedScopeHelper) { $this->reflectionProvider = $reflectionProvider; $this->ruleLevelHelper = $ruleLevelHelper; + $this->deprecatedScopeHelper = $deprecatedScopeHelper; } public function getNodeType(): string @@ -39,7 +43,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } diff --git a/src/Rules/Deprecations/TypeHintDeprecatedInClassMethodSignatureRule.php b/src/Rules/Deprecations/TypeHintDeprecatedInClassMethodSignatureRule.php index f116572..735b9e3 100644 --- a/src/Rules/Deprecations/TypeHintDeprecatedInClassMethodSignatureRule.php +++ b/src/Rules/Deprecations/TypeHintDeprecatedInClassMethodSignatureRule.php @@ -18,9 +18,13 @@ class TypeHintDeprecatedInClassMethodSignatureRule implements Rule /** @var DeprecatedClassHelper */ private $deprecatedClassHelper; - public function __construct(DeprecatedClassHelper $deprecatedClassHelper) + /** @var DeprecatedScopeHelper */ + private $deprecatedScopeHelper; + + public function __construct(DeprecatedClassHelper $deprecatedClassHelper, DeprecatedScopeHelper $deprecatedScopeHelper) { $this->deprecatedClassHelper = $deprecatedClassHelper; + $this->deprecatedScopeHelper = $deprecatedScopeHelper; } public function getNodeType(): string @@ -30,7 +34,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } diff --git a/src/Rules/Deprecations/TypeHintDeprecatedInClosureSignatureRule.php b/src/Rules/Deprecations/TypeHintDeprecatedInClosureSignatureRule.php index bfb83a1..36f952a 100644 --- a/src/Rules/Deprecations/TypeHintDeprecatedInClosureSignatureRule.php +++ b/src/Rules/Deprecations/TypeHintDeprecatedInClosureSignatureRule.php @@ -18,9 +18,13 @@ class TypeHintDeprecatedInClosureSignatureRule implements Rule /** @var DeprecatedClassHelper */ private $deprecatedClassHelper; - public function __construct(DeprecatedClassHelper $deprecatedClassHelper) + /** @var DeprecatedScopeHelper */ + private $deprecatedScopeHelper; + + public function __construct(DeprecatedClassHelper $deprecatedClassHelper, DeprecatedScopeHelper $deprecatedScopeHelper) { $this->deprecatedClassHelper = $deprecatedClassHelper; + $this->deprecatedScopeHelper = $deprecatedScopeHelper; } public function getNodeType(): string @@ -30,7 +34,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } diff --git a/src/Rules/Deprecations/TypeHintDeprecatedInFunctionSignatureRule.php b/src/Rules/Deprecations/TypeHintDeprecatedInFunctionSignatureRule.php index 9c91ec9..80d8c8d 100644 --- a/src/Rules/Deprecations/TypeHintDeprecatedInFunctionSignatureRule.php +++ b/src/Rules/Deprecations/TypeHintDeprecatedInFunctionSignatureRule.php @@ -19,9 +19,13 @@ class TypeHintDeprecatedInFunctionSignatureRule implements Rule /** @var DeprecatedClassHelper */ private $deprecatedClassHelper; - public function __construct(DeprecatedClassHelper $deprecatedClassHelper) + /** @var DeprecatedScopeHelper */ + private $deprecatedScopeHelper; + + public function __construct(DeprecatedClassHelper $deprecatedClassHelper, DeprecatedScopeHelper $deprecatedScopeHelper) { $this->deprecatedClassHelper = $deprecatedClassHelper; + $this->deprecatedScopeHelper = $deprecatedScopeHelper; } public function getNodeType(): string @@ -31,7 +35,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } diff --git a/src/Rules/Deprecations/UsageOfDeprecatedCastRule.php b/src/Rules/Deprecations/UsageOfDeprecatedCastRule.php index 04ff91f..e6d653a 100644 --- a/src/Rules/Deprecations/UsageOfDeprecatedCastRule.php +++ b/src/Rules/Deprecations/UsageOfDeprecatedCastRule.php @@ -14,6 +14,14 @@ class UsageOfDeprecatedCastRule implements Rule { + /** @var DeprecatedScopeHelper */ + private $deprecatedScopeHelper; + + public function __construct(DeprecatedScopeHelper $deprecatedScopeHelper) + { + $this->deprecatedScopeHelper = $deprecatedScopeHelper; + } + public function getNodeType(): string { return Cast::class; @@ -21,7 +29,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } diff --git a/src/Rules/Deprecations/UsageOfDeprecatedTraitRule.php b/src/Rules/Deprecations/UsageOfDeprecatedTraitRule.php index cd06f7a..80b9431 100644 --- a/src/Rules/Deprecations/UsageOfDeprecatedTraitRule.php +++ b/src/Rules/Deprecations/UsageOfDeprecatedTraitRule.php @@ -20,9 +20,13 @@ class UsageOfDeprecatedTraitRule implements Rule /** @var ReflectionProvider */ private $reflectionProvider; - public function __construct(ReflectionProvider $reflectionProvider) + /** @var DeprecatedScopeHelper */ + private $deprecatedScopeHelper; + + public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper) { $this->reflectionProvider = $reflectionProvider; + $this->deprecatedScopeHelper = $deprecatedScopeHelper; } public function getNodeType(): string @@ -32,7 +36,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } diff --git a/tests/Rules/Deprecations/AccessDeprecatedPropertyRuleTest.php b/tests/Rules/Deprecations/AccessDeprecatedPropertyRuleTest.php index 4b0afa8..9fade8c 100644 --- a/tests/Rules/Deprecations/AccessDeprecatedPropertyRuleTest.php +++ b/tests/Rules/Deprecations/AccessDeprecatedPropertyRuleTest.php @@ -13,7 +13,10 @@ class AccessDeprecatedPropertyRuleTest extends RuleTestCase protected function getRule(): Rule { - return new AccessDeprecatedPropertyRule($this->createReflectionProvider()); + return new AccessDeprecatedPropertyRule( + $this->createReflectionProvider(), + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]) + ); } public function testAccessDeprecatedProperty(): void diff --git a/tests/Rules/Deprecations/AccessDeprecatedStaticPropertyRuleTest.php b/tests/Rules/Deprecations/AccessDeprecatedStaticPropertyRuleTest.php index 75e4cbe..2ad233f 100644 --- a/tests/Rules/Deprecations/AccessDeprecatedStaticPropertyRuleTest.php +++ b/tests/Rules/Deprecations/AccessDeprecatedStaticPropertyRuleTest.php @@ -14,7 +14,11 @@ class AccessDeprecatedStaticPropertyRuleTest extends RuleTestCase protected function getRule(): Rule { - return new AccessDeprecatedStaticPropertyRule($this->createReflectionProvider(), self::getContainer()->getByType(RuleLevelHelper::class)); + return new AccessDeprecatedStaticPropertyRule( + $this->createReflectionProvider(), + self::getContainer()->getByType(RuleLevelHelper::class), + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]) + ); } public function testAccessDeprecatedStaticProperty(): void diff --git a/tests/Rules/Deprecations/CallToDeprecatedFunctionRuleTest.php b/tests/Rules/Deprecations/CallToDeprecatedFunctionRuleTest.php index 5221e31..a174bb0 100644 --- a/tests/Rules/Deprecations/CallToDeprecatedFunctionRuleTest.php +++ b/tests/Rules/Deprecations/CallToDeprecatedFunctionRuleTest.php @@ -13,7 +13,10 @@ class CallToDeprecatedFunctionRuleTest extends RuleTestCase protected function getRule(): Rule { - return new CallToDeprecatedFunctionRule($this->createReflectionProvider()); + return new CallToDeprecatedFunctionRule( + $this->createReflectionProvider(), + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]) + ); } public function testDeprecatedFunctionCall(): void diff --git a/tests/Rules/Deprecations/CallToDeprecatedMethodRuleTest.php b/tests/Rules/Deprecations/CallToDeprecatedMethodRuleTest.php index 58f454d..a018d86 100644 --- a/tests/Rules/Deprecations/CallToDeprecatedMethodRuleTest.php +++ b/tests/Rules/Deprecations/CallToDeprecatedMethodRuleTest.php @@ -13,7 +13,10 @@ class CallToDeprecatedMethodRuleTest extends RuleTestCase protected function getRule(): Rule { - return new CallToDeprecatedMethodRule($this->createReflectionProvider()); + return new CallToDeprecatedMethodRule( + $this->createReflectionProvider(), + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]) + ); } public function testDeprecatedMethodCall(): void diff --git a/tests/Rules/Deprecations/CallToDeprecatedStaticMethodRuleTest.php b/tests/Rules/Deprecations/CallToDeprecatedStaticMethodRuleTest.php index ecc1393..9df6a40 100644 --- a/tests/Rules/Deprecations/CallToDeprecatedStaticMethodRuleTest.php +++ b/tests/Rules/Deprecations/CallToDeprecatedStaticMethodRuleTest.php @@ -14,7 +14,11 @@ class CallToDeprecatedStaticMethodRuleTest extends RuleTestCase protected function getRule(): Rule { - return new CallToDeprecatedStaticMethodRule($this->createReflectionProvider(), self::getContainer()->getByType(RuleLevelHelper::class)); + return new CallToDeprecatedStaticMethodRule( + $this->createReflectionProvider(), + self::getContainer()->getByType(RuleLevelHelper::class), + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]) + ); } public function testDeprecatedStaticMethodCall(): void diff --git a/tests/Rules/Deprecations/CustomDeprecatedScopeResolverTest.php b/tests/Rules/Deprecations/CustomDeprecatedScopeResolverTest.php new file mode 100644 index 0000000..a2e0550 --- /dev/null +++ b/tests/Rules/Deprecations/CustomDeprecatedScopeResolverTest.php @@ -0,0 +1,53 @@ + + */ +final class CustomDeprecatedScopeResolverTest extends RuleTestCase +{ + + protected function getRule(): Rule + { + $customScopeResolver = new class implements DeprecatedScopeResolver + { + + public function isScopeDeprecated(Scope $scope): bool + { + $function = $scope->getFunction(); + return $function !== null + && $function->getDocComment() !== null + && strpos($function->getDocComment(), '@group legacy') !== false; + } + + }; + return new CallToDeprecatedMethodRule( + $this->createReflectionProvider(), + new DeprecatedScopeHelper([ + new DefaultDeprecatedScopeResolver(), + $customScopeResolver, + ]) + ); + } + + public function testCustomScope(): void + { + require_once __DIR__ . '/data/call-to-deprecated-method-definition.php'; + $this->analyse( + [__DIR__ . '/data/custom-deprecation-scope.php'], + [ + [ + 'Call to deprecated method deprecatedFoo() of class CheckDeprecatedMethodCall\Foo.', + 13, + ], + ] + ); + } + +} diff --git a/tests/Rules/Deprecations/FetchingClassConstOfDeprecatedClassRuleTest.php b/tests/Rules/Deprecations/FetchingClassConstOfDeprecatedClassRuleTest.php index a20f815..4a1370c 100644 --- a/tests/Rules/Deprecations/FetchingClassConstOfDeprecatedClassRuleTest.php +++ b/tests/Rules/Deprecations/FetchingClassConstOfDeprecatedClassRuleTest.php @@ -14,7 +14,11 @@ class FetchingClassConstOfDeprecatedClassRuleTest extends RuleTestCase protected function getRule(): Rule { - return new FetchingClassConstOfDeprecatedClassRule($this->createReflectionProvider(), self::getContainer()->getByType(RuleLevelHelper::class)); + return new FetchingClassConstOfDeprecatedClassRule( + $this->createReflectionProvider(), + self::getContainer()->getByType(RuleLevelHelper::class), + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]) + ); } public function testFetchingClassConstOfDeprecatedClass(): void diff --git a/tests/Rules/Deprecations/FetchingDeprecatedConstRuleTest.php b/tests/Rules/Deprecations/FetchingDeprecatedConstRuleTest.php index d71304f..08c95c4 100644 --- a/tests/Rules/Deprecations/FetchingDeprecatedConstRuleTest.php +++ b/tests/Rules/Deprecations/FetchingDeprecatedConstRuleTest.php @@ -15,7 +15,10 @@ class FetchingDeprecatedConstRuleTest extends RuleTestCase protected function getRule(): Rule { - return new FetchingDeprecatedConstRule($this->createReflectionProvider()); + return new FetchingDeprecatedConstRule( + $this->createReflectionProvider(), + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]) + ); } public function testFetchingDeprecatedConst(): void diff --git a/tests/Rules/Deprecations/ImplementationOfDeprecatedInterfaceRuleTest.php b/tests/Rules/Deprecations/ImplementationOfDeprecatedInterfaceRuleTest.php index e7fcc6d..69b78a7 100644 --- a/tests/Rules/Deprecations/ImplementationOfDeprecatedInterfaceRuleTest.php +++ b/tests/Rules/Deprecations/ImplementationOfDeprecatedInterfaceRuleTest.php @@ -13,7 +13,10 @@ class ImplementationOfDeprecatedInterfaceRuleTest extends RuleTestCase protected function getRule(): Rule { - return new ImplementationOfDeprecatedInterfaceRule($this->createReflectionProvider()); + return new ImplementationOfDeprecatedInterfaceRule( + $this->createReflectionProvider(), + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]) + ); } public function testImplementationOfDeprecatedInterfacesInClasses(): void diff --git a/tests/Rules/Deprecations/InheritanceOfDeprecatedClassRuleTest.php b/tests/Rules/Deprecations/InheritanceOfDeprecatedClassRuleTest.php index 44428d7..ae39339 100644 --- a/tests/Rules/Deprecations/InheritanceOfDeprecatedClassRuleTest.php +++ b/tests/Rules/Deprecations/InheritanceOfDeprecatedClassRuleTest.php @@ -13,7 +13,10 @@ class InheritanceOfDeprecatedClassRuleTest extends RuleTestCase protected function getRule(): Rule { - return new InheritanceOfDeprecatedClassRule($this->createReflectionProvider()); + return new InheritanceOfDeprecatedClassRule( + $this->createReflectionProvider(), + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]) + ); } public function testInheritanceOfDeprecatedClassInClasses(): void diff --git a/tests/Rules/Deprecations/InstantiationOfDeprecatedClassRuleTest.php b/tests/Rules/Deprecations/InstantiationOfDeprecatedClassRuleTest.php index 476069e..368fe5c 100644 --- a/tests/Rules/Deprecations/InstantiationOfDeprecatedClassRuleTest.php +++ b/tests/Rules/Deprecations/InstantiationOfDeprecatedClassRuleTest.php @@ -14,7 +14,11 @@ class InstantiationOfDeprecatedClassRuleTest extends RuleTestCase protected function getRule(): Rule { - return new InstantiationOfDeprecatedClassRule($this->createReflectionProvider(), self::getContainer()->getByType(RuleLevelHelper::class)); + return new InstantiationOfDeprecatedClassRule( + $this->createReflectionProvider(), + self::getContainer()->getByType(RuleLevelHelper::class), + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]) + ); } public function testInstantiationOfDeprecatedClass(): void diff --git a/tests/Rules/Deprecations/TypeHintDeprecatedInClassMethodSignatureRuleTest.php b/tests/Rules/Deprecations/TypeHintDeprecatedInClassMethodSignatureRuleTest.php index d007a97..1a49867 100644 --- a/tests/Rules/Deprecations/TypeHintDeprecatedInClassMethodSignatureRuleTest.php +++ b/tests/Rules/Deprecations/TypeHintDeprecatedInClassMethodSignatureRuleTest.php @@ -13,7 +13,10 @@ class TypeHintDeprecatedInClassMethodSignatureRuleTest extends RuleTestCase protected function getRule(): Rule { - return new TypeHintDeprecatedInClassMethodSignatureRule(new DeprecatedClassHelper($this->createReflectionProvider())); + return new TypeHintDeprecatedInClassMethodSignatureRule( + new DeprecatedClassHelper($this->createReflectionProvider()), + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]) + ); } public function test(): void diff --git a/tests/Rules/Deprecations/TypeHintDeprecatedInClosureSignatureRuleTest.php b/tests/Rules/Deprecations/TypeHintDeprecatedInClosureSignatureRuleTest.php index 41d442e..39ba70a 100644 --- a/tests/Rules/Deprecations/TypeHintDeprecatedInClosureSignatureRuleTest.php +++ b/tests/Rules/Deprecations/TypeHintDeprecatedInClosureSignatureRuleTest.php @@ -13,7 +13,10 @@ class TypeHintDeprecatedInClosureSignatureRuleTest extends RuleTestCase protected function getRule(): Rule { - return new TypeHintDeprecatedInClosureSignatureRule(new DeprecatedClassHelper($this->createReflectionProvider())); + return new TypeHintDeprecatedInClosureSignatureRule( + new DeprecatedClassHelper($this->createReflectionProvider()), + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]) + ); } public function test(): void diff --git a/tests/Rules/Deprecations/TypeHintDeprecatedInFunctionSignatureRuleTest.php b/tests/Rules/Deprecations/TypeHintDeprecatedInFunctionSignatureRuleTest.php index a1817ad..7c25a37 100644 --- a/tests/Rules/Deprecations/TypeHintDeprecatedInFunctionSignatureRuleTest.php +++ b/tests/Rules/Deprecations/TypeHintDeprecatedInFunctionSignatureRuleTest.php @@ -13,7 +13,10 @@ class TypeHintDeprecatedInFunctionSignatureRuleTest extends RuleTestCase protected function getRule(): Rule { - return new TypeHintDeprecatedInFunctionSignatureRule(new DeprecatedClassHelper($this->createReflectionProvider())); + return new TypeHintDeprecatedInFunctionSignatureRule( + new DeprecatedClassHelper($this->createReflectionProvider()), + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]) + ); } public function test(): void diff --git a/tests/Rules/Deprecations/UsageOfDeprecatedCastRuleTest.php b/tests/Rules/Deprecations/UsageOfDeprecatedCastRuleTest.php index b592532..d5d5d0b 100644 --- a/tests/Rules/Deprecations/UsageOfDeprecatedCastRuleTest.php +++ b/tests/Rules/Deprecations/UsageOfDeprecatedCastRuleTest.php @@ -13,7 +13,9 @@ class UsageOfDeprecatedCastRuleTest extends RuleTestCase protected function getRule(): Rule { - return new UsageOfDeprecatedCastRule(); + return new UsageOfDeprecatedCastRule( + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]) + ); } public function testUsageOfDeprecatedTrait(): void diff --git a/tests/Rules/Deprecations/UsageOfDeprecatedTraitRuleTest.php b/tests/Rules/Deprecations/UsageOfDeprecatedTraitRuleTest.php index ac410ec..d416b0a 100644 --- a/tests/Rules/Deprecations/UsageOfDeprecatedTraitRuleTest.php +++ b/tests/Rules/Deprecations/UsageOfDeprecatedTraitRuleTest.php @@ -13,7 +13,10 @@ class UsageOfDeprecatedTraitRuleTest extends RuleTestCase protected function getRule(): Rule { - return new UsageOfDeprecatedTraitRule($this->createReflectionProvider()); + return new UsageOfDeprecatedTraitRule( + $this->createReflectionProvider(), + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]) + ); } public function testUsageOfDeprecatedTrait(): void diff --git a/tests/Rules/Deprecations/data/custom-deprecation-scope.php b/tests/Rules/Deprecations/data/custom-deprecation-scope.php new file mode 100644 index 0000000..6e84f0c --- /dev/null +++ b/tests/Rules/Deprecations/data/custom-deprecation-scope.php @@ -0,0 +1,25 @@ +foo(); + $foo->deprecatedFoo(); + } + + /** + * @group legacy + */ + public function testFooGroupedLegacy() + { + $foo = new Foo(); + $foo->foo(); + $foo->deprecatedFoo(); + } +}