Skip to content

Commit 0494a38

Browse files
committed
Take advantage of RestrictedMethodUsageExtension
1 parent 96f9357 commit 0494a38

8 files changed

+113
-121
lines changed

Diff for: composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"require": {
99
"php": "^7.4 || ^8.0",
10-
"phpstan/phpstan": "^2.0"
10+
"phpstan/phpstan": "^2.1.13"
1111
},
1212
"require-dev": {
1313
"php-parallel-lint/php-parallel-lint": "^1.2",

Diff for: rules.neon

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ services:
1919
-
2020
class: PHPStan\Rules\Deprecations\CallWithDeprecatedIniOptionRule
2121

22+
-
23+
class: PHPStan\Rules\Deprecations\RestrictedDeprecatedMethodUsageExtension
24+
tags:
25+
- phpstan.restrictedMethodUsageExtension
26+
2227
rules:
2328
- PHPStan\Rules\Deprecations\AccessDeprecatedPropertyRule
2429
- PHPStan\Rules\Deprecations\AccessDeprecatedStaticPropertyRule
2530
- PHPStan\Rules\Deprecations\CallToDeprecatedFunctionRule
26-
- PHPStan\Rules\Deprecations\CallToDeprecatedMethodRule
2731
- PHPStan\Rules\Deprecations\CallToDeprecatedStaticMethodRule
2832
- PHPStan\Rules\Deprecations\FetchingClassConstOfDeprecatedClassRule
2933
- PHPStan\Rules\Deprecations\FetchingDeprecatedConstRule

Diff for: src/Rules/Deprecations/CallToDeprecatedMethodRule.php

-92
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PHPStan\Analyser\Scope;
6+
use PHPStan\Reflection\ExtendedMethodReflection;
7+
use PHPStan\Rules\RestrictedUsage\RestrictedMethodUsageExtension;
8+
use PHPStan\Rules\RestrictedUsage\RestrictedUsage;
9+
use function sprintf;
10+
use function strtolower;
11+
12+
class RestrictedDeprecatedMethodUsageExtension implements RestrictedMethodUsageExtension
13+
{
14+
15+
private DeprecatedScopeHelper $deprecatedScopeHelper;
16+
17+
public function __construct(DeprecatedScopeHelper $deprecatedScopeHelper)
18+
{
19+
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
20+
}
21+
22+
public function isRestrictedMethodUsage(
23+
ExtendedMethodReflection $methodReflection,
24+
Scope $scope
25+
): ?RestrictedUsage
26+
{
27+
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
28+
return null;
29+
}
30+
31+
if (!$methodReflection->isDeprecated()->yes()) {
32+
return null;
33+
}
34+
35+
$description = $methodReflection->getDeprecatedDescription();
36+
if ($description === null) {
37+
return RestrictedUsage::create(
38+
sprintf(
39+
'Call to deprecated method %s() of %s %s.',
40+
$methodReflection->getName(),
41+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
42+
$methodReflection->getDeclaringClass()->getName(),
43+
),
44+
'method.deprecated',
45+
);
46+
}
47+
48+
return RestrictedUsage::create(
49+
sprintf(
50+
"Call to deprecated method %s() of %s %s:\n%s",
51+
$methodReflection->getName(),
52+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
53+
$methodReflection->getDeclaringClass()->getName(),
54+
$description,
55+
),
56+
'method.deprecated',
57+
);
58+
}
59+
60+
}

Diff for: tests/Rules/Deprecations/CallToDeprecatedMethodRuleTest.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@
22

33
namespace PHPStan\Rules\Deprecations;
44

5+
use PHPStan\Rules\RestrictedUsage\RestrictedMethodUsageRule;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Testing\RuleTestCase;
78

89
/**
9-
* @extends RuleTestCase<CallToDeprecatedMethodRule>
10+
* @extends RuleTestCase<RestrictedMethodUsageRule>
1011
*/
1112
class CallToDeprecatedMethodRuleTest extends RuleTestCase
1213
{
1314

1415
protected function getRule(): Rule
1516
{
16-
return new CallToDeprecatedMethodRule(
17-
$this->createReflectionProvider(),
18-
new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]),
19-
);
17+
return self::getContainer()->getByType(RestrictedMethodUsageRule::class);
2018
}
2119

2220
public function testDeprecatedMethodCall(): void
@@ -53,4 +51,12 @@ public function testDeprecatedMethodCall(): void
5351
);
5452
}
5553

54+
public static function getAdditionalConfigFiles(): array
55+
{
56+
return [
57+
__DIR__ . '/../../../rules.neon',
58+
...parent::getAdditionalConfigFiles(),
59+
];
60+
}
61+
5662
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PHPStan\Analyser\Scope;
6+
use function strpos;
7+
8+
class CustomDeprecatedScopeResolver implements DeprecatedScopeResolver
9+
{
10+
11+
public function isScopeDeprecated(Scope $scope): bool
12+
{
13+
$function = $scope->getFunction();
14+
return $function !== null
15+
&& $function->getDocComment() !== null
16+
&& strpos($function->getDocComment(), '@group legacy') !== false;
17+
}
18+
19+
}

Diff for: tests/Rules/Deprecations/CustomDeprecatedScopeResolverTest.php

+12-22
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,19 @@
22

33
namespace PHPStan\Rules\Deprecations;
44

5-
use PHPStan\Analyser\Scope;
5+
use PHPStan\Rules\RestrictedUsage\RestrictedMethodUsageRule;
66
use PHPStan\Rules\Rule;
77
use PHPStan\Testing\RuleTestCase;
8-
use function strpos;
98

109
/**
11-
* @extends RuleTestCase<CallToDeprecatedMethodRule>
10+
* @extends RuleTestCase<RestrictedMethodUsageRule>
1211
*/
1312
final class CustomDeprecatedScopeResolverTest extends RuleTestCase
1413
{
1514

1615
protected function getRule(): Rule
1716
{
18-
$customScopeResolver = new class implements DeprecatedScopeResolver
19-
{
20-
21-
public function isScopeDeprecated(Scope $scope): bool
22-
{
23-
$function = $scope->getFunction();
24-
return $function !== null
25-
&& $function->getDocComment() !== null
26-
&& strpos($function->getDocComment(), '@group legacy') !== false;
27-
}
28-
29-
};
30-
return new CallToDeprecatedMethodRule(
31-
$this->createReflectionProvider(),
32-
new DeprecatedScopeHelper([
33-
new DefaultDeprecatedScopeResolver(),
34-
$customScopeResolver,
35-
]),
36-
);
17+
return self::getContainer()->getByType(RestrictedMethodUsageRule::class);
3718
}
3819

3920
public function testCustomScope(): void
@@ -50,4 +31,13 @@ public function testCustomScope(): void
5031
);
5132
}
5233

34+
public static function getAdditionalConfigFiles(): array
35+
{
36+
return [
37+
__DIR__ . '/../../../rules.neon',
38+
__DIR__ . '/custom-deprecated-scope.neon',
39+
...parent::getAdditionalConfigFiles(),
40+
];
41+
}
42+
5343
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
-
3+
class: PHPStan\Rules\Deprecations\CustomDeprecatedScopeResolver
4+
tags:
5+
- phpstan.deprecations.deprecatedScopeResolver

0 commit comments

Comments
 (0)