-
Notifications
You must be signed in to change notification settings - Fork 510
Rework ArrayReplaceFunctionReturnTypeExtension #3958
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
base: 1.12.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,12 +5,21 @@ | |||||||||||||||||||||||||||||||||||||||||||
use PhpParser\Node\Expr\FuncCall; | ||||||||||||||||||||||||||||||||||||||||||||
use PHPStan\Analyser\Scope; | ||||||||||||||||||||||||||||||||||||||||||||
use PHPStan\Reflection\FunctionReflection; | ||||||||||||||||||||||||||||||||||||||||||||
use PHPStan\TrinaryLogic; | ||||||||||||||||||||||||||||||||||||||||||||
use PHPStan\Type\Accessory\AccessoryArrayListType; | ||||||||||||||||||||||||||||||||||||||||||||
use PHPStan\Type\Accessory\NonEmptyArrayType; | ||||||||||||||||||||||||||||||||||||||||||||
use PHPStan\Type\ArrayType; | ||||||||||||||||||||||||||||||||||||||||||||
use PHPStan\Type\Constant\ConstantArrayType; | ||||||||||||||||||||||||||||||||||||||||||||
use PHPStan\Type\Constant\ConstantArrayTypeBuilder; | ||||||||||||||||||||||||||||||||||||||||||||
use PHPStan\Type\Constant\ConstantIntegerType; | ||||||||||||||||||||||||||||||||||||||||||||
use PHPStan\Type\Constant\ConstantStringType; | ||||||||||||||||||||||||||||||||||||||||||||
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||||||||||||||||||||||||||||||||||||||||||||
use PHPStan\Type\NeverType; | ||||||||||||||||||||||||||||||||||||||||||||
use PHPStan\Type\Type; | ||||||||||||||||||||||||||||||||||||||||||||
use PHPStan\Type\TypeCombinator; | ||||||||||||||||||||||||||||||||||||||||||||
use function array_keys; | ||||||||||||||||||||||||||||||||||||||||||||
use function count; | ||||||||||||||||||||||||||||||||||||||||||||
use function in_array; | ||||||||||||||||||||||||||||||||||||||||||||
use function strtolower; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
final class ArrayReplaceFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -23,54 +32,107 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo | |||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type | ||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||
$arrayTypes = $this->collectArrayTypes($functionCall, $scope); | ||||||||||||||||||||||||||||||||||||||||||||
$args = $functionCall->getArgs(); | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if (count($arrayTypes) === 0) { | ||||||||||||||||||||||||||||||||||||||||||||
if (!isset($args[0])) { | ||||||||||||||||||||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
return $this->getResultType(...$arrayTypes); | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
$argTypes = []; | ||||||||||||||||||||||||||||||||||||||||||||
$optionalArgTypes = []; | ||||||||||||||||||||||||||||||||||||||||||||
foreach ($args as $arg) { | ||||||||||||||||||||||||||||||||||||||||||||
$argType = $scope->getType($arg->value); | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
private function getResultType(Type ...$arrayTypes): Type | ||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||
$keyTypes = []; | ||||||||||||||||||||||||||||||||||||||||||||
$valueTypes = []; | ||||||||||||||||||||||||||||||||||||||||||||
$nonEmptyArray = false; | ||||||||||||||||||||||||||||||||||||||||||||
foreach ($arrayTypes as $arrayType) { | ||||||||||||||||||||||||||||||||||||||||||||
if (!$nonEmptyArray && $arrayType->isIterableAtLeastOnce()->yes()) { | ||||||||||||||||||||||||||||||||||||||||||||
$nonEmptyArray = true; | ||||||||||||||||||||||||||||||||||||||||||||
if ($arg->unpack) { | ||||||||||||||||||||||||||||||||||||||||||||
if ($argType->isConstantArray()->yes()) { | ||||||||||||||||||||||||||||||||||||||||||||
foreach ($argType->getConstantArrays() as $constantArray) { | ||||||||||||||||||||||||||||||||||||||||||||
foreach ($constantArray->getValueTypes() as $valueType) { | ||||||||||||||||||||||||||||||||||||||||||||
$argTypes[] = $valueType; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||
$argTypes[] = $argType->getIterableValueType(); | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if (!$argType->isIterableAtLeastOnce()->yes()) { | ||||||||||||||||||||||||||||||||||||||||||||
// unpacked params can be empty, making them optional | ||||||||||||||||||||||||||||||||||||||||||||
$optionalArgTypesOffset = count($argTypes) - 1; | ||||||||||||||||||||||||||||||||||||||||||||
foreach (array_keys($argTypes) as $key) { | ||||||||||||||||||||||||||||||||||||||||||||
$optionalArgTypes[] = $optionalArgTypesOffset + $key; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||
$argTypes[] = $argType; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
$keyTypes[] = $arrayType->getIterableKeyType(); | ||||||||||||||||||||||||||||||||||||||||||||
$valueTypes[] = $arrayType->getIterableValueType(); | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
$keyType = TypeCombinator::union(...$keyTypes); | ||||||||||||||||||||||||||||||||||||||||||||
$valueType = TypeCombinator::union(...$valueTypes); | ||||||||||||||||||||||||||||||||||||||||||||
$allConstant = TrinaryLogic::createYes()->lazyAnd( | ||||||||||||||||||||||||||||||||||||||||||||
$argTypes, | ||||||||||||||||||||||||||||||||||||||||||||
static fn (Type $argType) => $argType->isConstantArray(), | ||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if ($allConstant->yes()) { | ||||||||||||||||||||||||||||||||||||||||||||
$newArrayBuilder = ConstantArrayTypeBuilder::createEmpty(); | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
foreach ($argTypes as $argType) { | ||||||||||||||||||||||||||||||||||||||||||||
/** @var array<int|string, ConstantIntegerType|ConstantStringType> $keyTypes */ | ||||||||||||||||||||||||||||||||||||||||||||
$keyTypes = []; | ||||||||||||||||||||||||||||||||||||||||||||
foreach ($argType->getConstantArrays() as $constantArray) { | ||||||||||||||||||||||||||||||||||||||||||||
foreach ($constantArray->getKeyTypes() as $keyType) { | ||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is what you want. Let's say you have You'll end up with two values in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure to understand your point. This is the current behavior we have with array_replace vs array_merge Since both function are working almost the same way, I copied and adapt the logic from ArrayMergeFunctionReturnTypeExtension
In which we're doing
With this PR, we're now getting
with both array_merge and array_replace |
||||||||||||||||||||||||||||||||||||||||||||
$keyTypes[$keyType->getValue()] = $keyType; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
foreach ($keyTypes as $keyType) { | ||||||||||||||||||||||||||||||||||||||||||||
$newArrayBuilder->setOffsetValueType( | ||||||||||||||||||||||||||||||||||||||||||||
$keyType, | ||||||||||||||||||||||||||||||||||||||||||||
$argType->getOffsetValueType($keyType), | ||||||||||||||||||||||||||||||||||||||||||||
!$argType->hasOffsetValueType($keyType)->yes(), | ||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
$arrayType = new ArrayType($keyType, $valueType); | ||||||||||||||||||||||||||||||||||||||||||||
return $nonEmptyArray ? TypeCombinator::intersect($arrayType, new NonEmptyArrayType()) : $arrayType; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
return $newArrayBuilder->getArray(); | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||
* @return Type[] | ||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||
private function collectArrayTypes(FuncCall $functionCall, Scope $scope): array | ||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||
$args = $functionCall->getArgs(); | ||||||||||||||||||||||||||||||||||||||||||||
$keyTypes = []; | ||||||||||||||||||||||||||||||||||||||||||||
$valueTypes = []; | ||||||||||||||||||||||||||||||||||||||||||||
$nonEmpty = false; | ||||||||||||||||||||||||||||||||||||||||||||
$isList = true; | ||||||||||||||||||||||||||||||||||||||||||||
foreach ($argTypes as $key => $argType) { | ||||||||||||||||||||||||||||||||||||||||||||
$keyType = $argType->getIterableKeyType(); | ||||||||||||||||||||||||||||||||||||||||||||
$keyTypes[] = $keyType; | ||||||||||||||||||||||||||||||||||||||||||||
$valueTypes[] = $argType->getIterableValueType(); | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if (!$argType->isList()->yes()) { | ||||||||||||||||||||||||||||||||||||||||||||
$isList = false; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
$arrayTypes = []; | ||||||||||||||||||||||||||||||||||||||||||||
foreach ($args as $arg) { | ||||||||||||||||||||||||||||||||||||||||||||
$argType = $scope->getType($arg->value); | ||||||||||||||||||||||||||||||||||||||||||||
if (!$argType->isArray()->yes()) { | ||||||||||||||||||||||||||||||||||||||||||||
if (in_array($key, $optionalArgTypes, true) || !$argType->isIterableAtLeastOnce()->yes()) { | ||||||||||||||||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
$arrayTypes[] = $arg->unpack ? $argType->getIterableValueType() : $argType; | ||||||||||||||||||||||||||||||||||||||||||||
$nonEmpty = true; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
$keyType = TypeCombinator::union(...$keyTypes); | ||||||||||||||||||||||||||||||||||||||||||||
if ($keyType instanceof NeverType) { | ||||||||||||||||||||||||||||||||||||||||||||
return new ConstantArrayType([], []); | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
$arrayType = new ArrayType( | ||||||||||||||||||||||||||||||||||||||||||||
$keyType, | ||||||||||||||||||||||||||||||||||||||||||||
TypeCombinator::union(...$valueTypes), | ||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if ($nonEmpty) { | ||||||||||||||||||||||||||||||||||||||||||||
$arrayType = TypeCombinator::intersect($arrayType, new NonEmptyArrayType()); | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
if ($isList) { | ||||||||||||||||||||||||||||||||||||||||||||
$arrayType = TypeCombinator::intersect($arrayType, new AccessoryArrayListType()); | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
return $arrayTypes; | ||||||||||||||||||||||||||||||||||||||||||||
return $arrayType; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug12828; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
$a = ['abc' => 'def', 'hello' => 'world']; | ||
assertType("array{abc: 'def', hello: 'world'}", $a); | ||
$a = array_replace($a, ['hello' => 'country']); | ||
assertType("array{abc: 'def', hello: 'country'}", $a); |
Uh oh!
There was an error while loading. Please reload this page.