Is it possible to use the ExpectedValues trait or the expectedArgument() meta directive to hint the possible values in an array?
My use-case is a function that accepts either an array or a string:
function check(array|string $checkable): bool
{
if (!is_array($checkable)) {
$checkable = [$checkable];
}
foreach ($checkable as $item) {
if (!in_array($item, ['value1', 'value2', 'value anything'])) {
return false;
}
}
return true;
}
Here I wanted PhpStorm to auto-complete with the values in the array (['value1', 'value2', 'value anything']
) when I use the function, both when passing an array of values and a value directly.
For just a single value this is already possible using #[ExpectedValues()] or expectedArguments().
function check(
#[ExpectedValues(values: ['value1', 'value2', 'value anything'])] array|string $checkable
): bool
But when passing an array it does nothing.
value-of<Type::ARRAY_CONST>
-- phpstan.org/writing-php-code/… --- try and see if PhpStorm supports this in full (both recognizing the stuff and in code completion -- the later might not be the case)[ExpectedValues()]
orexpectedArguments()
) -- check this comment youtrack.jetbrains.com/issue/… . NOTE: the support is provided by the bundled Pslam plugin. It must be enabled even if you do not use the actual Pslam -- it's about the IDE side of things for this syntax.array()
needs. If you get no better suggestions here, I suggest you file a Feature request ticket to PhpStorm at youtrack.jetbrains.com/newIssue?project=WI and see what they have to say.