-
Notifications
You must be signed in to change notification settings - Fork 667
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
Annotations - constants as array-key not working #3399
Comments
I found these snippets: https://psalm.dev/r/00af5dfd5f<?php
class Foo {
public const MY_KEY = 'x';
}
/**
* A class constant as list array-value does work and is resolved
*
* @return list<Foo::MY_KEY>
*/
function test__arrayValue(): array {
return [Foo::MY_KEY];
}
/**
* ...even with "wildcard".
*
* @return list<Foo::MY_*>
*/
function test__arrayValueWildcard(): array {
return [Foo::MY_KEY];
}
https://psalm.dev/r/593314e3a1<?php
class Foo {
public const MY_KEY = 'x';
}
/**
* A class constant as array-key with "optional" works
*
* @return array{Foo::MY_KEY?:string}
*/
function test__optionalArrayKey(): array {
return [Foo::MY_KEY => 'foo'];
}
/**
* ...but an multidimensional array with class constant as array-key required not work.
*
* @return array{Foo::MY_KEY:string}
*/
function test__requiredArrayKey(): array {
return [Foo::MY_KEY => 'foo'];
}
/**
* Same for wildcard class constant as array-key
*
* @return array{Foo::MY_*:string}
*/
function test__wildcardArrayKey(): array {
return [Foo::MY_KEY => 'foo'];
}
|
Chrico
changed the title
Annotations constants as array-key not working
Annotations - constants as array-key not working
May 19, 2020
This https://psalm.dev/r/edc3d9f34b works (but all keys are treated as optional). |
I found these snippets: https://psalm.dev/r/edc3d9f34b<?php
class Foo {
public const MY_KEY = 'x';
}
/**
* A class constant as array-key with "optional" works
*
* @return array<Foo::MY_KEY,string>
*/
function test__optionalArrayKey(): array {
return [Foo::MY_KEY => 'foo'];
}
/**
* ...but an multidimensional array with class constant as array-key required not work.
*
* @return array<Foo::MY_KEY,string>
*/
function test__requiredArrayKey(): array {
return [Foo::MY_KEY => 'foo'];
}
/**
* Same for wildcard class constant as array-key
*
* @return array<Foo::MY_*,string>
*/
function test__wildcardArrayKey(): array {
return [Foo::MY_KEY => 'foo'];
}
|
This shouldn't work – array keys in |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[!] Note: This issue is a duplicate of #2836 (comment) -but since the #2836 is closed and no reaction, i'd like to push the visibility of the topic.
Problem:
As far as i can see, in
list<>
class constants are working as array-value.But when using
array{}
, they are not working as array-key.Is there any reason why this is not supported? Are there technical restrictions to implement this?
Test cases:
✔️ List: https://psalm.dev/r/00af5dfd5f
❌ Array: https://psalm.dev/r/593314e3a1
@muglug
The text was updated successfully, but these errors were encountered: