Home > Mobile >  Share arguments list between multiple PHPSTORM_META\expectedArguments
Share arguments list between multiple PHPSTORM_META\expectedArguments

Time:06-18

I'm trying to use the .phpstorm.meta.php file to manually define intellisense to a function I use to get and set dynamic settings in the database.

So imagine a class that looks like this:

class Setting
{
    public function get(string $key) {
        // ...
    }

    public function set(string $key, $value) {
        // ...
    }
}

Then, to define the possible values that can go in the $key argument, I can do this in the meta file:

namespace PHPSTORM_META {
    expectedArguments(
        \App\Models\Setting::get(),
        0,
        'foo',
        'bar',
        'baz'
    );

    expectedArguments(
        \App\Models\Setting::set(),
        0,
        'foo',
        'bar',
        'baz'
    );
}

The thing is, the list of possible values is quite extensive, and duplicating them is a pain. Is there a way to define these values on multiple functions using the same statement? Or maybe put them in an array and use it somehow? I couldn't get it to work.

Thanks!

PhpStorm version: 2021.3.3.

CodePudding user response:

Sure, PhpStorm's Advanced Metadata has named sets of arguments feature that does just that: allows to define sets of arguments using registerArgumentsSet() and then use it in multiple places using argumentsSet().

It is used for quite a few core PHP functions, for example ini_get() and ini_set() etc.

In your case it will be something like this:

namespace PHPSTORM_META {
    registerArgumentsSet('ModelSettingArgs', 'foo', 'bar', 'baz');

    expectedArguments(
        \App\Models\Setting::get(),
        0,
        argumentsSet('ModelSettingArgs')
    );

    expectedArguments(
        \App\Models\Setting::set(),
        0,
        argumentsSet('ModelSettingArgs')
    );
}

PhpStorm Advanced Metadata help page: https://www.jetbrains.com/help/phpstorm/ide-advanced-metadata.html#arguments-set

  • Related