Skip to content

Fix: Don't rewrite non-boolean expectations in the boolean-simplification rules - #3

Merged
MrPunyapal merged 2 commits into
pestphp:5.xfrom
webard:fix/tobetruenotfalse-non-boolean-guard
Jul 29, 2026
Merged

Fix: Don't rewrite non-boolean expectations in the boolean-simplification rules#3
MrPunyapal merged 2 commits into
pestphp:5.xfrom
webard:fix/tobetruenotfalse-non-boolean-guard

Conversation

@webard

@webard webard commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Two boolean-simplification rules can silently change what a test asserts when the expected value isn't a real boolean:

  • ToBeTrueNotFalseRector rewrites ->not->toBeFalse() -> ->toBeTrue()
  • SimplifyExpectNotRector rewrites expect(!$x)->toBeTrue()-> expect($x)->toBeFalse()

Both rewrites are only equivalent when the subject is a pure boolean. For anything else - most commonly a T|false union like mb_strstr() / openssl_pkey_get_public() (string|false), or a plain int / string / mixed - they turn a loose check into a strict === true / === false and break passing tests.

Example for ToBeTrueNotFalseRector

// mb_strstr() returns string|false
expect(mb_strstr($haystack, 'needle'))->not->toBeFalse(); // "found it" (!== false)

becomes

expect(mb_strstr($haystack, 'needle'))->toBeTrue(); // "=== true" - now fails on a string

Example for SimplifyExpectNotRector

expect(!$count)->toBeTrue();   // $count is an int; asserts "$count is falsy"

becomes

expect($count)->toBeFalse();   // asserts "$count === false" - fails for $count = 0

Both rules now flip the matcher only when the subject is provably a pure boolean (isBoolean()->yes()). Otherwise they leave the expectation untouched.

BTW: Thanks for releasing PestPHP 5! 🎉

@MrPunyapal

Copy link
Copy Markdown
Collaborator

@webard once CI passes we are good to go.

@MrPunyapal
MrPunyapal merged commit fc5dfcd into pestphp:5.x Jul 29, 2026
10 checks passed
@MrPunyapal

Copy link
Copy Markdown
Collaborator

thanks

@webard

webard commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

@MrPunyapal done!

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.

2 participants