diff --git a/src/Type/Php/PathinfoFunctionDynamicReturnTypeExtension.php b/src/Type/Php/PathinfoFunctionDynamicReturnTypeExtension.php index 4c5193d812..11a5f74752 100644 --- a/src/Type/Php/PathinfoFunctionDynamicReturnTypeExtension.php +++ b/src/Type/Php/PathinfoFunctionDynamicReturnTypeExtension.php @@ -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()); diff --git a/tests/PHPStan/Analyser/nsrt/pathinfo.php b/tests/PHPStan/Analyser/nsrt/pathinfo.php index 94a5e19d8b..b58886e6d1 100644 --- a/tests/PHPStan/Analyser/nsrt/pathinfo.php +++ b/tests/PHPStan/Analyser/nsrt/pathinfo.php @@ -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)); + } }