Skip to content

Commit 6d416c7

Browse files
mglamanondrejmirtes
authored andcommitted
Add deprecated scope resolving
Adds the ability to register deprecated scope resolvers for controlling the deprecated scope.
1 parent 94d68d3 commit 6d416c7

37 files changed

+337
-59
lines changed

rules.neon

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@ parameters:
22
deprecationRulesInstalled: true
33

44
services:
5-
-
6-
class: PHPStan\Rules\Deprecations\DeprecatedClassHelper
5+
-
6+
class: PHPStan\Rules\Deprecations\DeprecatedClassHelper
7+
8+
-
9+
class: PHPStan\DependencyInjection\LazyDeprecatedScopeResolverProvider
10+
-
11+
class: PHPStan\Rules\Deprecations\DeprecatedScopeHelper
12+
factory: @PHPStan\DependencyInjection\LazyDeprecatedScopeResolverProvider::get
13+
14+
-
15+
class: PHPStan\Rules\Deprecations\DefaultDeprecatedScopeResolver
16+
tags:
17+
- phpstan.deprecations.deprecatedScopeResolver
718

819
rules:
920
- PHPStan\Rules\Deprecations\AccessDeprecatedPropertyRule
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DependencyInjection;
4+
5+
use PHPStan\Rules\Deprecations\DeprecatedScopeHelper;
6+
7+
final class LazyDeprecatedScopeResolverProvider
8+
{
9+
10+
public const EXTENSION_TAG = 'phpstan.deprecations.deprecatedScopeResolver';
11+
12+
/** @var Container */
13+
private $container;
14+
15+
/** @var DeprecatedScopeHelper */
16+
private $scopeHelper;
17+
18+
public function __construct(Container $container)
19+
{
20+
$this->container = $container;
21+
}
22+
23+
public function get(): DeprecatedScopeHelper
24+
{
25+
if ($this->scopeHelper === null) {
26+
$this->scopeHelper = new DeprecatedScopeHelper(
27+
$this->container->getServicesByTag(self::EXTENSION_TAG)
28+
);
29+
}
30+
return $this->scopeHelper;
31+
}
32+
33+
}

src/Rules/Deprecations/AccessDeprecatedPropertyRule.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ class AccessDeprecatedPropertyRule implements Rule
2121
/** @var ReflectionProvider */
2222
private $reflectionProvider;
2323

24-
public function __construct(ReflectionProvider $reflectionProvider)
24+
/** @var DeprecatedScopeHelper */
25+
private $deprecatedScopeHelper;
26+
27+
public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper)
2528
{
2629
$this->reflectionProvider = $reflectionProvider;
30+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
2731
}
2832

2933
public function getNodeType(): string
@@ -33,7 +37,7 @@ public function getNodeType(): string
3337

3438
public function processNode(Node $node, Scope $scope): array
3539
{
36-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
40+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
3741
return [];
3842
}
3943

src/Rules/Deprecations/AccessDeprecatedStaticPropertyRule.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ class AccessDeprecatedStaticPropertyRule implements Rule
2828
/** @var RuleLevelHelper */
2929
private $ruleLevelHelper;
3030

31-
public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper)
31+
/** @var DeprecatedScopeHelper */
32+
private $deprecatedScopeHelper;
33+
34+
public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper, DeprecatedScopeHelper $deprecatedScopeHelper)
3235
{
3336
$this->reflectionProvider = $reflectionProvider;
3437
$this->ruleLevelHelper = $ruleLevelHelper;
38+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
3539
}
3640

3741
public function getNodeType(): string
@@ -41,7 +45,7 @@ public function getNodeType(): string
4145

4246
public function processNode(Node $node, Scope $scope): array
4347
{
44-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
48+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
4549
return [];
4650
}
4751

src/Rules/Deprecations/CallToDeprecatedFunctionRule.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ class CallToDeprecatedFunctionRule implements Rule
2020
/** @var ReflectionProvider */
2121
private $reflectionProvider;
2222

23-
public function __construct(ReflectionProvider $reflectionProvider)
23+
/** @var DeprecatedScopeHelper */
24+
private $deprecatedScopeHelper;
25+
26+
public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper)
2427
{
2528
$this->reflectionProvider = $reflectionProvider;
29+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
2630
}
2731

