Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeDoesNotContainNull: on return value from dom_import_simplexml() #6151

Closed
KevinVanSonsbeek opened this issue Jul 21, 2021 · 6 comments · Fixed by #7489
Closed

TypeDoesNotContainNull: on return value from dom_import_simplexml() #6151

KevinVanSonsbeek opened this issue Jul 21, 2021 · 6 comments · Fixed by #7489
Labels
bug easy problems Issues that can be fixed without background knowledge of Psalm good first issue Help wanted internal stubs/callmap

Comments

@KevinVanSonsbeek
Copy link
Contributor

KevinVanSonsbeek commented Jul 21, 2021

When using the dom_import_simplexml() function, psalm gives a TypeDoesNotContainNull warning when performing a null check on the return value. Psalm still indicates the return type to be DOMElement|false while the actual return type is DOMElement|null.

Example: https://psalm.dev/r/42bcd39f32

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/42bcd39f32
<?php

class Foo {
 	public function test(\SimpleXMLElement $xmlElement): \SimpleXMLElement
    {
        $domElement = dom_import_simplexml($xmlElement);
        
        if (null === $domElement) {
            throw new \LogicException('Error');
        }
        
        return $domElement;
    }
}
Psalm output (using commit a2c87b8):

ERROR: TypeDoesNotContainNull - 8:13 - DOMElement|false does not contain null

ERROR: InvalidReturnStatement - 12:16 - The inferred type 'DOMElement|false' does not match the declared return type 'SimpleXMLElement' for Foo::test

ERROR: InvalidReturnType - 4:56 - The declared return type 'SimpleXMLElement' for Foo::test is incorrect, got 'DOMElement|false'

@weirdan
Copy link
Collaborator

weirdan commented Jul 21, 2021

while the actual return type is DOMElement|null

I'm not sure about that - can you devise a reproducer showing null returned in PHP 8+? For older versions it should be changed to |null, as the code returning null was there since about 18 years ago.

@KevinVanSonsbeek
Copy link
Contributor Author

KevinVanSonsbeek commented Jul 21, 2021

Not entirely sure how i can make a reproducer. But going through the source code i did notice that going from 7.4 to 8.0 the return has been changed.
From a RETURN_NULL() to a RETURN_THROWS(). Oddly the dom stub shows the return type as ?DOMElement The stub has been updated on the master branch to be just the DOMElement as the return type. And has been pushed to the php-8.0 branch.

@KevinVanSonsbeek
Copy link
Contributor Author

But from what i can see, the function can now only return a DOMElement. And it will throw a ValueError on failure. (ArgumentCountError when not enough arguments are supplied)

@orklah
Copy link
Collaborator

orklah commented Jan 25, 2022

Indeed, the function doesn't seem to have ever returned false. The return null was kinda legit before 8.0 but was only returned when wrong params were send. Psalm already reports wrong params so adding the null return type would be redundant.

So, all that is needed is to remove false from the type in the callmaps.

We actually have a doc page if you want to contribute on that: https://psalm.dev/docs/contributing/editing_callmaps/

Care for a PR? :)

@KevinVanSonsbeek
Copy link
Contributor Author

PR is open

orklah added a commit that referenced this issue Jan 27, 2022
…otcontainnull-on-return-value-from-dom_import_simplexml

Bugfix/#6151 typedoesnotcontainnull on return value from dom import simplexml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug easy problems Issues that can be fixed without background knowledge of Psalm good first issue Help wanted internal stubs/callmap
Projects
None yet
3 participants