Skip to content

Commit f7027e9

Browse files
committed
RestrictedInternalMethodUsageExtension - differentiate between internal method and internal declaring classlike
1 parent c7f870a commit f7027e9

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

Diff for: phpstan-baseline.neon

+2-2
Original file line numberDiff line numberDiff line change
@@ -1921,8 +1921,8 @@ parameters:
19211921
path: tests/PHPStan/Reflection/SignatureMap/Php8SignatureMapProviderTest.php
19221922

19231923
-
1924-
message: '#^Call to internal method PHPUnit\\Framework\\ExpectationFailedException\:\:getComparisonFailure\(\) from outside its root namespace PHPUnit\.$#'
1925-
identifier: method.internal
1924+
message: '#^Call to method getComparisonFailure\(\) of internal class PHPUnit\\Framework\\ExpectationFailedException from outside its root namespace PHPUnit\.$#'
1925+
identifier: method.internalClass
19261926
count: 2
19271927
path: tests/PHPStan/Testing/NonexistentAnalysedClassRuleTest.php
19281928

Diff for: src/Reflection/Php/PhpMethodReflection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public function isDeprecated(): TrinaryLogic
379379

380380
public function isInternal(): TrinaryLogic
381381
{
382-
return TrinaryLogic::createFromBoolean($this->isInternal || $this->declaringClass->isInternal());
382+
return TrinaryLogic::createFromBoolean($this->isInternal);
383383
}
384384

385385
public function isBuiltin(): TrinaryLogic

Diff for: src/Rules/InternalTag/RestrictedInternalMethodUsageExtension.php

+33-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use function explode;
1111
use function sprintf;
1212
use function str_starts_with;
13+
use function strtolower;
1314

1415
final class RestrictedInternalMethodUsageExtension implements RestrictedMethodUsageExtension
1516
{
@@ -19,33 +20,61 @@ public function isRestrictedMethodUsage(
1920
Scope $scope,
2021
): ?RestrictedUsage
2122
{
22-
if (!$methodReflection->isInternal()->yes()) {
23+
$isMethodInternal = $methodReflection->isInternal()->yes();
24+
$isDeclaringClassInternal = $methodReflection->getDeclaringClass()->isInternal();
25+
if (!$isMethodInternal && !$isDeclaringClassInternal) {
2326
return null;
2427
}
2528

2629
$currentNamespace = $scope->getNamespace();
2730
$declaringClassName = $methodReflection->getDeclaringClass()->getName();
2831
$namespace = array_slice(explode('\\', $declaringClassName), 0, -1)[0] ?? null;
2932
if ($currentNamespace === null) {
30-
return $this->buildRestrictedUsage($methodReflection, $namespace);
33+
return $this->buildRestrictedUsage($methodReflection, $namespace, $isMethodInternal);
3134
}
3235

3336
$currentNamespace = explode('\\', $currentNamespace)[0];
3437
if (str_starts_with($namespace . '\\', $currentNamespace . '\\')) {
3538
return null;
3639
}
3740

38-
return $this->buildRestrictedUsage($methodReflection, $namespace);
41+
return $this->buildRestrictedUsage($methodReflection, $namespace, $isMethodInternal);
3942
}
4043

41-
private function buildRestrictedUsage(ExtendedMethodReflection $methodReflection, ?string $namespace): RestrictedUsage
44+
private function buildRestrictedUsage(ExtendedMethodReflection $methodReflection, ?string $namespace, bool $isMethodInternal): RestrictedUsage
4245
{
4346
if ($namespace === null) {
47+
if (!$isMethodInternal) {
48+
return RestrictedUsage::create(
49+
sprintf(
50+
'Call to method %s() of internal %s %s.',
51+
$methodReflection->getName(),
52+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
53+
$methodReflection->getDeclaringClass()->getDisplayName(),
54+
),
55+
sprintf('method.internal%s', $methodReflection->getDeclaringClass()->getClassTypeDescription()),
56+
);
57+
}
58+
4459
return RestrictedUsage::create(
4560
sprintf('Call to internal method %s::%s().', $methodReflection->getDeclaringClass()->getDisplayName(), $methodReflection->getName()),
4661
'method.internal',
4762
);
4863
}
64+
65+
if (!$isMethodInternal) {
66+
return RestrictedUsage::create(
67+
sprintf(
68+
'Call to method %s() of internal %s %s from outside its root namespace %s.',
69+
$methodReflection->getName(),
70+
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
71+
$methodReflection->getDeclaringClass()->getDisplayName(),
72+
$namespace,
73+
),
74+
sprintf('method.internal%s', $methodReflection->getDeclaringClass()->getClassTypeDescription()),
75+
);
76+
}
77+
4978
return RestrictedUsage::create(
5079
sprintf('Call to internal method %s::%s() from outside its root namespace %s.', $methodReflection->getDeclaringClass()->getDisplayName(), $methodReflection->getName(), $namespace),
5180
'method.internal',

Diff for: tests/PHPStan/Rules/InternalTag/RestrictedInternalMethodUsageExtensionTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testRule(): void
2525
58,
2626
],
2727
[
28-
'Call to internal method MethodInternalTagOne\FooInternal::doFoo() from outside its root namespace MethodInternalTagOne.',
28+
'Call to method doFoo() of internal class MethodInternalTagOne\FooInternal from outside its root namespace MethodInternalTagOne.',
2929
63,
3030
],
3131
[
@@ -34,23 +34,23 @@ public function testRule(): void
3434
],
3535

3636
[
37-
'Call to internal method MethodInternalTagOne\FooInternal::doFoo() from outside its root namespace MethodInternalTagOne.',
37+
'Call to method doFoo() of internal class MethodInternalTagOne\FooInternal from outside its root namespace MethodInternalTagOne.',
3838
76,
3939
],
4040
[
4141
'Call to internal method FooWithInternalMethodWithoutNamespace::doInternal().',
4242
107,
4343
],
4444
[
45-
'Call to internal method FooInternalWithoutNamespace::doFoo().',
45+
'Call to method doFoo() of internal class FooInternalWithoutNamespace.',
4646
112,
4747
],
4848
[
4949
'Call to internal method FooWithInternalMethodWithoutNamespace::doInternal().',
5050
120,
5151
],
5252
[
53-
'Call to internal method FooInternalWithoutNamespace::doFoo().',
53+
'Call to method doFoo() of internal class FooInternalWithoutNamespace.',
5454
125,
5555
],
5656
]);

0 commit comments

Comments
 (0)