Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Attempt to fix #302
What a wonderful bug we found.
I'll start from afar and take the function
Assert::greaterThan($value, $limit)
as an example.The asserted condition is literally described in PHP as follows:
And when we need to describe opposite condition in which our assertion must throw an exception there are at least two ways:
!($value > $limit)
(literal)$value <= $limit
(simple)In the first case, the condition is described literally as the logical negation of the assertion.
In the second case, the same conditions is described, but in a more readable form using only one operand.
Its a common practice to avoid negative conditions for better readability.
So it is not surprising that many methods in this library use the second one.
Unfortunately this doesn't work with
NAN
.In normal cases both form give the same result:
But it doesn't work that way for
NAN
.Any comparison with
NAN
will returnfalse
(except!=
and!==
).And our forms of negative condition now return different result:
In addition to functions mentioned in issue I found 9 more assertions that lead to unexpected results when passing
NAN
.I added additional test cases for those method in this PR.
Solutions
Straightforward one.
Just change the form of the negative condition from literal to simple.
This is the solution I described in this PR.
Another solution is to left simple condition but add additional checks for NAN for each of arguments.
Method to check for
NAN
:And the condition for Assert::greaterThan function will be:
This is a more readable but verbose method.
I like the second solution more. To reduce verbosity we can change check-method signature to
isNaN(...$values)
oratLeastOneIsNaN(...$values)