Skip to content

Commit 7d448c7

Browse files
committed
RestrictedInternalUsageHelper extraction
1 parent 9ae17c9 commit 7d448c7

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

conf/config.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,9 @@ services:
10021002
-
10031003
class: PHPStan\Rules\Generics\VarianceCheck
10041004

1005+
-
1006+
class: PHPStan\Rules\InternalTag\RestrictedInternalUsageHelper
1007+
10051008
-
10061009
class: PHPStan\Rules\IssetCheck
10071010
arguments:

src/Rules/InternalTag/RestrictedInternalMethodUsageExtension.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,33 @@
99
use function array_slice;
1010
use function explode;
1111
use function sprintf;
12-
use function str_starts_with;
1312
use function strtolower;
1413

1514
final class RestrictedInternalMethodUsageExtension implements RestrictedMethodUsageExtension
1615
{
1716

17+
public function __construct(private RestrictedInternalUsageHelper $helper)
18+
{
19+
}
20+
1821
public function isRestrictedMethodUsage(
1922
ExtendedMethodReflection $methodReflection,
2023
Scope $scope,
2124
): ?RestrictedUsage
2225
{
2326
$isMethodInternal = $methodReflection->isInternal()->yes();
24-
$isDeclaringClassInternal = $methodReflection->getDeclaringClass()->isInternal();
27+
$declaringClass = $methodReflection->getDeclaringClass();
28+
$isDeclaringClassInternal = $declaringClass->isInternal();
2529
if (!$isMethodInternal && !$isDeclaringClassInternal) {
2630
return null;
2731
}
2832

29-
$currentNamespace = $scope->getNamespace();
30-
$declaringClassName = $methodReflection->getDeclaringClass()->getName();
31-
$namespace = array_slice(explode('\\', $declaringClassName), 0, -1)[0] ?? null;
32-
if ($currentNamespace === null) {
33-
return $this->buildRestrictedUsage($methodReflection, $namespace, $isMethodInternal);
34-
}
35-
36-
$currentNamespace = explode('\\', $currentNamespace)[0];
37-
if (str_starts_with($namespace . '\\', $currentNamespace . '\\')) {
33+
$declaringClassName = $declaringClass->getName();
34+
if (!$this->helper->shouldBeReported($scope, $declaringClassName)) {
3835
return null;
3936
}
4037

41-
return $this->buildRestrictedUsage($methodReflection, $namespace, $isMethodInternal);
42-
}
43-
44-
private function buildRestrictedUsage(ExtendedMethodReflection $methodReflection, ?string $namespace, bool $isMethodInternal): RestrictedUsage
45-
{
38+
$namespace = array_slice(explode('\\', $declaringClassName), 0, -1)[0] ?? null;
4639
if ($namespace === null) {
4740
if (!$isMethodInternal) {
4841
return RestrictedUsage::create(
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\InternalTag;
4+
5+
use PHPStan\Analyser\Scope;
6+
use function array_slice;
7+
use function explode;
8+
use function str_starts_with;
9+
10+
final class RestrictedInternalUsageHelper
11+
{
12+
13+
public function shouldBeReported(Scope $scope, string $name): bool
14+
{
15+
$currentNamespace = $scope->getNamespace();
16+
$namespace = array_slice(explode('\\', $name), 0, -1)[0] ?? null;
17+
if ($currentNamespace === null) {
18+
return true;
19+
}
20+
21+
$currentNamespace = explode('\\', $currentNamespace)[0];
22+
23+
return !str_starts_with($namespace . '\\', $currentNamespace . '\\');
24+
}
25+
26+
}

0 commit comments

Comments
 (0)