Make the ordering assertions reject operands PHP can only compare by coercion - #366
Open
lenamonj wants to merge 1 commit into
Open
Make the ordering assertions reject operands PHP can only compare by coercion#366lenamonj wants to merge 1 commit into
lenamonj wants to merge 1 commit into
Conversation
…coercion greaterThan, greaterThanEq, lessThan, lessThanEq and range handed their operands straight to the relational operators, so lessThan(new stdClass(), 10) and lessThan(null, 10) passed: an object compares as 1, and null or bool against a number is compared as bool. Ask first whether the pair can be ordered (two numerics, two strings, two bools, two arrays or two objects) and report anything else as the failed assertion it is. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Assert::lessThan(new stdClass(), 10),Assert::lessThan(null, 10),Assert::lessThan(false, 10)andAssert::range(new stdClass(), 1, 10)all pass.greaterThan([1, 2], 10)andgreaterThan('abc', 10)pass too.Cause: the five ordering assertions hand their operands straight to
<,<=,>,>=, which answer for any pair by coercing: an object compares as1, an array outranks any scalar, and null or bool against a number is compared as bool.Change: a
comparable()helper that accepts two numerics, two strings, two bools, two arrays or two objects; anything else is reported through the method's existing message path, so messages and placeholders are unchanged. Pairs the suite already exercised keep their verdicts ('5'vs10,DateTimeImmutablevsDateTimeImmutable,[1]vs[1, 2],truevsfalseare pinned in the provider). NAN is deliberately left alone per #302.This does narrow what the five methods accept, which is the point of an assertion, but if you would rather draw the line differently (for example keep bool against int) I am happy to adjust.
Verified:
composer run test4997 tests green on PHP 8.5 (the 8 new rejection rows fail before the fix),cs-checkandstatic-analysisclean.Found by an automated code-review loop I run; the fix and this description were prepared with Claude and verified by hand.