vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.54k stars 659 forks source link

isset on a string key should imply associative array #8979

Open mndevel opened 1 year ago

mndevel commented 1 year ago

I believe checking a string key on a variable can only imply an associative array, numeric keys could be string or array.

  if (isset($input['a']) {
   //input is an array but if it is used as an array psalm complains we do not know the type.
  }

update: Anything multi-dimensional probably also implies an array.

  if (isset($input[0][1]) {
   //input is an array but if it is used as an array psalm complains we do not know the type.
  }

https://psalm.dev/r/fda407c85e

psalm-github-bot[bot] commented 1 year ago

I found these snippets:

https://psalm.dev/r/fda407c85e ```php 'value']; } $input = return_array(); if (isset($input['key'])) { accept_array($input); } ``` ``` Psalm output (using commit c75f06e): INFO: MissingReturnType - 8:10 - Method return_array does not have a return type, expecting array{key: 'value'} INFO: MixedAssignment - 13:1 - Unable to determine the type that $input is being assigned to INFO: MixedArgument - 16:16 - Argument 1 of accept_array cannot be mixed, expecting array ```
weirdan commented 1 year ago

It could also enter that if if $input was an object implementing ArrayAccess

weirdan commented 1 year ago

https://3v4l.org/6VfRW

mndevel commented 1 year ago

Right forgot about array access, should probably update my interfaces.