Skip to content

fix(resolver): stop corrupting single-segment use const imports - #22

Open
AlessioGiacobbe wants to merge 1 commit into
swoole:masterfrom
AlessioGiacobbe:fix/single-segment-imports
Open

fix(resolver): stop corrupting single-segment use const imports#22
AlessioGiacobbe wants to merge 1 commit into
swoole:masterfrom
AlessioGiacobbe:fix/single-segment-imports

Conversation

@AlessioGiacobbe

Copy link
Copy Markdown
Contributor

Problem

parseUse rebuilds the imported constant name by splitting on the last backslash, but strrpos() returns false for a single-segment name, and substr($id, false + 1) drops the first character:

namespace App;

use const PHP_EOL;    // registered as `\HP_EOL`

echo PHP_EOL;         // compiled binary: Undefined constant "HP_EOL"

The mangled name is visible verbatim in the runtime error of the compiled program.

Fix

$id is already the fully qualified constant name — store it directly and drop the split/re-join. For multi-segment imports the removed recomposition produced the identical value, so nothing else changes.

Tests

  • New tests/compiler/namespace/use-const-single-segment.phpt: a namespaced file importing the global PHP_EOL plus a cross-namespace use const App\ANSWER. Expected output verified against PHP 8.4; on master the compiled binary dies with Undefined constant "HP_EOL".
  • tests/compiler/namespace/ suite: 34/34 pass. No PHPStan errors on the touched file.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant