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 04527b3

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

File tree

37 files changed

+337
-59
lines changed

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+
}

0 commit comments

Comments
(0)

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