diff --git a/src/Php/Generator/Utils.php b/src/Php/Generator/Utils.php index b86511eb..89391995 100644 --- a/src/Php/Generator/Utils.php +++ b/src/Php/Generator/Utils.php @@ -183,4 +183,15 @@ trait Utils return $str; } + + protected function getNamespaceOfClass(string $class): string + { + $lastPos = strrpos($class, '\\'); + if ($lastPos !== false) { + $namespace = substr($class, 0, $lastPos); + } else { + $namespace = ''; + } + return $namespace; + } } diff --git a/src/Php/Translator.php b/src/Php/Translator.php index e608ba82..e4442e97 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -1195,11 +1195,11 @@ CODE; * 仅用于 gen_stub 脚本 * @throws \Exception */ - public function getClassConstValue(NodeAbstract $expr, string $_class, string $name, string $currentClass): mixed + public function getClassConstValue(NodeAbstract $expr, string $_class, string $name, string $currentClass = ''): mixed { $namespace = $this->namespace; - if (!$namespace) { - $namespace = (string)strstr($currentClass, '\\', true); + if (!$namespace and $currentClass and !str_contains($_class, '\\')) { + $namespace = $this->getNamespaceOfClass($currentClass); } $class = $this->getNamespacedClassName($_class, $namespace); $nativeConst = $this->findNativeClassConst($expr, $class, $name); diff --git a/src/gen_stub.php b/src/gen_stub.php index a4b68a5a..0728384a 100755 --- a/src/gen_stub.php +++ b/src/gen_stub.php @@ -2307,14 +2307,14 @@ class EvaluatedValue if ($class === 'self') { $constName = ClassInfo::$currentClass . "::" . $expr->name->__toString(); if (isset($allConstInfos[$constName])) { - return $allConstInfos[$constName]->getValue($allConstInfos)->value; + return formatConstValue($allConstInfos[$constName]->getValue($allConstInfos)->value); } else { - return getTranslator()->getClassConstValue($expr, ClassInfo::$currentClass, $expr->name->toString(), ClassInfo::$currentClass); + return formatConstValue(getTranslator()->getClassConstValue($expr, ClassInfo::$currentClass, $expr->name->toString())); } } elseif ($expr->name->__toString() === 'class') { return $class; } else { - return getTranslator()->getClassConstValue($expr, $class, $expr->name->__toString(), ClassInfo::$currentClass); + return formatConstValue(getTranslator()->getClassConstValue($expr, $class, $expr->name->__toString(), ClassInfo::$currentClass)); } } else { $constName = $expr->name->__toString(); @@ -2352,11 +2352,7 @@ class EvaluatedValue if (isset($definedConstants[$constName])) { $constValue = $definedConstants[$constName]; if (is_scalar($constValue)) { - if (is_string($constValue)) { - return '"' . $constValue . '"'; - } else { - return $constValue; - } + return formatConstValue($constValue); } } @@ -4869,7 +4865,7 @@ function parseFunctionLike( } if ($param->default instanceof Expr\ClassConstFetch && $param->default->class->toLowerString() === "self") { - $defaultValue = getTranslator()->getClassConstValue($func, $name->className->name, $param->default->name->name, ClassInfo::$currentClass); + $defaultValue = getTranslator()->getClassConstValue($func, $name->className->name, $param->default->name->name); } else { $defaultValue = $param->default ? $prettyPrinter->prettyPrintExpr($param->default) : null; } @@ -6186,6 +6182,18 @@ function initPhpParser() { $isInitialized = true; } +function formatConstValue(mixed $constValue) +{ + if (is_string($constValue)) { + if (str_starts_with($constValue, '"') and str_ends_with($constValue, '"')) { + return $constValue; + } + return '"' . $constValue . '"'; + } else { + return $constValue; + } +} + function getTranslator(): Translator { global $translator; diff --git a/tests/aot/object_property/005.phpt b/tests/aot/object_property/005.phpt new file mode 100644 index 00000000..cf46ae6a --- /dev/null +++ b/tests/aot/object_property/005.phpt @@ -0,0 +1,21 @@ +--TEST-- +default array property +--FILE-- +x); + } +} + +function main() { + require __DIR__ . '/../../../src/Assert.php'; + $obj = new Test; + $obj->bar(); +} +?> +--EXPECT-- +string(4) "test" \ No newline at end of file