"One of array element" type #10347
-
class A
{
/**
* @var string[]
*/
private const MY_CONST_ARRAY = ['foo', 'bar'];
/**
* @return ??? // e.g. one-of-value<self::MY_CONST_ARRAY>
*/
function f(): string
{
return array_rand(self::MY_CONST_ARRAY);
}
} How can I tell PHPStan that my function will return one of a value from MY_CONST_ARRAY array? I know I can write type alias or just duplicate foo and bar strings but this is duplication. The array content is known so PHPStan could access it. |
Beta Was this translation helpful? Give feedback.
Answered by
javaDeveloperKid
Dec 28, 2023
Replies: 1 comment
-
Got it. class A
{
private const MY_CONST_ARRAY = ['foo', 'bar'];
/**
* @return value-of<self::MY_CONST_ARRAY>
*/
function f(): string
{
return array_rand(self::MY_CONST_ARRAY);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
javaDeveloperKid
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Got it.