-
Notifications
You must be signed in to change notification settings - Fork 659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Psalm does not derive correct element type of array shape member #9801
Comments
I found these snippets: https://psalm.dev/r/64389d2bd0<?php
/**
* @psalm-type PropTypes = array{
* 'lkey'?: list<array{'content-type': string, version: string}>,
* 'skey'?: string,
* }
*/
class Test {
/**
* @psalm-var PropTypes
*/
public $props = [];
public function storeProperty(string $name): void
{
switch ($name) {
case 'lkey':
if (!isset($this->props[$name])) {
$this->props[$name] = [];
}
$addrData = [ 'content-type' => 'text/vcard', 'version' => '3.0' ];
$this->props[$name][] = $addrData;
break;
}
}
public function storePropertyWorks(string $name): void
{
switch ($name) {
case 'lkey':
if (!isset($this->props[$name])) {
$this->props[$name] = [];
}
$addrData = [ 'content-type' => 'text/vcard', 'version' => '3.0' ];
$this->props['lkey'][] = $addrData;
break;
}
}
}
|
@ptomulik if you're able to take a look, it would be very helpful :) |
I guess, we shall start from here: #9439 (comment) |
Found another variant for this bug, but this time it seems to be independent of 2327917: |
I found these snippets: https://psalm.dev/r/338c7bae15<?php
/**
* @psalm-type PropTypes = array{
* lkey?: list<array{'content-type': string, version: string}>,
* skey?: string,
* }
*/
class Test {
/**
* @psalm-var PropTypes
*/
public $props = [];
public function storeProperty(string $name): void
{
switch ($name) {
case 'lkey':
if (!isset($this->props[$name])) { }
$addrData = [ 'content-type' => 'text/vcard', 'version' => '3.0' ];
$this->props[$name][] = $addrData;
break;
}
}
public function storePropertyWorks(string $name): void
{
switch ($name) {
case 'lkey':
$addrData = [ 'content-type' => 'text/vcard', 'version' => '3.0' ];
$this->props[$name][] = $addrData;
break;
}
}
}
|
This gets more and more interesting, deleted https://psalm.dev/r/5223e6b4cd now get it back, but change the, seeminly unrelated https://psalm.dev/r/1e02b68bcd looks like types from different keys of array interfere here, reverting 2327917 doesn't help |
I found these snippets: https://psalm.dev/r/5223e6b4cd<?php
/**
* @psalm-type PropTypes = array{
* lkey?: list<array{'content-type': string, version: string}>,
* }
*/
class Test {
/**
* @psalm-var PropTypes
*/
public $props = [];
public function storeProperty(string $name): void
{
switch ($name) {
case 'lkey':
if (!isset($this->props[$name])) { }
$addrData = [ 'content-type' => 'text/vcard', 'version' => '3.0' ];
$this->props[$name][] = $addrData;
break;
}
}
public function storePropertyWorks(string $name): void
{
switch ($name) {
case 'lkey':
$addrData = [ 'content-type' => 'text/vcard', 'version' => '3.0' ];
$this->props[$name][] = $addrData;
break;
}
}
}
https://psalm.dev/r/1e02b68bcd<?php
/**
* @psalm-type PropTypes = array{
* lkey?: list<array{'content-type': string, version: string}>,
* skey?: int
* }
*/
class Test {
/**
* @psalm-var PropTypes
*/
public $props = [];
public function storeProperty(string $name): void
{
switch ($name) {
case 'lkey':
if (!isset($this->props[$name])) { }
$addrData = [ 'content-type' => 'text/vcard', 'version' => '3.0' ];
$this->props[$name][] = $addrData;
break;
}
}
public function storePropertyWorks(string $name): void
{
switch ($name) {
case 'lkey':
$addrData = [ 'content-type' => 'text/vcard', 'version' => '3.0' ];
$this->props[$name][] = $addrData;
break;
}
}
}
|
|
I found these snippets: https://psalm.dev/r/1149743dfb<?php
/**
* @psalm-type PropTypes = array{
* lkey?: list<array{'content-type': string, version: string}>,
* skey?: int
* }
*/
class Test {
/**
* @psalm-var PropTypes
*/
public $props = [];
public function storeProperty(string $name): void
{
switch ($name) {
case 'lkey':
if (!isset($this->props[$name])) { }
$addrData = [ 'content-type' => 'text/vcard', 'version' => '3.0' ];
$this->props[$name . ''][] = $addrData;
$this->props[$name][] = $addrData;
break;
}
}
}
|
Hello,
after upgrading to psalm 5.11, I am getting a spurious error that did not occur with a previous 5.x version of psalm (unfortunately I do not know the exact version I used before, I think I upgraded last around January 2023).
I tried to condense the code snippet to something minimal:
https://psalm.dev/r/64389d2bd0
Inside the switch, psalm knows the value of
$name
an carries it as a string constant type. Therefore it should be able to know which member of the array shape is accessed when using$name
as index, but apparently it does not. When I use the string literal as index instead (see second variant of the function), it works.The text was updated successfully, but these errors were encountered: