Skip to content

Commit

Permalink
fix mixed union case
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jul 6, 2024
1 parent 68141f4 commit cc494b6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Type/Php/PathinfoFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,20 @@ public function getTypeFromFunctionCall(
$scalarValues = $flagsType->getConstantScalarValues();
if ($scalarValues !== []) {
$pathInfoAll = $this->getConstant('PATHINFO_ALL');
if ($pathInfoAll === null) {
return null;
}

$result = [];
foreach ($scalarValues as $scalarValue) {
if ($pathInfoAll !== null && $scalarValue === $pathInfoAll) {
return $arrayType;
if ($scalarValue === $pathInfoAll) {
$result[] = $arrayType;
} else {
$result[] = new StringType();
}
}

return new StringType();
return TypeCombinator::union(...$result);
}

return TypeCombinator::union($arrayType, new StringType());
Expand Down
3 changes: 3 additions & 0 deletions tests/PHPStan/Analyser/nsrt/pathinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ function doFoo(string $s, int $i, $flag) {
if ($i === PATHINFO_ALL) {
assertType('array{dirname?: string, basename: string, extension?: string, filename: string}', pathinfo($s, $i));
}
if ($i === PATHINFO_ALL || $i === PATHINFO_DIRNAME) {
assertType('array{dirname?: string, basename: string, extension?: string, filename: string}|string', pathinfo($s, $i));
}
}

0 comments on commit cc494b6

Please sign in to comment.