Skip to content

Conversation

@mpesari
Copy link
Contributor

@mpesari mpesari commented Jan 15, 2026

This makes it possible to ignore disallowedLooseComparison issues for certain builtin PHP classes with overloaded comparison operators:

Currently the error message doesn't include types:

Loose comparison via "==" is not allowed.

This adds operands types to make the errors ignorable:

Loose comparison via "==" between DateTime and DateTime is not allowed.

@ondrejmirtes
Copy link
Member

Hi, we can't do this change in a minor/patch version because it'd break people's baselines.

This can be done by changing the error message only when the bleedingEdge toggle is on. You can inject the parameter into the rule with %featureToggles.bleedingEdge%.

@mpesari mpesari force-pushed the 2.0.x branch 2 times, most recently from 8dc2d6c to eb67092 Compare February 10, 2026 16:03
@mpesari
Copy link
Contributor Author

mpesari commented Feb 10, 2026

Thanks for the suggestion, I added an includeOperandTypesInErrorMessage toggle which defaults to bleedingEdge.

I also added a tip about the use case to README.

README.md Outdated
| `switchConditionsMatchingType` | Types in `switch` condition and `case` value must match. PHP compares them loosely by default and that can lead to unexpected results. |
| `dynamicCallOnStaticMethod` | Check that statically declared methods are called statically. |
| `disallowedEmpty` | Disallow `empty()` - it's a very loose comparison (see [manual](https://php.net/empty)), it's recommended to use more strict one. |
| `disallowedLooseComparison` | Disallow loose comparison via `==` and `!=`. You can use `includeOperandTypesInErrorMessage` to ignore some cases, for example `DateTime` object comparison. |
Copy link
Member

Choose a reason for hiding this comment

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

Please remove the mention of includeOperandTypesInErrorMessage. It's not user-accessible and it's not meant to be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, right. Removed.

@ondrejmirtes ondrejmirtes merged commit b94a0b1 into phpstan:2.0.x Feb 11, 2026
31 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

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.

disallowedLooseComparison - allow only DateTime/DateTimeImmutable

2 participants