refactor(php): 优化PHP解析器中的字符串连接操作处理

- 实现了递归展平函数来处理嵌套的连接表达式
- 将原有的二元操作符处理改为多参数列表处理
- 添加了类型检查逻辑以防止空值连接错误
- 更新了OPcache配置命令以确保扩展正确加载
- 重构了连接操作的字符串转换和参数构建过程
pull/3/head
韩天峰 2 months ago
parent 286330f45d
commit 0f6e07252b
  1. 2
      bin/opcode.php
  2. 26
      src/Php/Parser/BinaryOpTrait.php

@ -9,4 +9,4 @@ if (!file_exists($file)) {
echo "File not found: " . $file . "\n";
exit(1);
}
shell_exec("php -d opcache.enable_cli=On -d opcache.opt_debug_level=0x10000 " . $file);
shell_exec("php -d zend_extension=opcache -d opcache.enable_cli=On -d opcache.opt_debug_level=0x10000 " . $file);

@ -142,15 +142,29 @@ trait BinaryOpTrait
protected function parseBinaryOpConcat(Expr\BinaryOp\Concat $expr): string
{
$left = $this->parseExpr($expr->left);
$right = $this->parseExpr($expr->right);
$items = [];
$this->flattenConcatExpr($expr, $items);
$leftType = $this->detectTypeOfExpr($expr->left);
$rightType = $this->detectTypeOfExpr($expr->right);
if ($leftType === self::TYPE_VOID or $rightType === self::TYPE_VOID) {
$argList = [];
foreach ($items as $item) {
$type = $this->detectTypeOfExpr($item);
if ($type === self::TYPE_VOID) {
$this->fatalError($expr, 'Cannot concat void');
}
return Symbol::concat() . '(' . $this->convertExprToStringByType($left, $leftType) . ', ' . $this->convertExprToStringByType($right, $rightType) . ')';
$argList[] = $this->convertExprToStringByType($this->parseExpr($item), $type);
}
return Symbol::concat() . '(' . Symbol::argList() . '{' . implode(', ', $argList) . '})';
}
private function flattenConcatExpr(NodeAbstract $expr, array &$items): void
{
if ($expr instanceof Expr\BinaryOp\Concat) {
$this->flattenConcatExpr($expr->left, $items);
$this->flattenConcatExpr($expr->right, $items);
} else {
$items[] = $expr;
}
}
protected function parseBinaryOpSmaller(Expr\BinaryOp\Smaller $expr): string

Loading…
Cancel
Save