psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Type lost after array_walk

Open zmitic opened this issue 2 years ago • 3 comments

https://psalm.dev/r/c061438453

The echo 'break' is here only for psalm to correctly trace $input, cannot remember the rule for that, sorry.

zmitic avatar Aug 02 '22 10:08 zmitic

I found these snippets:

https://psalm.dev/r/c061438453
<?php

/** @var array<string> $input */
$input = [
	'a', 'b', 'c'    
];
/** @psalm-trace $input */


echo 'break';


array_walk($input, fn(string $x) => trim($x));

/** @psalm-trace $input */
Psalm output (using commit 24f7920):

INFO: Trace - 10:1 - $input: array<array-key, string>

INFO: Trace - 15:27 - $input: array<array-key, mixed>

psalm-github-bot[bot] avatar Aug 02 '22 10:08 psalm-github-bot[bot]

I thought we had something to handle this, but it looks like I was mistaken, we only do array_map. We don't even have a check for the callable's argument types right now: https://psalm.dev/r/6172df579d

It might be possible to pull out some of the stuff from ArrayMapReturnTypeProvider.php and reuse it for array_walk (probably add some stuff in NamedFunctionCallHandler.php where we're checking for object iteration).

The echo 'break' is here only for psalm to correctly trace $input, cannot remember the rule for that, sorry.

You can just add a semicolon to create an empty statement for the docblock to be applied to: https://psalm.dev/r/f973c0b7e5

AndrolGenhald avatar Aug 02 '22 15:08 AndrolGenhald

I found these snippets:

https://psalm.dev/r/6172df579d
<?php

/** @var array<string> $input */
$input = [
	'a', 'b', 'c'    
];
/** @psalm-trace $input */


echo 'break';


array_walk($input, fn(int $x) => $x + 1);

/** @psalm-trace $input */
Psalm output (using commit 24f7920):

INFO: Trace - 10:1 - $input: array<array-key, string>

INFO: Trace - 15:27 - $input: array<array-key, mixed>
https://psalm.dev/r/f973c0b7e5
<?php

/** @var array<string> $input */
$input = [
	'a', 'b', 'c'    
];
/** @psalm-trace $input */;

array_walk($input, fn(string $x) => trim($x));

/** @psalm-trace $input */;
Psalm output (using commit 24f7920):

INFO: Trace - 7:27 - $input: array<array-key, string>

INFO: Trace - 11:27 - $input: array<array-key, mixed>

psalm-github-bot[bot] avatar Aug 02 '22 15:08 psalm-github-bot[bot]