phpstan-src
phpstan-src copied to clipboard
Rework ArrayReplaceFunctionReturnTypeExtension
Close https://github.com/phpstan/phpstan/issues/12828
I basically copy the ArrayMergeFunctionReturnTypeExtension with the following change
https://github.com/phpstan/phpstan-src/blob/4c41073c1dcdf44b6c3c90768c4430e45238b522/src/Type/Php/ArrayMergeFunctionDynamicReturnTypeExtension.php#L87
$keyType instanceof ConstantIntegerType ? null : $keyType, is replaced by $keyType because
- array_merge reindex numeric keys
- array_replace keep all the existing keys
https://github.com/phpstan/phpstan-src/blob/4c41073c1dcdf44b6c3c90768c4430e45238b522/src/Type/Php/ArrayMergeFunctionDynamicReturnTypeExtension.php#L106
if (!(new IntegerType())->isSuperTypeOf($keyType)->yes()) { is replaced by if (!$argType->isList()->yes()) { because of the same reason.
This pull request has been marked as ready for review.
I know I'm maybe a bit fast suggesting this, but we could create a Type::mergeArray() or Type::replaceArray() with a proper $preserveKeys or $reindex arg or whatever and then us it in both return type extensions. And maybe I should have done this already the last time I touched the merge one. Anyway, can be done in a follow-up too IMO, so this comment should not be considered a blocker IMO 😊 and I can volunteer to do this.
Anyway, can be done in a follow-up too IMO, so this comment should not be considered a blocker IMO 😊 and I can volunteer to do this.
Yeah, I would prefer this to be done in a follow up PR to separate (and validate separately by ondrej)
- The new implementation
- The refactoring
/cc @herndlm Waiting for LGTM from you here :)
I guess this is fine. And also brings value of course. Let's see if we can refactor array_merge and array_replace stuff a bit as follow-up I'd say :)
Thank you.