From 204cdfb6680a896768514732b9e056bd91f31046 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 9 Jun 2026 15:10:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E9=87=8D=E6=9E=84PHP=E9=A2=84?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=99=A8=E4=B8=AD=E7=9A=84=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 提取类属性创建逻辑到独立的 addClassProperty 方法 - 简化构造函数参数提升属性的解析流程 - 统一常规属性声明和构造函数属性提升的处理方式 - 移除重复的属性解析代码,提高代码复用性 - 优化空值类型判断逻辑,减少条件分支嵌套 --- src/Php/Preprocessor.php | 66 +++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index b3f55ec1..64821370 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -255,25 +255,8 @@ class Preprocessor extends CompilerBase $this->fatalError($param, 'Promoted properties are not supported'); } $name = $this->parseIdentifier($param->var); - if ($param->type instanceof NullableType) { - $type = $param->type->type; - $nullable = true; - } elseif ($param->type instanceof UnionType) { - $type = 'mixed'; - $nullable = false; - } else { - $type = $param->type === null ? '' : $param->type; - $nullable = false; - } - $default = $this->parseParamDefaultValue($param->default); - if ($this->classDef->hasProperty($name)) { - $this->fatalError($param, "Duplicate property `{$name}`"); - } - $propClass = ''; - $propType = $this->parseTypeDecl($type, self::DECL_TYPE_OF_PROPERTY, $propClass); - $propertyDef = new PropertyDef($name, $param->flags, $propType, $default, $nullable); - $propertyDef->class = $propClass; - $this->classDef->properties[$name] = $propertyDef; + $nullable = $param->type instanceof NullableType; + $this->addClassProperty($name, $param->flags, $param->type, $param->default, $nullable, $param); } if ($param->variadic) { if ($i !== $last) { @@ -546,6 +529,34 @@ class Preprocessor extends CompilerBase } } + /** + * Create and register a class property with type normalization, shared by + * regular property declarations and constructor property promotion. + */ + protected function addClassProperty(string $name, int $flags, ?NodeAbstract $typeNode, $defaultNode, bool $nullable, NodeAbstract $errorNode): PropertyDef + { + $flags = $this->parseModifiers($flags); + $class = ''; + $type = $this->parseTypeDecl($typeNode, self::DECL_TYPE_OF_PROPERTY, $class); + + $default = null; + if ($defaultNode !== null) { + $default = $this->parseIdentifier($defaultNode); + if ($defaultNode->getType() == 'Expr_Array') { + $type = self::TYPE_ARRAY; + } + } + + if ($this->classDef->hasProperty($name)) { + $this->fatalError($errorNode, "Duplicate property `{$name}`"); + } + + $propDef = new PropertyDef($name, $flags, $type, $default, $nullable); + $propDef->class = $class; + $this->classDef->properties[$name] = $propDef; + return $propDef; + } + protected function parseClassPropertyDef(Node\Stmt\Property $v): void { if ($v->hooks and count($v->hooks) > 0) { @@ -553,24 +564,11 @@ class Preprocessor extends CompilerBase } $oriCtx = $this->context; $this->context = $this->classDef->propertyContext; - $flags = $this->parseModifiers($v->flags); - $class = ''; - $type = $this->parseTypeDecl($v->type, self::DECL_TYPE_OF_PROPERTY, $class); + $nullable = $v->type instanceof NullableType; foreach ($v->props as $prop) { $propName = $this->parseIdentifier($prop->name); - if ($this->classDef->hasProperty($propName)) { - $this->fatalError($v, "Duplicate property `{$propName}`"); - } - $propDef = new PropertyDef($propName, $flags, $type); - if ($prop->default) { - $propDef->default = $this->parseIdentifier($prop->default); - if ($prop->default->getType() == 'Expr_Array') { - $propDef->type = self::TYPE_ARRAY; - } - } - $propDef->class = $class; - $this->classDef->properties[$propDef->name] = $propDef; + $this->addClassProperty($propName, $v->flags, $v->type, $prop->default, $nullable, $v); } $this->context = $oriCtx;