Skip to content

ConstantArray is not aware of the order of the key #11600

@VincentLanglet

Description

@VincentLanglet

Bug report

This might be both a bugfix and a feature request...

While investigating on the issue I discovered a current behavior/shortcut which may introduce multiple bugs with constant arrays and I dunno if you already aware about this @ondrejmirtes.

Part of the PHPStan code consider that the keys of ConstantArray are given in the right order, and part of the code doesn't.

Code snippet that reproduces the problem

https://phpstan.org/r/c6807da5-b730-441c-966e-a3ede09d56e4

Expected output

In the given snippet,

  • either HelloWorld::sayHello($b); should be invalid,
  • either array_is_list call shouldn't be considered as always true.

As an example Psalm makes a difference between array{0: 1, 1: 2} which can be unordered and array{1, 2} which is a list
https://psalm.dev/r/163c323bc0
https://psalm.dev/r/08ec03609f

But the problem also exists with ConstantArray which are not lists, like
https://phpstan.org/r/a8c8d470-b902-4b97-b176-e2508ccf29b6
vs psalm behavior https://psalm.dev/r/cd63983ce9

I feel like the easiest path to a fix could maybe

  • Do not resolve array{0: 1, 1: 2} as array{1, 2} like psalm
  • Fix place where ConstantArray is considered as ordered
  • Maybe introduce a $isOrdered param to the ConstantArrayType ?

but this might require lot of work...

Did PHPStan help you today? Did it make you happy in any way?

PHPStan is really a great tool I use everyday. I can't imagine working on a PHP codebase without it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions