Skip to content

Commit

Permalink
Fix conditional types in PHPDocs from stubs for native functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 3, 2024
1 parent 518d3a3 commit 8ae0b28
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/PhpDoc/ResolvedPhpDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,26 @@ public function changeParameterNamesByMapping(array $parameterNameMapping): self
return $this;
}

$mapParameterCb = static function (Type $type, callable $traverse) use ($parameterNameMapping): Type {
if ($type instanceof ConditionalTypeForParameter) {
$parameterName = substr($type->getParameterName(), 1);
if (array_key_exists($parameterName, $parameterNameMapping)) {
$type = $type->changeParameterName('$' . $parameterNameMapping[$parameterName]);
}
}

return $traverse($type);
};

$paramTags = $this->getParamTags();

$newParamTags = [];
foreach ($paramTags as $key => $paramTag) {
if (!array_key_exists($key, $parameterNameMapping)) {
continue;
}
$newParamTags[$parameterNameMapping[$key]] = $paramTag;
$transformedType = TypeTraverser::map($paramTag->getType(), $mapParameterCb);
$newParamTags[$parameterNameMapping[$key]] = $paramTag->withType($transformedType);
}

$paramOutTags = $this->getParamOutTags();
Expand All @@ -298,21 +310,14 @@ public function changeParameterNamesByMapping(array $parameterNameMapping): self
if (!array_key_exists($key, $parameterNameMapping)) {
continue;
}
$newParamOutTags[$parameterNameMapping[$key]] = $paramOutTag;

$transformedType = TypeTraverser::map($paramOutTag->getType(), $mapParameterCb);
$newParamOutTags[$parameterNameMapping[$key]] = $paramOutTag->withType($transformedType);
}

$returnTag = $this->getReturnTag();
if ($returnTag !== null) {
$transformedType = TypeTraverser::map($returnTag->getType(), static function (Type $type, callable $traverse) use ($parameterNameMapping): Type {
if ($type instanceof ConditionalTypeForParameter) {
$parameterName = substr($type->getParameterName(), 1);
if (array_key_exists($parameterName, $parameterNameMapping)) {
$type = $type->changeParameterName('$' . $parameterNameMapping[$parameterName]);
}
}

return $traverse($type);
});
$transformedType = TypeTraverser::map($returnTag->getType(), $mapParameterCb);
$returnTag = $returnTag->withType($transformedType);
}

Expand Down

0 comments on commit 8ae0b28

Please sign in to comment.