phptools-docs icon indicating copy to clipboard operation
phptools-docs copied to clipboard

Missing linting for possible null values

Open JokaJukka opened this issue 1 year ago • 1 comments

Hey there, let's say I have the following code:

<?php

class Test {
    public function hello(): void {
        echo "Hello world";
    }
}

function get_hello_class(bool $null): ?Test {
    if ($null) {
        return null;
    } else {
        return new Test;
    }
}

get_hello_class(true)->hello();
get_hello_class(false)->hello();

The first callback of get_hello_class will return null, and thus the script will crash - Call to a member function hello() on null. However this is not linted by the plugin at all, and I believe that it is a missing feature.

JokaJukka avatar Oct 06 '23 13:10 JokaJukka

Thank you for the suggestion!

You're right, but I'm afraid this is out of the scope of our static analysis for now.

The editor respects the function declaration and the ?Test return type. Possibly, in the future, we will do deeper code analysis, preferably taking advantage of @return documentary comment with the conditional type (https://phpstan.org/writing-php-code/phpdoc-types#conditional-return-types), and "guess" it actually returns "null".

So I can't guarantee this will get implemented any time soon.

jakubmisek avatar Oct 06 '23 14:10 jakubmisek