From 1e3cf4d25e2377332f829bc0b38337d86d494155 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sat, 13 Jun 2026 09:24:51 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E4=BC=98=E5=8C=96=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E8=A1=A8=E8=BE=BE=E5=BC=8F=E8=A7=A3=E6=9E=90=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 提前解析条件表达式并存储到变量中避免重复计算 - 更新三元运算符表达式的构建方式提高代码可读性 - 添加 preg_match 函数相关的 AOT 编译测试用例 - 添加对象链接操作符相关的引用测试用例 - 完善正则匹配功能的自动化测试覆盖 --- src/Php/CompilerBase.php | 3 ++- tests/aot/functions/preg_match.phpt | 41 +++++++++++++++++++++++++++++ tests/aot/ref/ref-new-var.phpt | 13 +++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/aot/functions/preg_match.phpt create mode 100644 tests/aot/ref/ref-new-var.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index a2797587..22fcde4f 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -3076,13 +3076,14 @@ class CompilerBase extends \PhpAot\Core\Translator if ($expr->if === null) { return $this->parseValueSelection($expr, $expr->cond, $expr->else, self::OP_NOT_EMPTY); } + $cond = $this->parseExpr($expr->cond); $if = $this->parseExpr($expr->if); $else = $this->parseExpr($expr->else); if ($this->detectTypeOfExpr($expr->if) !== $this->detectTypeOfExpr($expr->else)) { $if = 'php::Var(' . $if . ')'; $else = 'php::Var(' . $else . ')'; } - return '(' . $this->parseExpr($expr->cond) . ') ? (' . $if . ') : (' . $else . ')'; + return '(' . $cond . ') ? (' . $if . ') : (' . $else . ')'; } protected function parseMatch(Expr\Match_ $expr): string diff --git a/tests/aot/functions/preg_match.phpt b/tests/aot/functions/preg_match.phpt new file mode 100644 index 00000000..b8e73463 --- /dev/null +++ b/tests/aot/functions/preg_match.phpt @@ -0,0 +1,41 @@ +--TEST-- +compact +--FILE-- + +--EXPECT-- +array(4) { + [0]=> + array(2) { + [0]=> + string(9) "foobarbaz" + [1]=> + int(0) + } + [1]=> + array(2) { + [0]=> + string(3) "foo" + [1]=> + int(0) + } + [2]=> + array(2) { + [0]=> + string(3) "bar" + [1]=> + int(3) + } + [3]=> + array(2) { + [0]=> + string(3) "baz" + [1]=> + int(6) + } +} \ No newline at end of file diff --git a/tests/aot/ref/ref-new-var.phpt b/tests/aot/ref/ref-new-var.phpt new file mode 100644 index 00000000..485acc22 --- /dev/null +++ b/tests/aot/ref/ref-new-var.phpt @@ -0,0 +1,13 @@ +--TEST-- +object link operator +--FILE-- + +--EXPECT-- +string(4) "foo_" \ No newline at end of file