refactor(php): 优化条件表达式解析逻辑

- 提前解析条件表达式并存储到变量中避免重复计算
- 更新三元运算符表达式的构建方式提高代码可读性
- 添加 preg_match 函数相关的 AOT 编译测试用例
- 添加对象链接操作符相关的引用测试用例
- 完善正则匹配功能的自动化测试覆盖
pull/1/head
韩天峰 2 months ago
parent 9138dea99f
commit 1e3cf4d25e
  1. 3
      src/Php/CompilerBase.php
  2. 41
      tests/aot/functions/preg_match.phpt
  3. 13
      tests/aot/ref/ref-new-var.phpt

@ -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

@ -0,0 +1,41 @@
--TEST--
compact
--FILE--
<?php
function main()
{
preg_match('/(foo)(bar)(baz)/', 'foobarbaz', $matches, PREG_OFFSET_CAPTURE);
var_dump($matches);
}
?>
--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)
}
}

@ -0,0 +1,13 @@
--TEST--
object link operator
--FILE--
<?php
function main()
{
$name = 'foo_123()';
$prefix = preg_match('/^([a-z_]+_)/i', $name, $m) ? $m[1] : 'other';
var_dump($prefix);
}
?>
--EXPECT--
string(4) "foo_"
Loading…
Cancel
Save