Skip to content

Commit e5d941d

Browse files
committed
Restore original CallToDeprecatedStaticMethodRuleTest
1 parent 0deffb7 commit e5d941d

6 files changed

+87
-33
lines changed

phpstan-baseline.neon

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ parameters:
2424
count: 1
2525
path: tests/Rules/Deprecations/InstantiationOfDeprecatedClassRuleTest.php
2626

27+
-
28+
message: '#^Accessing PHPStan\\Rules\\Methods\\CallStaticMethodsRule\:\:class is not covered by backward compatibility promise\. The class might change in a minor PHPStan version\.$#'
29+
identifier: phpstanApi.classConstant
30+
count: 1
31+
path: tests/Rules/Deprecations/RestrictedDeprecatedClassNameUsageExtensionTest.php
32+
2733
-
2834
message: '#^Accessing PHPStan\\Rules\\Classes\\ExistingClassInTraitUseRule\:\:class is not covered by backward compatibility promise\. The class might change in a minor PHPStan version\.$#'
2935
identifier: phpstanApi.classConstant

src/Rules/Deprecations/RestrictedDeprecatedClassNameUsageExtension.php

+27
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,33 @@ public function isRestrictedClassNameUsage(
203203
);
204204
}
205205

206+
if ($location->value === ClassNameUsageLocation::STATIC_METHOD_CALL) {
207+
$method = $location->getMethod();
208+
if ($method !== null) {
209+
if ($method->isDeprecated()->yes() || $method->getDeclaringClass()->isDeprecated()) {
210+
return null;
211+
}
212+
}
213+
}
214+
215+
if ($location->value === ClassNameUsageLocation::STATIC_PROPERTY_ACCESS) {
216+
$property = $location->getProperty();
217+
if ($property !== null) {
218+
if ($property->isDeprecated()->yes() || $property->getDeclaringClass()->isDeprecated()) {
219+
return null;
220+
}
221+
}
222+
}
223+
224+
if ($location->value === ClassNameUsageLocation::CLASS_CONSTANT_ACCESS) {
225+
$constant = $location->getClassConstant();
226+
if ($constant !== null) {
227+
if ($constant->isDeprecated()->yes() || $constant->getDeclaringClass()->isDeprecated()) {
228+
return null;
229+
}
230+
}
231+
}
232+
206233
if (!$this->bleedingEdge) {
207234
return null;
208235
}

tests/Rules/Deprecations/CallToDeprecatedStaticMethodRuleTest.php

+6-10
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,13 @@ public function testDeprecatedStaticMethodCall(): void
3636
9,
3737
],
3838
[
39-
'Call to method foo() of deprecated class CheckDeprecatedStaticMethodCall\DeprecatedBar.',
40-
11,
41-
],
42-
[
43-
'Call to method deprecatedFoo() of deprecated class CheckDeprecatedStaticMethodCall\DeprecatedBar.',
39+
'Call to deprecated method deprecatedFoo() of class CheckDeprecatedStaticMethodCall\Foo.',
4440
12,
4541
],
4642
[
47-
'Call to method deprecatedFoo2() of deprecated class CheckDeprecatedStaticMethodCall\DeprecatedBar.',
43+
'Call to deprecated method deprecatedFoo2() of class CheckDeprecatedStaticMethodCall\Foo.',
4844
13,
4945
],
50-
[
51-
"Call to method foo() of deprecated class CheckDeprecatedStaticMethodCall\DeprecatedBaz:\nDo not touch this at all.",
52-
15,
53-
],
5446
[
5547
"Call to deprecated method deprecatedWithDescription() of class CheckDeprecatedStaticMethodCall\Foo:\nThis is probably a singleton.",
5648
16,
@@ -75,6 +67,10 @@ public function testDeprecatedStaticMethodCall(): void
7567
'Call to deprecated method deprecatedOtherFoo() of class CheckDeprecatedStaticMethodCall\Child.',
7668
77,
7769
],
70+
[
71+
'Call to method doDeprecatedBar() of deprecated class CheckDeprecatedStaticMethodCall\DeprecatedBar.',
72+
81,
73+
],
7874
],
7975
);
8076
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Deprecations;
4+
5+
use PHPStan\Rules\Methods\CallStaticMethodsRule;
6+
use PHPStan\Rules\Rule;
7+
use PHPStan\Testing\RuleTestCase;
8+
9+
/**
10+
* @extends RuleTestCase<CallStaticMethodsRule>
11+
*/
12+
class RestrictedDeprecatedClassNameUsageExtensionTest extends RuleTestCase
13+
{
14+
15+
protected function getRule(): Rule
16+
{
17+
return self::getContainer()->getByType(CallStaticMethodsRule::class);
18+
}
19+
20+
public function testStaticMethodCallOnDeprecatedSubclass(): void
21+
{
22+
require_once __DIR__ . '/data/call-to-deprecated-static-method-definition.php';
23+
24+
$this->analyse([__DIR__ . '/data/call-to-deprecated-static-method.php'], [
25+
[
26+
'Call to static method foo() on deprecated class CheckDeprecatedStaticMethodCall\DeprecatedBar.',
27+
11,
28+
],
29+
[
30+
'Call to static method foo() on deprecated class CheckDeprecatedStaticMethodCall\DeprecatedBaz.',
31+
15,
32+
],
33+
]);
34+
}
35+
36+
public static function getAdditionalConfigFiles(): array
37+
{
38+
return [
39+
__DIR__ . '/../../../rules.neon',
40+
...parent::getAdditionalConfigFiles(),
41+
];
42+
}
43+
44+
}

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

+2-23
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,9 @@ 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()
55+
public static function doDeprecatedBar()
6456
{
65-
66-
}
67-
68-
/**
69-
* @deprecated
70-
*/
71-
public static function deprecatedFoo2()
72-
{
73-
57+
7458
}
7559

7660
}
@@ -81,9 +65,4 @@ public static function deprecatedFoo2()
8165
class DeprecatedBaz extends Foo
8266
{
8367

84-
public static function foo()
85-
{
86-
87-
}
88-
8968
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,5 @@ public static function foo()
7777
static::deprecatedOtherFoo();
7878
}
7979
}
80+
81+
DeprecatedBar::doDeprecatedBar();

0 commit comments

Comments
 (0)