PhpStorm为什么会报告“参数类型不匹配”错误?

4

我正在使用 PhpStorm 10.0.2 开发一个基于 PHP 7 的项目。

每当我为一个具有标量类型提示(stringint等)的函数参数声明 PHPDoc @param 时,就会收到以下警告:

参数类型不匹配

以下是一些 PhpStorm 抱怨的示例代码:

class HostConfig
{
    /**
     * @var string
     */
    private $hostname;
    /**
     * @var string
     */
    private $domainname;

    /**
     * Creates a new instance of hte HostConfig model.
     *
     * @param string $hostname   A host name (e.g. "dev", "int", "feature-xy")
     * @param string $domainname A domain name (e.g. "example.com")
     *
     * @throws \InvalidArgumentException If the given $hostname is empty
     * @throws \InvalidArgumentException If the given $domainname is empty
     */
    public function __construct(string $hostname, string $domainname)
    {
        if (strlen(trim($hostname)) === 0) {
            throw new \InvalidArgumentException("The host name cannot be empty");
        }

        if (strlen(trim($domainname)) === 0) {
            throw new \InvalidArgumentException("The domain name cannot be empty");
        }

        $this->hostname = $hostname;
        $this->domainname = $domainname;
    }
}

以下是 PhpStorm 显示构造函数 PHPDoc 中两个强类型字符串参数的“参数类型不匹配”提示的屏幕截图:

PhpStorm 显示带有类型提示的字符串函数参数的“参数类型不匹配”提示的屏幕截图

请问是否有人知道我是否做错了什么,还是这只是 PhpStorm 的一个 bug?


我在过去几年中每天都使用PHPStorm,不幸的是,在版本之间存在许多令人烦恼的错误。 - Marty
@Marty 为什么你还在使用它?为什么你没有换掉它? - BeetleJuice
1
@BeetleJuice 到哪里?没有其他工具有相同的 PHPDoc 支持。 - Marty
1个回答

5

您使用的PHPStorm版本是在PHP 7发布后仅仅7天发布的,因此它对该语言的新特性(包括标量类型提示)的支持并不完美。您正在经历一种IDE错误。您的问题可能是这个问题,它已经在PHPStorm 10.0.3中得到修复。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接