From 8fa49e078f66729fd302902d19262ee53a07856d Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sat, 13 Jun 2026 15:55:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E4=BC=98=E5=8C=96=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E5=99=A8=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E4=BB=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=B1=BB=E5=85=A5=E5=8F=A3=E6=8C=87=E9=92=88?= =?UTF-8?q?=E5=92=8C=E5=AD=97=E9=9D=A2=E9=87=8F=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用 getClassEntryPtr 获取 IteratorAggregate 和 Iterator 类入口指针 - 使用 getLiteralString 替换硬编码的方法名字符串 - 将 instanceOf 检查从字符串比较改为类入口指针比较 - 将方法调用从字符串参数改为字面量字符串参数 - 使用 getFuncPtr 替换 get_object_vars 函数调用的硬编码字符串 --- src/Php/Translator.php | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Php/Translator.php b/src/Php/Translator.php index bbb79438..77e89f1f 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -2638,37 +2638,46 @@ CODE; $tmpArrayVar = $this->genTmpVarName(); $this->addLocalVar($tmpArrayVar, self::TYPE_ARRAY); - $code = 'if (' . $obj . '.instanceOf("IteratorAggregate")) {' . PHP_EOL; - $code .= $this->getIndent() . $tmpVar . ' = ' . $obj . '.call("getIterator");' . PHP_EOL . '}' . PHP_EOL; - $code .= 'else if (' . $obj . '.instanceOf("Iterator")) {' . PHP_EOL; + $IteratorAggregateCe = $this->getClassEntryPtr('IteratorAggregate'); + $IteratorCe = $this->getClassEntryPtr('Iterator'); + $getIteratorStr = $this->getLiteralString('getIterator'); + $validStr = $this->getLiteralString('valid'); + $currentStr = $this->getLiteralString('current'); + $keyStr = $this->getLiteralString('key'); + $nextStr = $this->getLiteralString('next'); + $rewindStr = $this->getLiteralString('rewind'); + + $code = 'if (' . $obj . '.instanceOf(' . $IteratorAggregateCe . ')) {' . PHP_EOL; + $code .= $this->getIndent() . $tmpVar . ' = ' . $obj . '.call(' . $getIteratorStr . ');' . PHP_EOL . '}' . PHP_EOL; + $code .= 'else if (' . $obj . '.instanceOf(' . $IteratorCe . ')) {' . PHP_EOL; $code .= $this->getIndent() . $tmpVar . ' = ' . $obj . ';' . PHP_EOL . '}' . PHP_EOL; $code .= 'if (' . $tmpVar . ') {' . PHP_EOL; $this->indentLevel++; - $code .= $this->getIndent() . $tmpVar . '.call("rewind");' . PHP_EOL; - $code .= $this->getIndent() . 'for (;' . $tmpVar . '.call("valid"); ' . $tmpVar . '.call("next")) {' . PHP_EOL; + $code .= $this->getIndent() . $tmpVar . '.call(' . $rewindStr . ');' . PHP_EOL; + $code .= $this->getIndent() . 'for (;' . $tmpVar . '.call(' . $validStr . '); ' . $tmpVar . '.call(' . $nextStr . ')) {' . PHP_EOL; $this->indentLevel++; if ($node->valueVar instanceof List_) { $listTmpVar = $this->genTmpVarName(); $this->addLocalVar($listTmpVar, self::TYPE_VAR); - $code .= $this->getIndent() . ' ' . $listTmpVar . ' = ' . $tmpVar . '.call("current");' . PHP_EOL; + $code .= $this->getIndent() . ' ' . $listTmpVar . ' = ' . $tmpVar . '.call(' . $currentStr . ');' . PHP_EOL; if ($node->keyVar) { $keyVar = $this->parseIdentifier($node->keyVar); $this->checkVar($node, $keyVar); - $code .= $this->getIndent() . ' ' . $keyVar . ' = ' . $tmpVar . '.call("key");' . PHP_EOL; + $code .= $this->getIndent() . ' ' . $keyVar . ' = ' . $tmpVar . '.call(' . $keyStr . ');' . PHP_EOL; } $code .= $this->parseForeachItemAsList($listTmpVar, $node->valueVar->items); } else { $valueVar = $this->parseIdentifier($node->valueVar); $this->checkVar($node, $valueVar); - $code .= $this->getIndent() . ' ' . $valueVar . ' = ' . $tmpVar . '.call("current");' . PHP_EOL; + $code .= $this->getIndent() . ' ' . $valueVar . ' = ' . $tmpVar . '.call(' . $currentStr . ');' . PHP_EOL; if ($node->keyVar) { $keyVar = $this->parseIdentifier($node->keyVar); $this->checkVar($node, $keyVar); - $code .= $this->getIndent() . ' ' . $keyVar . ' = ' . $tmpVar . '.call("key");' . PHP_EOL; + $code .= $this->getIndent() . ' ' . $keyVar . ' = ' . $tmpVar . '.call(' . $keyStr . ');' . PHP_EOL; } } $code .= $this->parseStmts($node->stmts); @@ -2676,7 +2685,7 @@ CODE; $code .= '}' . PHP_EOL; $this->indentLevel--; $code .= $this->getIndent() . '} else {' . PHP_EOL; - $code .= $this->getIndent() . $tmpArrayVar . ' = php::call("get_object_vars", {' . $obj . '});' . PHP_EOL; + $code .= $this->getIndent() . $tmpArrayVar . ' = php::call(' . $this->getFuncPtr('get_object_vars') . ', {' . $obj . '});' . PHP_EOL; $code .= $this->parseForeachArray($node, $tmpArrayVar); $this->indentLevel--; $code .= '}' . PHP_EOL;