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

Properties in SimpleXMLElement may be null #76

Closed
wants to merge 2 commits into from

Conversation

orklah
Copy link
Contributor

@orklah orklah commented Jan 2, 2020

As discussed in phpstan/phpstan/issues/2784 this returns a BenevolentUnionType(SimpleXMLElement|null) on properties for SimpleXMLElement.

It then allows operations on BenevolentUnionType by overriding unionResults (the method implementing the logic for whether an operation is permitted). While UnionType allow an operation if it's allowed on every Type in the Union, BenevolenUnionType allow the operation if it's allowed on at least one Type in the Union.

I added a test on impossible-instanceof. Without the modification in SimpleXML, it returned

385: Instanceof between SimpleXMLElement and SimpleXMLElement will always evaluate to true.

and with the modification, it doesn't return anything.

As you said in phpstan/phpstan/issues/2784:

It's completely valid use-case that people are sure about their XML structure and know some attribute isn't going to be null.

I believe some functions in SimpleXMLElement should return BenevolentUnionType as well, such as SimpleXMLElement::attributes, who currently return SimpleXMLElement|null. Unfortunately, I tried modifying the type to (SimpleXMLElement|null) in FunctionMap but it doesn't seem to be resolved into a BenevolentUnionType. This could be for a future PR.

…mplement behaviour for calls on BenevolentUnionTypes by overriding UnionType::unionResults
@orklah
Copy link
Contributor Author

orklah commented Jan 2, 2020

I couldn't run phpcbf on my windows PHP 7.4 because of deprecations and fatal errors due to an old version of squizlabs\php_codesniffer. I will check on the CI here to fix potential issues

*/
protected function unionResults(callable $getResult): TrinaryLogic
{
return TrinaryLogic::maxMin(...array_map($getResult, $this->getTypes()));
Copy link
Contributor Author

@orklah orklah Jan 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not certain maxMin was the best method to use here, but I wasn't sure what to do with the Maybe case...

@orklah
Copy link
Contributor Author

orklah commented Jan 10, 2020

@ondrejmirtes I think this is ready to be reviewed. Please tell me if I made something wrong or if I should implement more tests or anything!

Thanks!

@ondrejmirtes
Copy link
Member

Merged as: fcfcbd5

Sorry to keep you waiting!

@orklah
Copy link
Contributor Author

orklah commented Feb 8, 2022

Wow! I lost hope on this one a looong time ago :D

Thanks Ondrej!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants