fix(const): 修复常量名与变量名冲突问题、常量名大小写敏感问题

pull/2/head
Yurun 2 months ago
parent a365474674
commit 30ef9352a2
  1. 9
      src/Php/CompilerBase.php
  2. 5
      src/Php/Generator/Utils.php
  3. 15
      tests/aot/const/const-same-name.phpt
  4. 15
      tests/aot/const/const-var-same-name.phpt

@ -143,6 +143,7 @@ class CompilerBase extends \PhpAot\Core\Translator
public const string DYNAMIC_CALLED_CLASS = '__dynamic_called_class__';
public const string STATIC_VAR = '_static_var_';
public const string GLOBAL_VAR = '_global_var_';
public const string CONST_VAR = '_const_var_';
public const string OBJECT_PROP = '_object_prop_';
public const string CLASS_MAP = 'class_map';
public const string FUNC_MAP = 'func_map';
@ -5021,22 +5022,22 @@ class CompilerBase extends \PhpAot\Core\Translator
$constInfo->type = $this->detectStrValueType($value);
$constInfo->namespace = $this->namespace;
$constInfo->name = $name;
$this->constants[$this->escapeNamespace($name)] = $constInfo;
$this->constants[$this->escapeConstVar($name)] = $constInfo;
}
protected function hasConstant(string $name): bool
{
return isset($this->constants[$this->escapeNamespace($name)]);
return isset($this->constants[$this->escapeConstVar($name)]);
}
protected function getConstant(string $name): string
{
return $this->escapeNamespace($name);
return $this->escapeConstVar($name);
}
protected function getConstantType(string $name): string
{
return $this->constants[$this->escapeNamespace($name)]->type;
return $this->constants[$this->escapeConstVar($name)]->type;
}
protected function detectStrValueType(mixed $constant): string

@ -95,6 +95,11 @@ trait Utils
return self::GLOBAL_VAR . $name;
}
protected function escapeConstVar(string $name): string
{
return self::CONST_VAR . str_replace('\\', self::NAMESPACE_SEPARATOR, $name);
}
protected function escapeNamespace(string $ns): string
{
return str_replace('\\', self::NAMESPACE_SEPARATOR, strtolower($ns));

@ -0,0 +1,15 @@
--TEST--
Constant with the same name (case-insensitive) resolve independently
--FILE--
<?php
const a = 123;
const A = 456;
function main(): void
{
var_dump(a, A);
}
?>
--EXPECT--
int(123)
int(456)

@ -0,0 +1,15 @@
--TEST--
Constant and variable with the same name (case-insensitive) resolve independently
--FILE--
<?php
const a = 123;
function main(): void
{
$a = 456;
var_dump(a, $a);
}
?>
--EXPECT--
int(123)
int(456)
Loading…
Cancel
Save