Skip to content

Implement Type->looseCompare(type)#2216

Draft
staabm wants to merge 37 commits into
phpstan:1.10.xfrom
staabm:loose-compare-new
Draft

Implement Type->looseCompare(type)#2216
staabm wants to merge 37 commits into
phpstan:1.10.xfrom
staabm:loose-compare-new

Conversation

@staabm

@staabm staabm commented Feb 2, 2023

Copy link
Copy Markdown
Contributor

@staabm

staabm commented Feb 2, 2023

Copy link
Copy Markdown
Contributor Author

for now I have implemented the looseCompare methods for all types.
now I need to have a look at the failling tests and afterwards type narrowing

@staabm
staabm force-pushed the loose-compare-new branch 9 times, most recently from 1337c88 to 8db02be Compare February 2, 2023 22:44
@ondrejmirtes

Copy link
Copy Markdown
Member

Type narrowing is a separate concern, I wouldn't do that here.

@ondrejmirtes ondrejmirtes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't see the table of == comparison described anywhere in this PR https://www.php.net/manual/en/types.comparisons.php.

All types need to take into account being compared to any other type. For example this implementation in NullType is too naive:

return $type->isNull()->toBooleanType();

Because all of the following examples are true:

null == []
null == false
null == 0
null == ""

Comment thread src/Reflection/InitializerExprTypeResolver.php Outdated
Comment thread src/Type/Constant/ConstantStringType.php Outdated
@ondrejmirtes

Copy link
Copy Markdown
Member

Also an idea for a preliminary or a future PR - NanType. See the last comment here: https://www.php.net/manual/en/function.is-nan.php

@staabm
staabm force-pushed the loose-compare-new branch 12 times, most recently from 0cff8e4 to 16fb924 Compare February 4, 2023 13:17
@staabm

staabm commented Feb 4, 2023

Copy link
Copy Markdown
Contributor Author

I have implemented a few basic types and added tests for them in loose-compare.php. please have a look whether this is done how you imagined it.

btw: this PR will get really big if we implement all combinations for all types here.
do you think we could split this, e.g. doing basic and simple object type in one PR, and all more specialized types in separate PRs?

thank you

@staabm
staabm force-pushed the loose-compare-new branch 2 times, most recently from 3fe288d to 9aed5d1 Compare February 4, 2023 14:41

@ondrejmirtes ondrejmirtes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like StringType::looseCompare but otherwise it's still gonna be a lot of work 😅 And we're not even diving into type narrowing now. I think it's obvious why I was avoiding this topic until now 😂

Comment thread src/Type/Traits/ConstantScalarTypeTrait.php Outdated
Comment thread src/Type/IntegerType.php Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

implement loose comparison semantics

3 participants