refactor(Php): 提取 std 容器解析的公共逻辑

- 将 std::map 和 std::unordered_map 的重复解析逻辑提取到 parseStdMapBase 方法
- 使用参数化的方式处理不同类型的容器解析需求
- 消除了两个方法中的代码重复
- 提高了代码的可维护性和一致性
- 保持了原有的功能行为不变
pull/1/head
韩天峰 3 months ago
parent bbbb3c948e
commit fc165fea27
  1. 31
      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'],

Loading…
Cancel
Save