Skip to content

Commit

Permalink
same for classes and class constants
Browse files Browse the repository at this point in the history
Fix #10025
  • Loading branch information
kkmuffme committed Mar 28, 2024
1 parent efd17a1 commit 086cc08
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,16 @@ private static function analyzeNamedConstructor(
);
}


if ($context->self
$caller_identifier = $context->self ?? $statements_analyzer->getNamespace();
if ($caller_identifier !== null
&& !$context->collect_initializations
&& !$context->collect_mutations
&& !NamespaceAnalyzer::isWithinAny($context->self, $storage->internal)
&& !NamespaceAnalyzer::isWithinAny($caller_identifier, $storage->internal)
) {
IssueBuffer::maybeAdd(
new InternalClass(
$fq_class_name . ' is internal to ' . InternalClass::listToPhrase($storage->internal)
. ' but called from ' . $context->self,
. ' but called from ' . ($caller_identifier ?: 'root namespace'),
new CodeLocation($statements_analyzer->getSource(), $stmt),
$fq_class_name,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -840,11 +840,13 @@ private static function handleNamedCall(
);
}

if ($context->self && ! NamespaceAnalyzer::isWithinAny($context->self, $class_storage->internal)) {
$caller_identifier = $context->self ?? $statements_analyzer->getNamespace();
if ($caller_identifier !== null
&& !NamespaceAnalyzer::isWithinAny($caller_identifier, $class_storage->internal)) {
IssueBuffer::maybeAdd(
new InternalClass(
$fq_class_name . ' is internal to ' . InternalClass::listToPhrase($class_storage->internal)
. ' but called from ' . $context->self,
. ' but called from ' . ($caller_identifier ?: 'root namespace'),
new CodeLocation($statements_analyzer->getSource(), $stmt),
$fq_class_name,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,16 +365,17 @@ public static function analyzeFetch(
}
}

if ($context->self
$caller_identifier = $context->self ?? $statements_analyzer->getNamespace();
if ($caller_identifier !== null
&& !$context->collect_initializations
&& !$context->collect_mutations
&& !NamespaceAnalyzer::isWithinAny($context->self, $const_class_storage->internal)
&& !NamespaceAnalyzer::isWithinAny($caller_identifier, $const_class_storage->internal)
) {
IssueBuffer::maybeAdd(
new InternalClass(
$fq_class_name . ' is internal to '
. InternalClass::listToPhrase($const_class_storage->internal)
. ' but called from ' . $context->self,
. ' but called from ' . ($caller_identifier ?: 'root namespace'),
new CodeLocation($statements_analyzer->getSource(), $stmt),
$fq_class_name,
),
Expand Down Expand Up @@ -656,16 +657,17 @@ public static function analyzeFetch(
}
}

if ($context->self
$caller_identifier = $context->self ?? $statements_analyzer->getNamespace();
if ($caller_identifier !== null
&& !$context->collect_initializations
&& !$context->collect_mutations
&& !NamespaceAnalyzer::isWithinAny($context->self, $const_class_storage->internal)
&& !NamespaceAnalyzer::isWithinAny($caller_identifier, $const_class_storage->internal)
) {
IssueBuffer::maybeAdd(
new InternalClass(
$fq_class_name . ' is internal to '
. InternalClass::listToPhrase($const_class_storage->internal)
. ' but called from ' . $context->self,
. ' but called from ' . ($caller_identifier ?: 'root namespace'),
new CodeLocation($statements_analyzer->getSource(), $stmt),
$fq_class_name,
),
Expand Down
56 changes: 56 additions & 0 deletions tests/InternalAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,62 @@ class Foo {
}',
'error_message' => 'InternalProperty',
],
'internalClassOutsideClass' => [
'code' => '<?php
namespace A {
/**
* @internal
*/
class Foo {}
}
namespace B {
$f = new \A\Foo();
}',
'error_message' => 'InternalClass',
],
'internalClassOutsideClassGlobalNamespace' => [
'code' => '<?php
namespace A {
/**
* @internal
*/
class Foo {}
}
namespace {
$f = new \A\Foo();
}',
'error_message' => 'InternalClass',
],
'internalClassInsideFunction' => [
'code' => '<?php
namespace A {
/**
* @internal
*/
class Foo {}
}
namespace B {
function hello(): void {
$f = new \A\Foo();
}
}',
'error_message' => 'InternalClass',
],
'internalClassInsideFunctionGlobalNamespace' => [
'code' => '<?php
namespace A {
/**
* @internal
*/
class Foo {}
}
namespace {
function hello(): void {
$f = new \A\Foo();
}
}',
'error_message' => 'InternalClass',
],
'magicPropertyGetInternalExplicit' => [
'code' => '<?php
namespace A {
Expand Down

0 comments on commit 086cc08

Please sign in to comment.