Home > database >  Function hint - possible values for parameters
Function hint - possible values for parameters

Time:11-14

Is it possible to show a user of PhpStorm what the possible function arguments are, not just the type of argument?

For example, I have a function that will show the programmer that two strings are needed, as shown in this graphic:

enter image description here

However, I would like the hint to show the possible values for each variable - tag and tag_type in this example; e.g.,

  • The possible values for tag are "full, view, edit, add, or delete".
  • The possible values for variable tag_type is a list of about 10 or so activities in the database.

Here is the code I have. Can it be changed to show the user in PhpStorm what the allowed variable values are?

/**
 * @param string $tag
 * @param string $tag_type
 * @return int
 *
 * [tag] = full, view, edit, add, or delete
 * [type] = all, activities, grades, orders, people, schools, users, team_classes,
 *          coaches, team_parents, or organization
 *
 * by default, if leave arguments blank, you are asking if the current user is a full admin,
 */
public function isAdmin(string $tag = '', string $tag_type = ''){

    if ($this->isFullAdmin())
        return true;

    return $this->tagas()
        ->where('tag', '=', 'full')
        ->where('tag_type', '=', $tag_type)
        ->orWhere(function($query) use ($tag, $tag_type) {
            $query->where('tag','=',$tag)
                  ->where('tag_type','=', $tag_type);
        })->count();
}

Okay, I came up with this, but it doesn't seem to be working. I checked my php version with phpversion() and it is 8.0.2.

    public function isAdmin(
        #[ExpectedValues(['all', 'activities', 'grades', 'orders', 'people', 'schools', 'users',
            'team_classes','coaches', 'team_parents', 'organization'])] string $tag_type = '',
        #[ExpectedValues(['*', 'full', 'view', 'edit', 'add', 'delete'])] string $tag = '*'
    ){

Yet it has only changed the type hinting slightly. enter image description here

It shows [ExpectedValues] but doesn't show the actual expected values?

CodePudding user response:

You can use PhpStorm Advanced Metadata. In particular: Arguments Accepted by a Method functionality -- enter image description here


For PHP 8 you can use custom #[ExpectedValues] PHP attribute made by JetBrains:

  • enter image description here

    The IDE can also hint you that an unexpected value is used:
    enter image description here

    NOTE: it's a Weak Warning severity so it's not super visible (you may adjust that in the inspection settings):
    enter image description here

  • Related