diff --git a/src/Type/FileTypeMapper.php b/src/Type/FileTypeMapper.php index c446b627a4..1baec0b9da 100644 --- a/src/Type/FileTypeMapper.php +++ b/src/Type/FileTypeMapper.php @@ -367,7 +367,7 @@ function (Node $node) use ($fileName, $lookForTrait, &$traitFound, $traitMethodA } if ($node instanceof Node\Stmt\Namespace_) { - $namespace = (string) $node->name; + $namespace = $node->name !== null ? (string) $node->name : null; } elseif ($node instanceof Node\Stmt\Use_) { if ($node->type === Node\Stmt\Use_::TYPE_NORMAL) { foreach ($node->uses as $use) { diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index f0eb2fbf3f..c706e719f9 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -1042,6 +1042,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7963-three.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8017.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8004.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/global-namespace.php'); } /** diff --git a/tests/PHPStan/Analyser/data/global-namespace.php b/tests/PHPStan/Analyser/data/global-namespace.php new file mode 100644 index 0000000000..971e0abd60 --- /dev/null +++ b/tests/PHPStan/Analyser/data/global-namespace.php @@ -0,0 +1,18 @@ +