0

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.

0

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.