2832
public function getNodeType(): string
@@ -32,7 +36,7 @@ public function getNodeType(): string
3236

3337
public function processNode(Node $node, Scope $scope): array
3438
{
35-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
39+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
3640
return [];
3741
}
3842

src/Rules/Deprecations/CallToDeprecatedMethodRule.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ class CallToDeprecatedMethodRule implements Rule
2121
/** @var ReflectionProvider */
2222
private $reflectionProvider;
2323

24-
public function __construct(ReflectionProvider $reflectionProvider)
24+
/** @var DeprecatedScopeHelper */
25+
private $deprecatedScopeHelper;
26+
27+
public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper)
2528
{
2629
$this->reflectionProvider = $reflectionProvider;
30+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
2731
}
2832

2933
public function getNodeType(): string
@@ -33,7 +37,7 @@ public function getNodeType(): string
3337

3438
public function processNode(Node $node, Scope $scope): array
3539
{
36-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
40+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
3741
return [];
3842
}
3943

src/Rules/Deprecations/CallToDeprecatedStaticMethodRule.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ class CallToDeprecatedStaticMethodRule implements Rule
2828
/** @var RuleLevelHelper */
2929
private $ruleLevelHelper;
3030

31-
public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper)
31+
/** @var DeprecatedScopeHelper */
32+
private $deprecatedScopeHelper;
33+
34+
public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper, DeprecatedScopeHelper $deprecatedScopeHelper)
3235
{
3336
$this->reflectionProvider = $reflectionProvider;
3437
$this->ruleLevelHelper = $ruleLevelHelper;
38+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
3539
}
3640

3741
public function getNodeType(): string
@@ -41,7 +45,7 @@ public function getNodeType(): string
4145

4246
public function processNode(Node $node, Scope $scope): array
4347
{
44-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
48+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
4549
return [];
4650
}
4751

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PHPStan\Analyser\Scope;
6+
7+
final class DefaultDeprecatedScopeResolver implements DeprecatedScopeResolver
8+
{
9+
10+
public function isScopeDeprecated(Scope $scope): bool
11+
{
12+
$class = $scope->getClassReflection();
13+
if ($class !== null && $class->isDeprecated()) {
14+
return true;
15+
}
16+
17+
$trait = $scope->getTraitReflection();
18+
if ($trait !== null && $trait->isDeprecated()) {
19+
return true;
20+
}
21+
22+
$function = $scope->getFunction();
23+
if ($function !== null && $function->isDeprecated()->yes()) {
24+
return true;
25+
}
26+
27+
return false;
28+
}
29+
30+
}

src/Rules/Deprecations/DeprecatedScopeHelper.php

