Skip to content

Commit 605f76c

Browse files
github-actions[bot]phpstan-bot
authored andcommitted
Fix scope namespace not reset when entering global namespace block
- In NodeScopeResolver, when processing a `namespace { }` block (global namespace) after a named namespace block, the scope's namespace was not being reset to null - This caused PHPDoc resolution to use the wrong function name (prefixed with the previous namespace), so PHPDoc annotations like `@return list<string>` were lost - Added `else` branch in namespace handling to call `enterNamespace('')` for global namespace blocks, which MutatingScope normalizes to `null` - New regression test in tests/PHPStan/Rules/Functions/data/bug-13831.php Closes phpstan/phpstan#13831
1 parent 2681e50 commit 605f76c

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,8 @@ private function processStmtNode(
10101010
} elseif ($stmt instanceof Node\Stmt\Namespace_) {
10111011
if ($stmt->name !== null) {
10121012
$scope = $scope->enterNamespace($stmt->name->toString());
1013+
} else {
1014+
$scope = $scope->enterNamespace('');
10131015
}
10141016

10151017
$scope = $this->processStmtNodesInternal($stmt, $stmt->stmts, $scope, $storage, $nodeCallback, $context)->getScope();

tests/PHPStan/Rules/Functions/MissingFunctionReturnTypehintRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,9 @@ public function testRule(): void
8282
]);
8383
}
8484

85+
public function testBug13831(): void
86+
{
87+
$this->analyse([__DIR__ . '/data/bug-13831.php'], []);
88+
}
89+
8590
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Bug13831 {
4+
// non-global namespace block
5+
}
6+
7+
namespace {
8+
9+
/** @return list<string> */
10+
function bug13831Qux(): array {
11+
return [ random_bytes(16) ];
12+
}
13+
14+
}

0 commit comments

Comments
 (0)