Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ec14389

Browse files
committed
Add deprecated scope resolving
Adds the ability to register deprecated scope resolvers for controlling the deprecated scope.
1 parent 0c761b8 commit ec14389

File tree

37 files changed

+342
-59
lines changed

37 files changed

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

‎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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,21 @@
2424
class AccessDeprecatedStaticPropertyRule implements Rule
2525
{
2626

27+
2728
/** @var ReflectionProvider */
2829
private $reflectionProvider;
2930

3031
/** @var RuleLevelHelper */
3132
private $ruleLevelHelper;
3233

33-
public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper)
34+
/** @var DeprecatedScopeHelper */
35+
private $deprecatedScopeHelper;
36+
37+
public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper, DeprecatedScopeHelper $deprecatedScopeHelper)
3438
{
3539
$this->reflectionProvider = $reflectionProvider;
3640
$this->ruleLevelHelper = $ruleLevelHelper;
41+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
3742
}
3843

3944
public function getNodeType(): string
@@ -43,7 +48,7 @@ public function getNodeType(): string
4348

4449
public function processNode(Node $node, Scope $scope): array
4550
{
46-
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) {
51+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
4752
return [];
4853
}
4954

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

‎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
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPStan\Rules\Deprecations;
6+
7+
use PHPStan\Analyser\Scope;
8+
9+
interface DeprecatedScopeResolver
10+
{
11+
public function isScopeDeprecated(Scope $scope): bool;
12+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /