Skip to content

Commit a5883dc

Browse files
committed
Rewrite Api method call and static call rules as a restricted method usage extension
Constructors are no longer reported by this extension: new Foo() is checked by ApiInstantiationRule, and a class that may be extended may call parent::__construct(). Amp-Thread-ID: https://ampcode.com/threads/T-01a1bf5f-ca34-7733-8886-c850b9908584
1 parent 24ba68c commit a5883dc

6 files changed

Lines changed: 90 additions & 197 deletions

‎src/Rules/Api/ApiMethodCallRule.php‎

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Api;
4+
5+
use PHPStan\Analyser\Scope;
6+
use PHPStan\DependencyInjection\AutowiredService;
7+
use PHPStan\Reflection\ExtendedMethodReflection;
8+
use PHPStan\Rules\RestrictedUsage\RestrictedMethodUsageExtension;
9+
use PHPStan\Rules\RestrictedUsage\RestrictedUsage;
10+
use function count;
11+
use function sprintf;
12+
use function str_contains;
13+
use function strtolower;
14+
15+
#[AutowiredService]
16+
final class ApiMethodUsageExtension implements RestrictedMethodUsageExtension
17+
{
18+
19+
public function __construct(private ApiRuleHelper $apiRuleHelper)
20+
{
21+
}
22+
23+
public function isRestrictedMethodUsage(
24+
ExtendedMethodReflection $methodReflection,
25+
Scope $scope,
26+
): ?RestrictedUsage
27+
{
28+
// new Foo() is checked by ApiInstantiationRule, and a class that may be extended may call parent::__construct()
29+
if (strtolower($methodReflection->getName()) === '__construct') {
30+
return null;
31+
}
32+
33+
// resolves the real declaring class when called again with the declaring class rewritten to the called subclass
34+
$declaringClass = $methodReflection->getDeclaringClass();
35+
if ($declaringClass->hasNativeMethod($methodReflection->getName())) {
36+
$methodReflection = $declaringClass->getNativeMethod($methodReflection->getName());
37+
$declaringClass = $methodReflection->getDeclaringClass();
38+
}
39+
40+
if (!$this->apiRuleHelper->isPhpStanCode($scope, $declaringClass->getName(), $declaringClass->getFileName())) {
41+
return null;
42+
}
43+
44+
if ($this->isCovered($methodReflection)) {
45+
return null;
46+
}
47+
48+
return RestrictedUsage::create(
49+
sprintf(
50+
'Calling %s::%s() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.',
51+
$declaringClass->getDisplayName(),
52+
$methodReflection->getName(),
53+
),
54+
'phpstanApi.method',
55+
sprintf(
56+
"If you think it should be covered by backward compatibility promise, open a discussion:\n %s\n\n See also:\n https://phpstan.org/developing-extensions/backward-compatibility-promise",
57+
'https://github.com/phpstan/phpstan/discussions',
58+
),
59+
);
60+
}
61+
62+
private function isCovered(ExtendedMethodReflection $methodReflection): bool
63+
{
64+
$classDocBlock = $methodReflection->getDeclaringClass()->getResolvedPhpDoc();
65+
if ($classDocBlock !== null) {
66+
foreach ($classDocBlock->getPhpDocNodes() as $phpDocNode) {
67+
if (count($phpDocNode->getTagsByName('@api')) > 0) {
68+
return true;
69+
}
70+
}
71+
}
72+
73+
$methodDocComment = $methodReflection->getDocComment();
74+
if ($methodDocComment === null) {
75+
return false;
76+
}
77+
78+
return str_contains($methodDocComment, '@api');
79+
}
80+
81+
}

‎src/Rules/Api/ApiStaticCallRule.php‎

Lines changed: 0 additions & 101 deletions
This file was deleted.

tests/PHPStan/Rules/Api/ApiMethodCallRuleTest.php renamed to tests/PHPStan/Rules/Api/ApiMethodUsageExtensionMethodCallTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
namespace PHPStan\Rules\Api;
44

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

910
/**
10-
* @extends RuleTestCase<ApiMethodCallRule>
11+
* @extends RuleTestCase<RestrictedMethodUsageRule>
1112
*/
12-
class ApiMethodCallRuleTest extends RuleTestCase
13+
class ApiMethodUsageExtensionMethodCallTest extends RuleTestCase
1314
{
1415

1516
protected function getRule(): Rule
1617
{
17-
return new ApiMethodCallRule(new ApiRuleHelper());
18+
return self::getContainer()->getByType(RestrictedMethodUsageRule::class);
1819
}
1920

2021
public function testRuleInPhpStan(): void

tests/PHPStan/Rules/Api/ApiStaticCallRuleTest.php renamed to tests/PHPStan/Rules/Api/ApiMethodUsageExtensionStaticCallTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
namespace PHPStan\Rules\Api;
44

5+
use PHPStan\Rules\RestrictedUsage\RestrictedStaticMethodUsageRule;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Testing\RuleTestCase;
78
use function sprintf;
89

910
/**
10-
* @extends RuleTestCase<ApiStaticCallRule>
11+
* @extends RuleTestCase<RestrictedStaticMethodUsageRule>
1112
*/
12-
class ApiStaticCallRuleTest extends RuleTestCase
13+
class ApiMethodUsageExtensionStaticCallTest extends RuleTestCase
1314
{
1415

1516
protected function getRule(): Rule
1617
{
17-
return new ApiStaticCallRule(new ApiRuleHelper(), self::createReflectionProvider());
18+
return self::getContainer()->getByType(RestrictedStaticMethodUsageRule::class);
1819
}
1920

2021
public function testRuleInPhpStan(): void
@@ -35,11 +36,6 @@ public function testRuleOutOfPhpStan(): void
3536
17,
3637
$tip,
3738
],
38-
[
39-
'Calling PHPStan\Node\InClassNode::__construct() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.',
40-
33,
41-
$tip,
42-
],
4339
]);
4440
}
4541

‎tests/PHPStan/Rules/Api/data/static-call-out-of-phpstan.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Bar extends InClassNode
3030

3131
public function __construct()
3232
{
33-
parent::__construct();
33+
parent::__construct(); // not reported - extending the class is what gets checked
3434
}
3535

3636
}

0 commit comments

Comments
 (0)