Skip to content

Handle conditional return type in ArrayMap #3411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/Type/Php/ArrayMapFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\ConditionalTypeForParameter;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
Expand All @@ -16,6 +17,7 @@
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeUtils;
use function array_slice;
use function count;
Expand Down Expand Up @@ -73,7 +75,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
foreach ($constantArray->getKeyTypes() as $i => $keyType) {
$returnedArrayBuilder->setOffsetValueType(
$keyType,
$valueType,
$this->mapValueType($constantArray->getOffsetValueType($keyType), $valueType),
$constantArray->isOptionalKey($i),
);
}
Expand All @@ -88,24 +90,24 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
} else {
$mappedArrayType = TypeCombinator::intersect(new ArrayType(
$arrayType->getIterableKeyType(),
$valueType,
$this->mapValueType($arrayType->getIterableValueType(), $valueType),
), ...TypeUtils::getAccessoryTypes($arrayType));
}
} elseif ($arrayType->isArray()->yes()) {
$mappedArrayType = TypeCombinator::intersect(new ArrayType(
$arrayType->getIterableKeyType(),
$valueType,
$this->mapValueType($arrayType->getIterableValueType(), $valueType),
), ...TypeUtils::getAccessoryTypes($arrayType));
} else {
$mappedArrayType = new ArrayType(
new MixedType(),
$valueType,
$this->mapValueType($arrayType->getIterableValueType(), $valueType),
);
}
} else {
$mappedArrayType = TypeCombinator::intersect(new ArrayType(
new IntegerType(),
$valueType,
$this->mapValueType($arrayType->getIterableValueType(), $valueType),
), ...TypeUtils::getAccessoryTypes($arrayType));
}

Expand All @@ -116,4 +118,15 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return $mappedArrayType;
}

private function mapValueType(Type $initialValue, Type $returnType): Type
{
return TypeTraverser::map($returnType, static function (Type $type, callable $traverse) use ($initialValue): Type {
if ($type instanceof ConditionalTypeForParameter) {
$type = $type->toConditional($initialValue);
}

return $traverse($type);
});
}

}
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,11 @@ public function testBug3759(): void
$this->analyse([__DIR__ . '/data/bug-3759.php'], []);
}

public function testBug10715(): void
{
$this->analyse([__DIR__ . '/data/bug-10715.php'], []);
}

public function testBug11337(): void
{
$this->analyse([__DIR__ . '/data/bug-11337.php'], []);
Expand Down
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-10715.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

namespace Bug10715;

class Test
{
/**
* @param string|array<string> $word
*
* @return ($word is array<string> ? array<string> : string)
*/
public static function wgtrim(string|array $word): string|array
{
if (\is_array($word)) {
return array_map(static::wgtrim(...), $word);
}

return 'word';
}

/**
* @param array{foo: array<string>, bar: string} $array
*
* @return array{foo: array<string>, bar: string}
*/
public static function example(array $array): array
{
return array_map(static::wgtrim(...), $array);
}
}
Loading