PhpStorm ExpectedValues/expectedArguments array auto-complete

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.

  • I’m not sure if it’s possible (an array with certain values only). You can define an array shape (array keys) with PHPStan like PHPDoc (that PhpStorm supports).. but I’m not sure about actual values (esp from a limited set — the real-usage case?). Maybe Enums can be used instead here somehow (if it’s a fixed set ofc)?

    – 




  • Actually — 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)

    – 

  • @LazyOne PhpStorm doesn’t seem to support it at all sadly :/

    – 

  • PhpStorm supports the syntax and completion … but for a single value parameter (just like [ExpectedValues()] or expectedArguments()) — 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.

    – 




  • In either case, that likely be not enough for your 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.

    – 

Leave a Comment