unionTypeAnalyzer = $unionTypeAnalyzer; $this->phpDocTypeChanger = $phpDocTypeChanger; $this->phpVersionProvider = $phpVersionProvider; $this->nodeFactory = $nodeFactory; } /** * @param \PhpParser\Node\Name|\PhpParser\Node\ComplexType|\PhpParser\Node\Identifier $typeNode */ public function decoratePropertyUnionType(UnionType $unionType, $typeNode, Property $property, PhpDocInfo $phpDocInfo, bool $changeVarTypeFallback = \true) : void { if (!$this->unionTypeAnalyzer->isNullable($unionType)) { if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES)) { $property->type = $typeNode; return; } if ($changeVarTypeFallback) { $this->phpDocTypeChanger->changeVarType($property, $phpDocInfo, $unionType); } return; } $property->type = $typeNode; $propertyProperty = $property->props[0]; // add null default if (!$propertyProperty->default instanceof Expr) { $propertyProperty->default = $this->nodeFactory->createNull(); } // has array with defined type? add docs if (!$this->isDocBlockRequired($unionType)) { return; } if (!$changeVarTypeFallback) { return; } $this->phpDocTypeChanger->changeVarType($property, $phpDocInfo, $unionType); } private function isDocBlockRequired(UnionType $unionType) : bool { foreach ($unionType->getTypes() as $unionedType) { if ($unionedType->isArray()->yes()) { $describedArray = $unionedType->describe(VerbosityLevel::value()); if ($describedArray !== 'array') { return \true; } } } return \false; } }