Skip to content

Commit

Permalink
add support for @internal in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Mar 28, 2024
1 parent 086cc08 commit 032edd9
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Psalm\Internal\Algebra\FormulaGenerator;
use Psalm\Internal\Analyzer\AlgebraAnalyzer;
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
use Psalm\Internal\Analyzer\NamespaceAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\CallAnalyzer;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
Expand All @@ -23,6 +24,8 @@
use Psalm\Internal\Type\TypeCombiner;
use Psalm\Issue\DeprecatedFunction;
use Psalm\Issue\ImpureFunctionCall;
use Psalm\Issue\InternalClass;
use Psalm\Issue\InternalMethod;
use Psalm\Issue\InvalidFunctionCall;
use Psalm\Issue\MixedFunctionCall;
use Psalm\Issue\NullFunctionCall;
Expand Down Expand Up @@ -402,6 +405,30 @@ public static function analyze(
$statements_analyzer->getSuppressedIssues(),
);
}

if (!$context->collect_initializations
&& !$context->collect_mutations
&& $function_call_info->function_id !== null
) {
$caller_identifier = $statements_analyzer->getFullyQualifiedFunctionMethodOrNamespaceName();
if ($caller_identifier !== null
&& !NamespaceAnalyzer::isWithinAny(
$caller_identifier,
$function_call_info->function_storage->internal,
)) {
IssueBuffer::maybeAdd(
new InternalMethod(
'The function ' . $function_call_info->function_id
. ' is internal to '
. InternalClass::listToPhrase($function_call_info->function_storage->internal)
. ' but called from ' . ($caller_identifier ?: 'root namespace'),
$code_location,
$function_call_info->function_id,
),
$statements_analyzer->getSuppressedIssues(),
);
}
}
}

if ($function_name instanceof PhpParser\Node\Name && $function_call_info->function_id) {
Expand Down
90 changes: 90 additions & 0 deletions tests/InternalAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,96 @@ function hello(): void {
}',
'error_message' => 'InternalClass',
],
'internalFunctionOutsideClass' => [
'code' => '<?php
namespace A {
/**
* @internal
*/
function foo(): void {}
}
namespace B {
\A\foo();
}',
'error_message' => 'InternalMethod',
],
'internalFunctionOutsideClassGlobalNamespace' => [
'code' => '<?php
namespace A {
/**
* @internal
*/
function foo(): void {}
}
namespace {
\A\foo();
}',
'error_message' => 'InternalMethod',
],
'internalFunctionInsideFunction' => [
'code' => '<?php
namespace A {
/**
* @internal
*/
function foo(): void {}
}
namespace B {
function hello(): void {
\A\foo();
}
}',
'error_message' => 'InternalMethod',
],
'internalFunctionInsideFunctionGlobalNamespace' => [
'code' => '<?php
namespace A {
/**
* @internal
*/
function foo(): void {}
}
namespace {
function hello(): void {
\A\foo();
}
}',
'error_message' => 'InternalMethod',
],
'internalFunctionInsideClass' => [
'code' => '<?php
namespace A {
/**
* @internal
*/
function foo(): void {}
}
namespace B {
class Bar {
public function run(): void {
\A\foo();
}
}
}',
'error_message' => 'InternalMethod',
],
'internalFunctionInsideClassGlobalNamespace' => [
'code' => '<?php
namespace A {
/**
* @internal
*/
function foo(): void {}
}
namespace {
class Bar {
public function run(): void {
\A\foo();
}
}
}',
'error_message' => 'InternalMethod',
],
'magicPropertyGetInternalExplicit' => [
'code' => '<?php
namespace A {
Expand Down

0 comments on commit 032edd9

Please sign in to comment.