Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Rules/InternalTag/RestrictedInternalUsageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ final class RestrictedInternalUsageHelper
public function shouldBeReported(Scope $scope, string $name): bool
{
$currentNamespace = $scope->getNamespace();
$namespace = array_slice(explode('\\', $name), 0, -1)[0] ?? null;
if ($currentNamespace === null) {
return true;
$classReflection = $scope->getClassReflection();
if ($classReflection === null) {
return true;
}

return $classReflection->getName() !== $name;
}

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

return !str_starts_with($namespace . '\\', $currentNamespace . '\\');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,14 @@ public function testBug12951(): void
]);
}

public function testNoNamespace(): void
{
$this->analyse([__DIR__ . '/data/no-namespace.php'], [
[
'Access to internal constant ClassInternal::INTERNAL_CONSTANT.',
41,
],
]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ public function testRule(): void
]);
}

public function testNoNamespace(): void
{
$this->analyse([__DIR__ . '/data/no-namespace.php'], [
[
'Call to internal method ClassInternal::internalMethod().',
38,
],
]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,14 @@ public function testStaticMethodCallOnInternalSubclass(): void
]);
}

public function testNoNamespace(): void
{
$this->analyse([__DIR__ . '/data/no-namespace.php'], [
[
'Call to internal static method ClassInternal::internalStaticMethod().',
39,
],
]);
}

}
43 changes: 43 additions & 0 deletions tests/PHPStan/Rules/InternalTag/data/no-namespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

class ClassInternal {
/**
* @internal
*/
public const INTERNAL_CONSTANT = 'xxx';

/**
* @internal
*/
public function internalMethod(): void
{

}

/**
* @internal
*/
public static function internalStaticMethod(): void
{

}

protected function getFoo(): string
{
$this->internalMethod();
self::internalStaticMethod();

return self::INTERNAL_CONSTANT;
}
}

class ClassAccessOnInternal {
protected function getFoo(): string
{
$classInternal = new ClassInternal();
$classInternal->internalMethod();
ClassInternal::internalStaticMethod();

return ClassInternal::INTERNAL_CONSTANT;
}
}
Loading