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

Unable to use class constants as array-key type #11142

Open
marcosh opened this issue Oct 25, 2024 · 1 comment
Open

Unable to use class constants as array-key type #11142

marcosh opened this issue Oct 25, 2024 · 1 comment

Comments

@marcosh
Copy link
Contributor

marcosh commented Oct 25, 2024

https://psalm.dev/r/f63080e0d7

With code

class Foo
{
	public const string BAR = 'asdf';
}

/**
 * @var array{Foo::BAR : string} $a
 */
$a = [Foo::BAR => 'ewqrtq'];

I get the error

ERROR: [InvalidDocblock](https://psalm.dev/008) - 11:1 - array{Foo::BAR : string} is not a valid type (:: in array key is only allowed for ::class

Is there a way to use a class constant as array key in a type definition?

Copy link

I found these snippets:

https://psalm.dev/r/f63080e0d7
<?php

class Foo
{
	public const string BAR = 'asdf';
}

/**
 * @var array{Foo::BAR : string} $a
 */
$a = [Foo::BAR => 'ewqrtq'];
Psalm output (using commit 03ee02c):

ERROR: InvalidDocblock - 11:1 - array{Foo::BAR : string} is not a valid type (:: in array key is only allowed for ::class in /var/www/vhosts/psalm.dev/httpdocs/src/somefile.php:8)

INFO: UnusedVariable - 11:1 - $a is never referenced or the value is not used

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

1 participant