cspray,
@cspray@phpc.social avatar

I have found myself, for the first time, having to write what I consider worse code to satisfy Psalm static analysis. Here's a watered-down example showing what I consider odd behavior.

https://psalm.dev/r/5862a290e2

I'm not certain why the 2nd call to doSomething() is valid but the 1st call is not.

sidawson,
@sidawson@mastodon.nz avatar

@cspray Oh, and to your specific example, if phpstan is anything to go by, it seems php stores non associative arrays internally AS associative, it’s just that the keys are indexes (0, 1, 2…) so that’s the “expects array <int, string >, was provided array<string >” malarkey.

In phpstan you can go either array<int, string> (or array<array-key, string>), or, more simply string[] - but the latter form avoids those sorts of complaints. Might be worth having a looksee if psalm is similar.

alessandrolai,
@alessandrolai@phpc.social avatar

@sidawson @cspray that's not the issue here. The same snippet should trigger PHPStan errors too, because it was the starting definition too strict, since it requires "list".

List is an array with only consecutive integer keys. If you don't re-index (and the spread operator does) you don't have a (guaranteed) list anymore.

herndlm,
@herndlm@phpc.social avatar

@cspray I don't understand or don't see the issue :) this is about the non-indexed array keys a list needs, right? array_unique does not re-index, while using the spread basically does I guess?

cspray,
@cspray@phpc.social avatar

@herndlm This turned out leading me to what I was missing. I was operating under the assumption that the spread operator does not re-index keys. Turns out it does re-index integer keys and explains the behavior I'm seeing. Thanks!

  • All
  • Subscribed
  • Moderated
  • Favorites
  • php
  • kavyap
  • InstantRegret
  • ethstaker
  • DreamBathrooms
  • mdbf
  • magazineikmin
  • thenastyranch
  • Youngstown
  • tacticalgear
  • slotface
  • Durango
  • khanakhh
  • rosin
  • everett
  • Leos
  • vwfavf
  • normalnudes
  • osvaldo12
  • cubers
  • GTA5RPClips
  • cisconetworking
  • ngwrru68w68
  • anitta
  • provamag3
  • tester
  • modclub
  • megavids
  • JUstTest
  • All magazines