PhpStorm 在我的类型化属性上推断 `mixed` 类型。

huangapple go评论54阅读模式
英文:

PhpStorm inferring `mixed` type on my typed properties

问题

以下是您要翻译的内容:

"I've been working recently with classes that initialize themselves from an array of data, and have typed properties which are set with values retrieved from the array.

The properties are typed, which means PHP will throw a TypeError if anything besides the specified type is passed in.

However, PhpStorm appears to be propogating the mixed type from the array access to the class property.

I'm struggling to explain it in words, so here's an example script:

<?php

class TestClass
{
    public readonly string $myString;
    public readonly int $myInt;
    public readonly array $myArray;

    public function __construct(array $src)
    {
        $this->myString = $src['stringVal'];
        $this->myInt = $src['intVal'];
        $this->myArray = $src['array'];
    }
}

$arr = [
    'stringVal' => 123,
    'intVal' => "I am a string",
    'array' => null,
];

$x = new TestClass($arr); // throws TypeError from within the constructor

$y = new TestClass($x->myInt); // PhpStorm doesn't see anything wrong with this???

You can see that there are types on all 3 of the properties. You can look at the script and see that it will fail. The script as written throws a TypeError exactly as you'd expect when it tries to set $this->myString.

However, if I hover over any of the class properties, PhpStorm is adding the mixed type to the property:

PhpStorm 在我的类型化属性上推断 `mixed` 类型。

Since PhpStorm is treating all of the properties as "x or mixed", it doesn't see anything wrong with the obviously incorrect last line of the script, where we're trying to pass an int to the constructor, which requires an array:

PhpStorm 在我的类型化属性上推断 `mixed` 类型。

How can I get PhpStorm to stop inferring mixed on these properties?

Update: it's not only mixed coming from an array, it seems that PhpStorm is just assuming that whatever happens in the constructor is valid, regardless of the types on the properties(?)

Another example:

class TestClass2
{
    public readonly int $myInt;

    public function __construct()
    {
        $this->myInt = "hello world"
    }
}"


<details>
<summary>英文:</summary>

I&#39;ve been working recently with classes that initialize themselves from an array of data, and have typed properties which are set with values retrieved from the array.

The properties are typed, which means PHP will throw a TypeError if anything besides the specified type is passed in.

However, PhpStorm appears to be propogating the `mixed` type from the array access to the class property.

I&#39;m struggling to explain it in words, so here&#39;s an example script:

```php
&lt;?php

class TestClass
{
    public readonly string $myString;
    public readonly int $myInt;
    public readonly array $myArray;

    public function __construct(array $src)
    {
        $this-&gt;myString = $src[&#39;stringVal&#39;];
        $this-&gt;myInt = $src[&#39;intVal&#39;];
        $this-&gt;myArray = $src[&#39;array&#39;];
    }
}

$arr = [
    &#39;stringVal&#39; =&gt; 123,
    &#39;intVal&#39; =&gt; &quot;I am a string&quot;,
    &#39;array&#39; =&gt; null,
];

$x = new TestClass($arr); // throws TypeError from within the constructor

$y = new TestClass($x-&gt;myInt); // PhpStorm doesn&#39;t see anything wrong with this???

You can see that there are types on all 3 of the properties. You can look at the script and see that it will fail. The script as written throws a TypeError exactly as you'd expect when it tries to set $this-&gt;myString.

However, if I hover over any of the class properties, PhpStorm is adding the mixed type to the property:

PhpStorm 在我的类型化属性上推断 `mixed` 类型。

Since PhpStorm is treating all of the properties as "x or mixed", it doesn't see anything wrong with the obviously incorrect last line of the script, where we're trying to pass an int to the constructor, which requires an array:

PhpStorm 在我的类型化属性上推断 `mixed` 类型。

How can I get PhpStorm to stop inferring mixed on these properties?

Update: it's not only mixed coming from an array, it seems that PhpStorm is just assuming that whatever happens in the constructor is valid, regardless of the types on the properties(?)

Another example:

class TestClass2
{
    public readonly int $myInt;

    public function __construct()
    {
        $this-&gt;myInt = &quot;hello world&quot;;
    }
}

The constructor throws a TypeError, again just as you'd expect. But PhpStorm seems content to assume that the property will accept a string:

PhpStorm 在我的类型化属性上推断 `mixed` 类型。

答案1

得分: 2

我向JetBrains提交了一个错误报告,他们给了我以下的解决方法:

作为解决方法,您可以要么标记文件为 declare(strict_types=1); 或启用 PHP | 类型兼容性 | 严格类型检查规则违规:对所有文件启用

在我的情况下,代码检查设置是最合适的,并为我解决了问题。

英文:

I filed a bug report to JetBrains, and they gave me the following workaround:

> As a workaround, you can either mark the file with declare(strict_types=1); or enable the PHP | Type compatibility | Strict type checking rules violation: Enable for all files.

In my case, the code inspection setting was most appropriate and solved the issue for me.

huangapple
  • 本文由 发表于 2023年4月10日 21:21:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定