Skip to content
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

Closed
Chrico opened this issue May 19, 2020 · 4 comments
Closed

Annotations - constants as array-key not working #3399

Chrico opened this issue May 19, 2020 · 4 comments

Comments

@Chrico
Copy link

Chrico commented May 19, 2020

[!] 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

@psalm-github-bot
Copy link

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];
}
Psalm output (using commit 666cc3b):

No issues!
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']; 
}
Psalm output (using commit 666cc3b):

ERROR: InvalidReturnStatement - 21:12 - The inferred type 'array{x: string(foo)}' does not match the declared return type 'array{Foo::MY_KEY: string}' for test__requiredArrayKey

ERROR: InvalidReturnType - 18:12 - The declared return type 'array{Foo::MY_KEY: string}' for test__requiredArrayKey is incorrect, got 'array{x: string(foo)}'

ERROR: InvalidReturnStatement - 29:12 - The inferred type 'array{x: string(foo)}' does not match the declared return type 'array{Foo::MY_*: string}' for test__wildcardArrayKey

ERROR: InvalidReturnType - 26:12 - The declared return type 'array{Foo::MY_*: string}' for test__wildcardArrayKey is incorrect, got 'array{x: string(foo)}'

@Chrico Chrico changed the title Annotations constants as array-key not working Annotations - constants as array-key not working May 19, 2020
@weirdan
Copy link
Collaborator

weirdan commented May 19, 2020

This https://psalm.dev/r/edc3d9f34b works (but all keys are treated as optional).

@psalm-github-bot
Copy link

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']; 
}
Psalm output (using commit 666cc3b):

No issues!

muglug added a commit that referenced this issue May 19, 2020
@muglug
Copy link
Collaborator

muglug commented May 19, 2020

This shouldn't work – array keys in array{...} annotations must be explicit (they can't be indirect). I've updated Psalm to treat array{SomeClass::CONST: string} as a docblock bug.

@muglug muglug closed this as completed May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants