From 96ab6a53effc367baeb603f086b873de22c82e86 Mon Sep 17 00:00:00 2001 From: Alessio Giacobbe Date: Fri, 28 Aug 2026 17:58:58 +0200 Subject: [PATCH] fix(resolver): stop corrupting single-segment `use const` imports parseUse rebuilt the imported constant name by splitting on the last backslash, but strrpos() returns false for a single-segment name: `use const PHP_EOL;` registered the import as `\HP_EOL` (substr($id, false + 1) drops the first character), and the compiled program failed at runtime with `Undefined constant "HP_EOL"`. $id is already the fully qualified constant name, so store it directly. For multi-segment imports the removed recomposition produced the same value, so their behavior is unchanged. --- src/Resolver/DeclarationSymbolTrait.php | 10 +++---- .../namespace/use-const-single-segment.phpt | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 tests/compiler/namespace/use-const-single-segment.phpt diff --git a/src/Resolver/DeclarationSymbolTrait.php b/src/Resolver/DeclarationSymbolTrait.php index b4c1da57..52e9a36d 100644 --- a/src/Resolver/DeclarationSymbolTrait.php +++ b/src/Resolver/DeclarationSymbolTrait.php @@ -80,11 +80,11 @@ trait DeclarationSymbolTrait if ($type === Node\Stmt\Use_::TYPE_FUNCTION) { $this->useFunctions[$alias] = $id; } elseif ($type === Node\Stmt\Use_::TYPE_CONSTANT) { - $lastIndex = strrpos($id, '\\'); - $cn = substr($id, $lastIndex + 1); - $ns = substr($id, 0, $lastIndex); - $fullName = $ns . '\\' . $cn; - $this->useConstants[$alias] = $fullName; + // $id is already the fully qualified constant name. Splitting + // and re-joining it on `\` corrupted single-segment imports: + // `use const PHP_EOL;` resolved to `\HP_EOL` because + // strrpos() returns false when there is no separator. + $this->useConstants[$alias] = $id; } else { $idLower = strtolower($id); if ($idLower === 'native_types') { diff --git a/tests/compiler/namespace/use-const-single-segment.phpt b/tests/compiler/namespace/use-const-single-segment.phpt new file mode 100644 index 00000000..c6426ee9 --- /dev/null +++ b/tests/compiler/namespace/use-const-single-segment.phpt @@ -0,0 +1,28 @@ +--TEST-- +use const with a single-segment (global) constant name +--FILE-- + +--EXPECT-- +hello +42