【问题标题】:Why does PhpStorm report an "Argument type does not match" error?为什么 PhpStorm 会报“参数类型不匹配”错误?
【发布时间】:2016-11-23 05:14:14
【问题描述】:

我正在使用 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,不幸的是,它在不同版本之间充斥着诸如此类的恼人错误。
  • @Marty 你为什么一直使用它?怎么还没换?
  • @BeetleJuice 做什么?没有其他任何东西具有相同的 PHPDoc 支持。

标签: php phpstorm phpdoc php-7 type-hinting


【解决方案1】:

您的 PHPStorm 版本仅在 PHP 7 后 7 天发布,因此它对该语言的新功能(包括标量类型提示)的支持不是很好。您遇到了 IDE 错误。您的问题是probably this one,已通过PHPStorm 10.0.3 修复

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-04
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多