From fc165fea275f849d2eb20e63ab8ea0bc3dd0a35e Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 2 Jun 2026 17:41:59 +0800 Subject: [PATCH] =?UTF-8?q?refactor(Php):=20=E6=8F=90=E5=8F=96=20std=20?= =?UTF-8?q?=E5=AE=B9=E5=99=A8=E8=A7=A3=E6=9E=90=E7=9A=84=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 std::map 和 std::unordered_map 的重复解析逻辑提取到 parseStdMapBase 方法 - 使用参数化的方式处理不同类型的容器解析需求 - 消除了两个方法中的代码重复 - 提高了代码的可维护性和一致性 - 保持了原有的功能行为不变 --- src/Php/Parser/StdContainerParser.php | 31 ++++++++++----------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/src/Php/Parser/StdContainerParser.php b/src/Php/Parser/StdContainerParser.php index 7bf14028..90e46fbd 100644 --- a/src/Php/Parser/StdContainerParser.php +++ b/src/Php/Parser/StdContainerParser.php @@ -729,34 +729,25 @@ trait StdContainerParser protected function parseStdMap(string $var, Expr\StaticCall $expr): string { - if (count($expr->args) !== 2) { - $this->fatalError($expr, 'std::map() expects two arguments'); - } - $keyType = $this->parseStdMapKeyType($expr->args[0]->value, 'std::map'); - $valueTypeInfo = $this->parseStdValueTypeInfo($expr->args[1]->value, 'std::map'); - $valueType = $valueTypeInfo['type']; - $decl = $this->getStdMapDecl(self::TYPE_STD_MAP, $keyType, $valueType); - $this->context->stdContainers[$var] = $this->addStdTypeId([ - 'kind' => 'map', - 'decl' => $decl, - 'type' => $valueType, - 'class' => $valueTypeInfo['class'], - 'keyType' => $keyType, - ]); - return '// ' . $decl; + return $this->parseStdMapBase($var, $expr, 'std::map', self::TYPE_STD_MAP, 'map'); } protected function parseStdUnorderedMap(string $var, Expr\StaticCall $expr): string + { + return $this->parseStdMapBase($var, $expr, 'std::unordered_map', self::TYPE_STD_UNORDERED_MAP, 'unordered_map'); + } + + private function parseStdMapBase(string $var, Expr\StaticCall $expr, string $funcName, string $containerType, string $kind): string { if (count($expr->args) !== 2) { - $this->fatalError($expr, 'std::unordered_map() expects two arguments'); + $this->fatalError($expr, $funcName . '() expects two arguments'); } - $keyType = $this->parseStdMapKeyType($expr->args[0]->value, 'std::unordered_map'); - $valueTypeInfo = $this->parseStdValueTypeInfo($expr->args[1]->value, 'std::unordered_map'); + $keyType = $this->parseStdMapKeyType($expr->args[0]->value, $funcName); + $valueTypeInfo = $this->parseStdValueTypeInfo($expr->args[1]->value, $funcName); $valueType = $valueTypeInfo['type']; - $decl = $this->getStdMapDecl(self::TYPE_STD_UNORDERED_MAP, $keyType, $valueType); + $decl = $this->getStdMapDecl($containerType, $keyType, $valueType); $this->context->stdContainers[$var] = $this->addStdTypeId([ - 'kind' => 'unordered_map', + 'kind' => $kind, 'decl' => $decl, 'type' => $valueType, 'class' => $valueTypeInfo['class'],