Merge pull request #22 from AlessioGiacobbe/fix/single-segment-imports

fix(resolver): stop corrupting single-segment use const imports
master
韩天峰 5 days ago committed by GitHub
commit 250a384bb8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 10
      src/Resolver/DeclarationSymbolTrait.php
  2. 28
      tests/compiler/namespace/use-const-single-segment.phpt

@ -80,11 +80,11 @@ trait DeclarationSymbolTrait
if ($type === Node\Stmt\Use_::TYPE_FUNCTION) { if ($type === Node\Stmt\Use_::TYPE_FUNCTION) {
$this->useFunctions[$alias] = $id; $this->useFunctions[$alias] = $id;
} elseif ($type === Node\Stmt\Use_::TYPE_CONSTANT) { } elseif ($type === Node\Stmt\Use_::TYPE_CONSTANT) {
$lastIndex = strrpos($id, '\\'); // $id is already the fully qualified constant name. Splitting
$cn = substr($id, $lastIndex + 1); // and re-joining it on `\` corrupted single-segment imports:
$ns = substr($id, 0, $lastIndex); // `use const PHP_EOL;` resolved to `\HP_EOL` because
$fullName = $ns . '\\' . $cn; // strrpos() returns false when there is no separator.
$this->useConstants[$alias] = $fullName; $this->useConstants[$alias] = $id;
} else { } else {
$idLower = strtolower($id); $idLower = strtolower($id);
if ($idLower === 'native_types') { if ($idLower === 'native_types') {

@ -0,0 +1,28 @@
--TEST--
use const with a single-segment (global) constant name
--FILE--
<?php
namespace App {
use const PHP_EOL;
const ANSWER = 42;
function hello(): string
{
return "hello" . PHP_EOL;
}
}
namespace {
use const App\ANSWER;
function main(): void
{
echo \App\hello();
echo ANSWER, "\n";
}
}
?>
--EXPECT--
hello
42
Loading…
Cancel
Save