Non existing methods don't get underlined for typeless variables

Answered

After PStorm new version reinstall (2020.1.2), values which I expect to be underlined as warnings/errors don't get underlined. 

 

Example:

private array $values;

foreach ($this->values as $value) {
$value->hasWildCard();
}

 

Since `$value` isn't type hinted, PStorm has no way of knowing what is this array of. In my case it is array of strings, so no method can be called on it, and yet it isn't underlined as undefined method.

If I explicitly type hint `$value` as string, then it underlines it.

How can I include this option to be active without type hinting?

4 comments
Comment actions Permalink

Hi there,

Please provide complete example (a whole file .. or set of files) that can show the issue. Something that I (or another person) can download or just copy paste and check it locally.

Do that in a brand new project (so it's not connected to your project-specific code in any way).

0
Comment actions Permalink

The thing I wrote is literally all there is to it. It doesn't allow me to attach files here, so here it is the whole file that can reproduce the issue:

 

<?php

class Testing
{
private array $values;

public function test()
{
foreach ($this->values as $value) {
$value->hasWildCard();
}
}
}

Thanks

0
Comment actions Permalink

Thanks for the example.

As far as I remember it's done on purpose (original ticket: WI-1831) for the "mixed" type otherwise way too many warnings when code is not properly type hinted (which happens a lot even these days as not everybody use modern frameworks & tools/work with old code etc).

As per WI-26667 an option had been added for Undefined Method inspection that allows to issue warning even for manually type hinted variables/properties of mixed, object, undefined or stdClass types.

Just enable that option (disabled by default)

1
Comment actions Permalink

That solves it, thanks!

0

Please sign in to leave a comment.