noverify icon indicating copy to clipboard operation
noverify copied to clipboard

information about instance of variable not used

Open Pomyk opened this issue 7 years ago • 4 comments

Code Example

<?php
declare(strict_types=1);

class Element {
    public function get(): int {
        return 0;
    }
}

class Test {
    private $arr = [];

    public function test($key) {
        if (isset($this->arr[$key]) && $this->arr[$key] instanceof Element) {
            return $this->arr[$key]->get();
        }
    }
}

Actual Behavior

ERROR Call to undefined method {}->get()

Expected Behavior

All ok

Environment

  • NoVerify Version: dev-master

Pomyk avatar Mar 06 '19 11:03 Pomyk

Yes, linter does not support <expression> instanceof Class, it only works in one specific case which is $var instanceof Class. I am not sure I want to support arbitrary expressions here (yet).

YuriyNasretdinov avatar Mar 06 '19 12:03 YuriyNasretdinov

Interestingly enough, PHPStorm does not support this either :). But I think that it is possible to implement in linter, I will try to do it.

YuriyNasretdinov avatar Mar 07 '19 17:03 YuriyNasretdinov

So this should work now :). It is not the best implementation possible, e.g. in this example I do not analyze that you do not change value of $key between checking instanceof and calling the method. I just match the whole expression and if it is identical to what was previously checked, then it will work.

YuriyNasretdinov avatar Mar 07 '19 20:03 YuriyNasretdinov

Wow, thanks :)

Pomyk avatar Mar 08 '19 13:03 Pomyk