fix(php): 修复受保护方法访问检查和字符串转义问题

- 在 CompilerBase.php 中添加 classDef 检查以避免空指针错误
- 在 gen_stub.php 中对字符串表达式进行转义处理
- 将 Utils.php 中的 genCharPtr 和 escapeString 方法设为公共方法
- 添加测试用例验证默认数组属性功能
pull/1/head
韩天峰 3 months ago
parent ce7d254709
commit 17fcb16aec
  1. 3
      src/Php/CompilerBase.php
  2. 4
      src/Php/Generator/Utils.php
  3. 2
      src/gen_stub.php
  4. 24
      tests/aot/object_property/004.phpt

@ -6064,6 +6064,9 @@ class CompilerBase extends \PhpAot\Core\Translator
}
// 保护方法,只能当前类和子类使用
if ($flags & Modifiers::PROTECTED) {
if (!$this->classDef) {
return false;
}
return $this->isInheritedFrom($this->classDef->getNamespacedName(false), $classDef->getNamespacedName(false));
}
// 类外部调用,只允许调用 public 方法

@ -28,7 +28,7 @@ trait Utils
}
}
protected function genCharPtr(string $str, bool $escape = false): string
public function genCharPtr(string $str, bool $escape = false): string
{
return '"' . ($escape ? $this->escapeString($str) : $str) . '"';
}
@ -48,7 +48,7 @@ trait Utils
return 'R"(' . $str . ')"';
}
protected function escapeString(string $str): string
public function escapeString(string $str): string
{
$str = addcslashes($str, "\\\"\n\r\t\v\f\0\x01..\x1f\x7f..\xff");
// C++ trigraph

@ -2458,7 +2458,7 @@ class EvaluatedValue
} elseif (!($this->expr instanceof String_)) {
throw new Exception("Expression at line " . $this->expr->getStartLine() . " must be a scalar string");
}
$expr = preg_replace("/(^'|'$)/", '"', $expr);
$expr = preg_replace("/(^'|'$)/", '"', getTranslator()->escapeString($expr));
} elseif ($this->type->isInt() or $this->type->isFloat()) {
return strval($this->value);
} elseif ($this->type->isBool()) {

@ -0,0 +1,24 @@
--TEST--
default array property
--FILE--
<?php
class Test {
protected string $x = '[{"label":"Option 1","value":"1"},{"label":"Option 2","value":"2"},{"label":"Option 3","value":"3"},{"label":"Option 4","value":"4"}]';
public function bar() {
$json = json_decode($this->x,true);
Assert::isArray($json);
Assert::count($json, 4);
Assert::eq($json[0]['label'], "Option 1");
echo "DONE\n";
}
}
function main() {
require __DIR__ . '/../../../src/Assert.php';
$obj = new Test;
$obj->bar();
}
?>
--EXPECT--
DONE
Loading…
Cancel
Save