Skip to content

Commit 7d78404

Browse files
mglamanondrejmirtes
authored andcommitted
Add deprecated scope resolving
Adds the ability to register deprecated scope resolvers for controlling the deprecated scope.
1 parent 0c761b8 commit 7d78404

37 files changed

+337
-59
lines changed

rules.neon

Lines changed: 13 additions & 2 deletions
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
Lines changed: 33 additions & 0 deletions
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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ class AccessDeprecatedPropertyRule implements Rule
2323
/** @var ReflectionProvider */
2424
private $reflectionProvider;
2525

26-
public function __construct(ReflectionProvider $reflectionProvider)
26+
/** @var DeprecatedScopeHelper */
27+
private $deprecatedScopeHelper;
28+
29+
public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper)
2730
{
2831
$this->reflectionProvider = $reflectionProvider;
32+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
2933
}
3034

3135
public function getNodeType(): string
@@ -35,7 +39,7 @@ public function getNodeType(): string
3539

3640
public function processNode(Node $node, Scope $scope): array
3741
{
38-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
42+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
3943
return [];
4044
}
4145

src/Rules/Deprecations/AccessDeprecatedStaticPropertyRule.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ class AccessDeprecatedStaticPropertyRule implements Rule
3030
/** @var RuleLevelHelper */
3131
private $ruleLevelHelper;
3232

33-
public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper)
33+
/** @var DeprecatedScopeHelper */
34+
private $deprecatedScopeHelper;
35+
36+
public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper, DeprecatedScopeHelper $deprecatedScopeHelper)
3437
{
3538
$this->reflectionProvider = $reflectionProvider;
3639
$this->ruleLevelHelper = $ruleLevelHelper;
40+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
3741
}
3842

3943
public function getNodeType(): string
@@ -43,7 +47,7 @@ public function getNodeType(): string
4347

4448
public function processNode(Node $node, Scope $scope): array
4549
{
46-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
50+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
4751
return [];
4852
}
4953

src/Rules/Deprecations/CallToDeprecatedFunctionRule.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ class CallToDeprecatedFunctionRule 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/CallToDeprecatedMethodRule.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ class CallToDeprecatedMethodRule implements Rule
2323
/** @var ReflectionProvider */
2424
private $reflectionProvider;
2525

26-
public function __construct(ReflectionProvider $reflectionProvider)
26+
/** @var DeprecatedScopeHelper */
27+
private $deprecatedScopeHelper;
28+
29+
public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper)
2730
{
2831
$this->reflectionProvider = $reflectionProvider;
32+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
2933
}
3034

3135
public function getNodeType(): string
@@ -35,7 +39,7 @@ public function getNodeType(): string
3539

3640
public function processNode(Node $node, Scope $scope): array
3741
{
38-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
42+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
3943
return [];
4044
}
4145

src/Rules/Deprecations/CallToDeprecatedStaticMethodRule.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ class CallToDeprecatedStaticMethodRule implements Rule
3030
/** @var RuleLevelHelper */
3131
private $ruleLevelHelper;
3232

33-
public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper)
33+
/** @var DeprecatedScopeHelper */
34+
private $deprecatedScopeHelper;
35+
36+
public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper, DeprecatedScopeHelper $deprecatedScopeHelper)
3437
{
3538
$this->reflectionProvider = $reflectionProvider;
3639
$this->ruleLevelHelper = $ruleLevelHelper;
40+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
3741
}
3842

3943
public function getNodeType(): string
@@ -43,7 +47,7 @@ public function getNodeType(): string
4347

4448
public function processNode(Node $node, Scope $scope): array
4549
{
46-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
50+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
4751
return [];
4852
}
4953

Lines changed: 30 additions & 0 deletions
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

Lines changed: 15 additions & 13 deletions
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;
Lines changed: 12 additions & 0 deletions
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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ class FetchingClassConstOfDeprecatedClassRule implements Rule
2929
/** @var RuleLevelHelper */
3030
private $ruleLevelHelper;
3131

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

3842
public function getNodeType(): string
@@ -42,7 +46,7 @@ public function getNodeType(): string
4246

4347
public function processNode(Node $node, Scope $scope): array
4448
{
45-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
49+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
4650
return [];
4751
}
4852

src/Rules/Deprecations/FetchingDeprecatedConstRule.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ class FetchingDeprecatedConstRule implements Rule
2020
/** @var ReflectionProvider */
2121
private $reflectionProvider;
2222

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

26-
public function __construct(ReflectionProvider $reflectionProvider)
29+
public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper)
2730
{
2831
$this->reflectionProvider = $reflectionProvider;
32+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
2933

3034
// phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
3135
if (PHP_VERSION_ID >= 70300) {
@@ -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/ImplementationOfDeprecatedInterfaceRule.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ class ImplementationOfDeprecatedInterfaceRule 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/InheritanceOfDeprecatedClassRule.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ class InheritanceOfDeprecatedClassRule 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/InstantiationOfDeprecatedClassRule.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ class InstantiationOfDeprecatedClassRule implements Rule
2727
/** @var RuleLevelHelper */
2828
private $ruleLevelHelper;
2929

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

3640
public function getNodeType(): string
@@ -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

0 commit comments

Comments
 (0)