Skip to content

Commit

Permalink
Make @internal in global namespace for methods consistent with proper…
Browse files Browse the repository at this point in the history
…ties and fix #10868
  • Loading branch information
kkmuffme committed Mar 29, 2024
1 parent 21c0066 commit 616237a
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Psalm/Internal/Analyzer/NamespaceAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,17 @@ public static function isWithin(string $calling_identifier, string $identifier):
*/
public static function isWithinAny(string $calling_identifier, array $identifiers): bool
{
if (count($identifiers) === 0) {
if ($identifiers === []) {
return true;
}

foreach ($identifiers as $identifier) {
if ($calling_identifier !== '' && $identifier === '') {
// if we have any non-empty, we ignore the empty one
// therefore continue instead of return
continue;
}

if (self::isWithin($calling_identifier, $identifier)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,8 @@ public function start(PhpParser\Node\Stmt\ClassLike $node): ?bool
$storage->internal = $docblock_info->psalm_internal;
} elseif ($docblock_info->internal && $this->aliases->namespace) {
$storage->internal = [NamespaceAnalyzer::getNameSpaceRoot($this->aliases->namespace)];
} elseif ($docblock_info->internal) {
$storage->internal = [''];

Check failure on line 654 in src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php

View workflow job for this annotation

GitHub Actions / build

InvalidPropertyAssignmentValue

src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php:654:38: InvalidPropertyAssignmentValue: $storage->internal with declared type 'list<non-empty-string>' cannot be assigned type 'list{''}' (see https://psalm.dev/145)
}

if ($docblock_info->final && !$storage->final) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ public static function addDocblockInfo(
$storage->internal = $docblock_info->psalm_internal;
} elseif ($docblock_info->internal && $aliases->namespace) {
$storage->internal = [NamespaceAnalyzer::getNameSpaceRoot($aliases->namespace)];
} elseif ($docblock_info->internal && $classlike_storage && $classlike_storage->name) {
$storage->internal = [NamespaceAnalyzer::getNameSpaceRoot($classlike_storage->name)];
} elseif ($docblock_info->internal) {
$storage->internal = [''];

Check failure on line 124 in src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockScanner.php

View workflow job for this annotation

GitHub Actions / build

InvalidPropertyAssignmentValue

src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockScanner.php:124:34: InvalidPropertyAssignmentValue: $storage->internal with declared type 'list<non-empty-string>' cannot be assigned type 'list{''}' (see https://psalm.dev/145)
}

if (($storage->internal || ($classlike_storage && $classlike_storage->internal))
Expand Down
6 changes: 5 additions & 1 deletion src/Psalm/Issue/InternalClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ final class InternalClass extends ClassIssue
public const ERROR_LEVEL = 4;
public const SHORTCODE = 174;

/** @param non-empty-list<non-empty-string> $words */
/** @param non-empty-list<string> $words */
public static function listToPhrase(array $words): string
{
$words = array_unique($words);
if ($words === ['']) {
return 'root namespace';
}

if (count($words) === 1) {
return reset($words);
}
Expand Down
108 changes: 108 additions & 0 deletions tests/InternalAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,30 @@ public function batBat() : void {
}
}',
],
'globalNamespaceInternalValidClass' => [
'code' => '<?php
namespace {
/**
* @internal
*/
class C {}
new C;
}
',
],
'globalNamespaceInternalValidFunction' => [
'code' => '<?php
namespace {
/**
* @internal
*/
function run(): void {}
run();
}
',
],
'psalmInternalMethodWithMethod' => [
'code' => '<?php
namespace X {
Expand Down Expand Up @@ -1532,6 +1556,90 @@ class C {
',
'error_message' => 'InternalProperty',
],
'globalNamespaceInternalClass' => [
'code' => '<?php
namespace {
/**
* @access private
*/
class C {}
}
namespace B {
use C;
new C;
}
',
'error_message' => 'InternalClass',
],
'globalNamespaceInternalConstructor' => [
'code' => '<?php
namespace {
class C {
/**
* @internal
*/
public function __construct() {}
}
}
namespace B {
use C;
new C;
}
',
'error_message' => 'InternalMethod',
],
'globalNamespaceInternalMethod' => [
'code' => '<?php
namespace {
class C {
/**
* @internal
*/
public function run(): void {}
}
}
namespace B {
use C;
$c = new C;
$c->run();
}
',
'error_message' => 'InternalMethod',
],
'globalNamespaceInternalFunction' => [
'code' => '<?php
namespace {
/**
* @internal
*/
function run(): void {}
}
namespace B {
run();
}
',
'error_message' => 'InternalMethod',
],
'globalNamespaceInternalProperty' => [
'code' => '<?php
namespace {
class C {
/**
* @internal
*
* @var string
*/
public $foo = "hello";
}
}
namespace B {
use C;
$c = new C;
echo $c->foo;
}
',
'error_message' => 'InternalProperty',
],
'psalmInternalClassWithCallClass' => [
'code' => '<?php
namespace A {
Expand Down

0 comments on commit 616237a

Please sign in to comment.