+15-13
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@
77
class DeprecatedScopeHelper
88
{
99

10-
public static function isScopeDeprecated(Scope $scope): bool
11-
{
12-
$class = $scope->getClassReflection();
13-
if ($class !== null && $class->isDeprecated()) {
14-
return true;
15-
}
10+
/** @var DeprecatedScopeResolver[] */
11+
private $resolvers;
1612

17-
$trait = $scope->getTraitReflection();
18-
if ($trait !== null && $trait->isDeprecated()) {
19-
return true;
20-
}
13+
/**
14+
* @param DeprecatedScopeResolver[] $checkers
15+
*/
16+
public function __construct(array $checkers)
17+
{
18+
$this->resolvers = $checkers;
19+
}
2120

22-
$function = $scope->getFunction();
23-
if ($function !== null && $function->isDeprecated()->yes()) {
24-
return true;
21+
public function isScopeDeprecated(Scope $scope): bool
22+
{
23+
foreach ($this->resolvers as $checker) {
24+
if ($checker->isScopeDeprecated($scope)) {
25+
return true;
26+
}
2527
}
2628

2729
return false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PHPStan\Analyser\Scope;
6+
7+
interface DeprecatedScopeResolver
8+
{
9+
10+
public function isScopeDeprecated(Scope $scope): bool;
11+
12+
}

src/Rules/Deprecations/FetchingClassConstOfDeprecatedClassRule.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ class FetchingClassConstOfDeprecatedClassRule implements Rule
2828
/** @var RuleLevelHelper */
2929
private $ruleLevelHelper;
3030

31-
public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper)
31+
/** @var DeprecatedScopeHelper */
32+
private $deprecatedScopeHelper;
33+
34+
public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper, DeprecatedScopeHelper $deprecatedScopeHelper)
3235
{
3336
$this->reflectionProvider = $reflectionProvider;
3437
$this->ruleLevelHelper = $ruleLevelHelper;
38+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
3539
}
3640

3741
public function getNodeType(): string
@@ -41,7 +45,7 @@ public function getNodeType(): string
4145

4246
public function processNode(Node $node, Scope $scope): array
4347
{
44-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
48+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
4549
return [];
4650
}
4751

src/Rules/Deprecations/FetchingDeprecatedConstRule.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ class FetchingDeprecatedConstRule implements Rule
1919
/** @var ReflectionProvider */
2020
private $reflectionProvider;
2121

22+
/** @var DeprecatedScopeHelper */
23+
private $deprecatedScopeHelper;
24+
2225
/** @var array<string,string> */
2326
private $deprecatedConstants = [];
2427

25-
public function __construct(ReflectionProvider $reflectionProvider)
28+
public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper)
2629
{
2730
$this->reflectionProvider = $reflectionProvider;
31+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
2832

2933
// phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
3034
if (PHP_VERSION_ID >= 70300) {
@@ -40,7 +44,7 @@ public function getNodeType(): string
4044

4145
public function processNode(Node $node, Scope $scope): array
4246
{
43-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
47+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
4448
return [];
4549
}
4650

src/Rules/Deprecations/ImplementationOfDeprecatedInterfaceRule.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ class ImplementationOfDeprecatedInterfaceRule implements Rule
1919
/** @var ReflectionProvider */
2020
private $reflectionProvider;
2121

22-
public function __construct(ReflectionProvider $reflectionProvider)
22+
/** @var DeprecatedScopeHelper */
23+
private $deprecatedScopeHelper;
24+
25+
public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper)
2326
{
2427
$this->reflectionProvider = $reflectionProvider;
28+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
2529
}
2630

2731
public function getNodeType(): string
@@ -31,7 +35,7 @@ public function getNodeType(): string
3135

3236
public function processNode(Node $node, Scope $scope): array
3337
{
34-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
38+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
3539
return [];
3640
}
3741

src/Rules/Deprecations/InheritanceOfDeprecatedClassRule.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ class InheritanceOfDeprecatedClassRule implements Rule
1919
/** @var ReflectionProvider */
2020
private $reflectionProvider;
2121

22-
public function __construct(ReflectionProvider $reflectionProvider)
22+
/** @var DeprecatedScopeHelper */
23+
private $deprecatedScopeHelper;
24+
25+
public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper)
2326
{
2427
$this->reflectionProvider = $reflectionProvider;
28+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
2529
}
2630

2731
public function getNodeType(): string
@@ -31,7 +35,7 @@ public function getNodeType(): string
3135

3236
public function processNode(Node $node, Scope $scope): array
3337
{
34-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
38+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
3539
return [];
3640
}
3741

src/Rules/Deprecations/InstantiationOfDeprecatedClassRule.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ class InstantiationOfDeprecatedClassRule implements Rule
2626
/** @var RuleLevelHelper */
2727
private $ruleLevelHelper;
2828

29-
public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper)
29+
/** @var DeprecatedScopeHelper */
30+
private $deprecatedScopeHelper;
31+
32+
public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper, DeprecatedScopeHelper $deprecatedScopeHelper)
3033
{
3134
$this->reflectionProvider = $reflectionProvider;
3235
$this->ruleLevelHelper = $ruleLevelHelper;
36+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
3337
}
3438

3539
public function getNodeType(): string
@@ -39,7 +43,7 @@ public function getNodeType(): string
3943

4044
public function processNode(Node $node, Scope $scope): array
4145
{
42-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
46+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
4347
return [];
4448
}
4549

0 commit comments

Comments
 (0)