Skip to content

Commit 2c76493

Browse files
Handle class without namespace
1 parent c5a9b8d commit 2c76493

5 files changed

+77
-2
lines changed

src/Rules/InternalTag/RestrictedInternalUsageHelper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ final class RestrictedInternalUsageHelper
1313
public function shouldBeReported(Scope $scope, string $name): bool
1414
{
1515
$currentNamespace = $scope->getNamespace();
16-
$namespace = array_slice(explode('\\', $name), 0, -1)[0] ?? null;
1716
if ($currentNamespace === null) {
18-
return true;
17+
$currentClass = $scope->getClassReflection()?->getName();
18+
19+
return $currentClass !== $name;
1920
}
2021

2122
$currentNamespace = explode('\\', $currentNamespace)[0];
23+
$namespace = array_slice(explode('\\', $name), 0, -1)[0] ?? null;
2224

2325
return !str_starts_with($namespace . '\\', $currentNamespace . '\\');
2426
}

tests/PHPStan/Rules/InternalTag/RestrictedInternalClassConstantUsageExtensionTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,14 @@ public function testBug12951(): void
7777
]);
7878
}
7979

80+
public function testNoNamespace(): void
81+
{
82+
$this->analyse([__DIR__ . '/data/no-namespace.php'], [
83+
[
84+
'Access to internal constant ClassInternal::INTERNAL_CONSTANT.',
85+
41,
86+
],
87+
]);
88+
}
89+
8090
}

tests/PHPStan/Rules/InternalTag/RestrictedInternalMethodUsageExtensionTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,14 @@ public function testRule(): void
5656
]);
5757
}
5858

59+
public function testNoNamespace(): void
60+
{
61+
$this->analyse([__DIR__ . '/data/no-namespace.php'], [
62+
[
63+
'Call to internal method ClassInternal::internalMethod().',
64+
38,
65+
],
66+
]);
67+
}
68+
5969
}

tests/PHPStan/Rules/InternalTag/RestrictedInternalStaticMethodUsageExtensionTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,14 @@ public function testStaticMethodCallOnInternalSubclass(): void
6666
]);
6767
}
6868

69+
public function testNoNamespace(): void
70+
{
71+
$this->analyse([__DIR__ . '/data/no-namespace.php'], [
72+
[
73+
'Call to internal static method ClassInternal::internalStaticMethod().',
74+
39,
75+
],
76+
]);
77+
}
78+
6979
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
class ClassInternal {
4+
/**
5+
* @internal
6+
*/
7+
public const INTERNAL_CONSTANT = 'xxx';
8+
9+
/**
10+
* @internal
11+
*/
12+
public function internalMethod(): void
13+
{
14+
15+
}
16+
17+
/**
18+
* @internal
19+
*/
20+
public static function internalStaticMethod(): void
21+
{
22+
23+
}
24+
25+
protected function getFoo(): string
26+
{
27+
$this->internalMethod();
28+
self::internalStaticMethod();
29+
30+
return self::INTERNAL_CONSTANT;
31+
}
32+
}
33+
34+
class ClassAccessOnInternal {
35+
protected function getFoo(): string
36+
{
37+
$classInternal = new ClassInternal();
38+
$classInternal->internalMethod();
39+
ClassInternal::internalStaticMethod();
40+
41+
return ClassInternal::INTERNAL_CONSTANT;
42+
}
43+
}

0 commit comments

Comments
 (0)