Skip to content

Commit 15f1d89

Browse files
committed
RestrictedMethodUsageExtension is called for static methods too
1 parent 0494a38 commit 15f1d89

5 files changed

+78
-155
lines changed

rules.neon

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ rules:
2828
- PHPStan\Rules\Deprecations\AccessDeprecatedPropertyRule
2929
- PHPStan\Rules\Deprecations\AccessDeprecatedStaticPropertyRule
3030
- PHPStan\Rules\Deprecations\CallToDeprecatedFunctionRule
31-
- PHPStan\Rules\Deprecations\CallToDeprecatedStaticMethodRule
3231
- PHPStan\Rules\Deprecations\FetchingClassConstOfDeprecatedClassRule
3332
- PHPStan\Rules\Deprecations\FetchingDeprecatedConstRule
3433
- PHPStan\Rules\Deprecations\ImplementationOfDeprecatedInterfaceRule

src/Rules/Deprecations/CallToDeprecatedStaticMethodRule.php

-133
This file was deleted.

src/Rules/Deprecations/RestrictedDeprecatedMethodUsageExtension.php

+37-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,41 @@ public function isRestrictedMethodUsage(
2828
return null;
2929
}
3030

31+
if ($methodReflection->getDeclaringClass()->isDeprecated()) {
32+
$class = $methodReflection->getDeclaringClass();
33+
$classDescription = $class->getDeprecatedDescription();
34+
if ($classDescription === null) {
35+
return RestrictedUsage::create(
36+
sprintf(
37+
'Call to method %s() of deprecated %s %s.',
38+
$methodReflection->getName(),
39+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
40+
$methodReflection->getDeclaringClass()->getName(),
41+
),
42+
sprintf(
43+
'%s.deprecated%s',
44+
$methodReflection->isStatic() ? 'staticMethod' : 'method',
45+
$methodReflection->getDeclaringClass()->getClassTypeDescription(),
46+
),
47+
);
48+
}
49+
50+
return RestrictedUsage::create(
51+
sprintf(
52+
"Call to method %s() of deprecated %s %s:\n%s",
53+
$methodReflection->getName(),
54+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
55+
$methodReflection->getDeclaringClass()->getName(),
56+
$classDescription,
57+
),
58+
sprintf(
59+
'%s.deprecated%s',
60+
$methodReflection->isStatic() ? 'staticMethod' : 'method',
61+
$methodReflection->getDeclaringClass()->getClassTypeDescription(),
62+
),
63+
);
64+
}
65+
3166
if (!$methodReflection->isDeprecated()->yes()) {
3267
return null;
3368
}
@@ -41,7 +76,7 @@ public function isRestrictedMethodUsage(
4176
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
4277
$methodReflection->getDeclaringClass()->getName(),
4378
),
44-
'method.deprecated',
79+
sprintf('%s.deprecated', $methodReflection->isStatic() ? 'staticMethod' : 'method'),
4580
);
4681
}
4782

@@ -53,7 +88,7 @@ public function isRestrictedMethodUsage(
5388
$methodReflection->getDeclaringClass()->getName(),
5489
$description,
5590
),
56-
'method.deprecated',
91+
sprintf('%s.deprecated', $methodReflection->isStatic() ? 'staticMethod' : 'method'),
5792
);
5893
}
5994

tests/Rules/Deprecations/CallToDeprecatedStaticMethodRuleTest.php

+15-19
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@
22

33
namespace PHPStan\Rules\Deprecations;
44

5+
use PHPStan\Rules\RestrictedUsage\RestrictedStaticMethodUsageRule;
56
use PHPStan\Rules\Rule;
6-
use PHPStan\Rules\RuleLevelHelper;
77
use PHPStan\Testing\RuleTestCase;
88

99
/**
10-
* @extends RuleTestCase<CallToDeprecatedStaticMethodRule>
10+
* @extends RuleTestCase<RestrictedStaticMethodUsageRule>
1111
*/
1212
class CallToDeprecatedStaticMethodRuleTest extends RuleTestCase
1313
{
1414

1515
protected function getRule(): Rule
1616
{
17-
return new CallToDeprecatedStaticMethodRule(
18-
$this->createReflectionProvider(),
19-
self::getContainer()->getByType(RuleLevelHelper::class),
20-
new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]),
21-
);
17+
return self::getContainer()->getByType(RestrictedStaticMethodUsageRule::class);
2218
}
2319

2420
public function testDeprecatedStaticMethodCall(): void
@@ -40,27 +36,19 @@ public function testDeprecatedStaticMethodCall(): void
4036
9,
4137
],
4238
[
43-
'Call to method foo() of deprecated class CheckDeprecatedStaticMethodCall\Foo.',
39+
'Call to method foo() of deprecated class CheckDeprecatedStaticMethodCall\DeprecatedBar.',
4440
11,
4541
],
4642
[
47-
'Call to method deprecatedFoo() of deprecated class CheckDeprecatedStaticMethodCall\Foo.',
48-
12,
49-
],
50-
[
51-
'Call to deprecated method deprecatedFoo() of class CheckDeprecatedStaticMethodCall\Foo.',
43+
'Call to method deprecatedFoo() of deprecated class CheckDeprecatedStaticMethodCall\DeprecatedBar.',
5244
12,
5345
],
5446
[
55-
'Call to method deprecatedFoo2() of deprecated class CheckDeprecatedStaticMethodCall\Foo.',
56-
13,
57-
],
58-
[
59-
'Call to deprecated method deprecatedFoo2() of class CheckDeprecatedStaticMethodCall\Foo.',
47+
'Call to method deprecatedFoo2() of deprecated class CheckDeprecatedStaticMethodCall\DeprecatedBar.',
6048
13,
6149
],
6250
[
63-
"Call to method foo() of deprecated class CheckDeprecatedStaticMethodCall\Foo:\nDo not touch this at all.",
51+
"Call to method foo() of deprecated class CheckDeprecatedStaticMethodCall\DeprecatedBaz:\nDo not touch this at all.",
6452
15,
6553
],
6654
[
@@ -91,4 +79,12 @@ public function testDeprecatedStaticMethodCall(): void
9179
);
9280
}
9381

82+
public static function getAdditionalConfigFiles(): array
83+
{
84+
return [
85+
__DIR__ . '/../../../rules.neon',
86+
...parent::getAdditionalConfigFiles(),
87+
];
88+
}
89+
9490
}

tests/Rules/Deprecations/data/call-to-deprecated-static-method-definition.php

+26
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ public static function deprecatedFoo()
5252
class DeprecatedBar extends Foo
5353
{
5454

55+
public static function foo()
56+
{
57+
58+
}
59+
60+
/**
61+
* @deprecated
62+
*/
63+
public static function deprecatedFoo()
64+
{
65+
66+
}
67+
68+
/**
69+
* @deprecated
70+
*/
71+
public static function deprecatedFoo2()
72+
{
73+
74+
}
75+
5576
}
5677

5778
/**
@@ -60,4 +81,9 @@ class DeprecatedBar extends Foo
6081
class DeprecatedBaz extends Foo
6182
{
6283

84+
public static function foo()
85+
{
86+
87+
}
88+
6389
}

0 commit comments

Comments
 (0)