【问题标题】:Function hint - possible values for parameters函数提示 - 参数的可能值
【发布时间】:2021-12-25 14:05:09
【问题描述】:

是否可以向 PhpStorm 的用户展示可能的函数参数是什么,而不仅仅是参数的类型?

例如,我有一个函数会告诉程序员需要两个字符串,如下图所示:

但是,我希望提示显示每个变量的可能值 - 在此示例中为 tagtag_type;例如,

  • 标签的可能值为“完整、查看、编辑、添加或删除”。
  • 变量 tag_type 的可能值是数据库中大约 10 个左右活动的列表。

这是我的代码。是否可以更改为在 PhpStorm 中向用户显示允许的变量值是什么?

/**
 * @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();
}

好的,我想出了这个,但它似乎不起作用。我用 phpversion() 检查了我的 php 版本,它是 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 = '*'
    ){

然而它只是稍微改变了类型提示。

显示 [ExpectedValues] 但不显示实际预期值?

【问题讨论】:

    标签: php function phpstorm laravel-8 type-hinting


    【解决方案1】:

    您可以使用 PhpStorm 高级元数据。特别是:方法接受的参数功能 -- https://www.jetbrains.com/help/phpstorm/ide-advanced-metadata.html#expected-arguments.

    创建一个名为.phpstorm.meta.php 的单独的类似PHP 的文件,并将其放在项目根目录中。 PhpStorm 将在适当的位置解析和使用此文件中的数据。适用于任何现代 PHP 版本;它被 Laravel IDE Helper 和 Symfony 插件所使用。

    <?php
    
    namespace PHPSTORM_META {
        expectedArguments(\AUser::isAdmin(), 0, 'full', 'view', 'edit', 'add', 'delete');
    }
    
    


    对于 PHP 8,您可以使用自定义 #[ExpectedValues] JetBrains 制作的 PHP 属性

    这样,所有值都与方法本身一起存储,而不是单独的文件。

    <?php
    declare(strict_types=1);
    
    use JetBrains\PhpStorm\ExpectedValues;
    
    class AUser {
        /**
         * Bla-bla ...
         * 
         * By default, if leave arguments blank, you are asking if the current user is a full admin.
         * 
         * @param string $tag
         * @param string $tag_type
         * @return int
         * 
         */
        public function isAdmin(
            string $tag = '',
            #[ExpectedValues(['all', 'activities', 'grades', 'orders', 'people', 'schools', 'users', 'team_classes', 'coaches', 'team_parents', 'organization'])]
            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();
        }
    }
    
    $user = new AUser();
    $user->isAdmin('full', '');
    

    IDE 还可以提示您使用了意外的值:

    注意:这是一个弱警告严重性,所以它不是超级可见(您可以在检查设置中调整它):

    【讨论】:

    • 谢谢!!!我注意到我必须使用 JetBrains\PhpStorm\ExpectedValues;我没有这样做。谢谢!现在它是内联的,这是一个多么棒的功能。 --- 实际上,phpStorm 自动为我输入了 use ref。我做错的是在 [ExpectedValues] 语句后加了一个逗号。再次感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 2011-09-12
    • 2019-09-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    相关资源
    最近更新 更多