phpstan-src icon indicating copy to clipboard operation
phpstan-src copied to clipboard

Rework ArrayReplaceFunctionReturnTypeExtension

Open VincentLanglet opened this issue 7 months ago • 5 comments
trafficstars

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.

VincentLanglet avatar Apr 21 '25 18:04 VincentLanglet

This pull request has been marked as ready for review.

phpstan-bot avatar Apr 22 '25 08:04 phpstan-bot

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.

herndlm avatar Apr 22 '25 08:04 herndlm

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

VincentLanglet avatar Apr 22 '25 09:04 VincentLanglet

/cc @herndlm Waiting for LGTM from you here :)

ondrejmirtes avatar Apr 28 '25 12:04 ondrejmirtes

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 :)

herndlm avatar Apr 28 '25 13:04 herndlm

Thank you.

ondrejmirtes avatar Jul 17 '25 12:07 ondrejmirtes