Skip to content

Commit

Permalink
Merge branch refs/heads/1.11.x into 1.12.x
Browse files Browse the repository at this point in the history
  • Loading branch information
phpstan-bot authored Jul 14, 2024
2 parents bf18ba1 + a0dc9ed commit 928e50d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/Type/Php/PathinfoFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,24 @@ public function getTypeFromFunctionCall(
}

$flagsType = $scope->getType($functionCall->getArgs()[1]->value);
if ($flagsType instanceof ConstantIntegerType) {
if ($flagsType->getValue() === $this->getConstant('PATHINFO_ALL')) {
return $arrayType;

$scalarValues = $flagsType->getConstantScalarValues();
if ($scalarValues !== []) {
$pathInfoAll = $this->getConstant('PATHINFO_ALL');
if ($pathInfoAll === null) {
return null;
}

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

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

return TypeCombinator::union($arrayType, new StringType());
Expand Down
13 changes: 12 additions & 1 deletion tests/PHPStan/Analyser/nsrt/pathinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@

use function PHPStan\Testing\assertType;

function doFoo(string $s, int $i) {
/**
* @param PATHINFO_BASENAME|PATHINFO_EXTENSION $flag
*/
function doFoo(string $s, int $i, $flag) {
assertType('array{dirname?: string, basename: string, extension?: string, filename: string}|string', pathinfo($s, $i));
assertType('array{dirname?: string, basename: string, extension?: string, filename: string}', pathinfo($s));

assertType('string', pathinfo($s, PATHINFO_DIRNAME));
assertType('string', pathinfo($s, PATHINFO_BASENAME));
assertType('string', pathinfo($s, PATHINFO_EXTENSION));
assertType('string', pathinfo($s, PATHINFO_FILENAME));

assertType('string', pathinfo($s, $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 928e50d

Please sign in to comment.