psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Edit an array, member of a Weakmap is wrongly thought to be assigned to the Weakmap member itself

Open mathroc opened this issue 1 year ago • 1 comments

I don't know how to explain it better than the issue title :sweat_smile:

https://psalm.dev/r/79578e4ab5

not sure it's only related to Weakmap, but it works when using a plain array

mathroc avatar Oct 15 '24 12:10 mathroc

I found these snippets:

https://psalm.dev/r/79578e4ab5
<?php

/**
 * @param array<string,array<string,int>> $wm
 */
function g(array $wm, string $k, int $i, string $s): void {
  $wm[$k] ??= [];
  $wm[$k][$s] ??= 0;
  $wm[$k][$s]++;
}

/**
 * @param \WeakMap<object,array<string,int>> $wm
 */
function f1(\Weakmap $wm, object $k, int $i, string $s): void {
  $wm[$k] ??= [];
  $wm[$k][$s] ??= 0;
  $wm[$k][$s]++;
}

/**
 * @param \WeakMap<object,array<string,int>> $wm
 */
function f2(\Weakmap $wm, object $k, int $i, string $s): void {
  $wm[$k] ??= [];
  $a = $wm[$k];
  $a[$s] ??= 0;
  $a[$s]++;
}
Psalm output (using commit 03ee02c):

INFO: MixedAssignment - 17:3 - Unable to determine the type of this assignment

INFO: MixedArgument - 17:3 - Argument 2 of WeakMap::offsetSet cannot be 0|mixed, expecting array<string, int>

ERROR: PossiblyInvalidArgument - 17:3 - Argument 2 of WeakMap::offsetSet expects array<string, int>, but possibly different type 0|mixed provided

INFO: MixedOperand - 18:3 - Left operand cannot be mixed

INFO: MixedAssignment - 18:3 - Unable to determine the type of this assignment

INFO: MixedArgument - 18:3 - Argument 2 of WeakMap::offsetSet cannot be mixed, expecting array<string, int>

psalm-github-bot[bot] avatar Oct 15 '24 12:10 psalm-github-bot[bot]