Code Quality: Remove three redundant boolean sub-expressions - #13086
Code Quality: Remove three redundant boolean sub-expressions#13086tstokes8040 wants to merge 4 commits into
Conversation
Each of these conditions re-tests something the surrounding expression has already established. In `wp_render_typography_support()`, `! empty( $block['attrs']['fitText'] )` already requires the value to be truthy, so the following `&& $block['attrs']['fitText']` can never fail. In `_get_block_templates_files()`, the right operand of the `||` is only evaluated when `! $post_type` was false, so the leading `$post_type &&` is likewise always true. In `Walker::display_element()`, `$newlevel` is a local assigned the literal `true` and nothing else, so `isset( $newlevel ) && $newlevel` reduces to the `isset()`.
All three are simplifications with no change in behaviour. This resolves two `booleanAnd.rightAlwaysTrue` and one `booleanAnd.leftAlwaysTrue` occurrence. The baselines were regenerated with:
{{{
composer phpstan:baselines -- --identifier=booleanAnd.rightAlwaysTrue,booleanAnd.leftAlwaysTrue
}}}
Props tstokes8040.
See #65817.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
There was a problem hiding this comment.
🟢 Approval recommended
The boolean simplifications are behavior-preserving in context and the baseline updates correctly reflect the removed PHPStan findings.
Pull request overview
This PR removes three redundant boolean sub-expressions that PHPStan correctly identifies as tautologies, and updates the generated PHPStan baselines accordingly. The changes are small, localized, and intended to be behavior-preserving while improving code clarity and reducing baseline noise.
Changes:
- Simplify
wp_render_typography_support()by removing a redundant re-test offitText. - Simplify
_get_block_templates_files()by removing an always-true left operand in a short-circuited||clause. - Simplify
Walker::display_element()by removing a redundant truthiness check for a variable that is only ever set totruewhen set.
File summaries
| File | Description |
|---|---|
| tests/phpstan/baselines/booleanAnd.rightAlwaysTrue.neon | Removes two baseline entries corresponding to eliminated && tautologies. |
| tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon | Removes one baseline entry corresponding to an eliminated left-always-true && operand. |
| src/wp-includes/class-wp-walker.php | Drops redundant && $newlevel check after confirming $newlevel is only set to true in the relevant scope. |
| src/wp-includes/block-template-utils.php | Removes redundant $post_type && within a branch that only executes when $post_type is already truthy. |
| src/wp-includes/block-supports/typography.php | Removes redundant $block['attrs']['fitText'] operand already implied by ! empty( ... ) (and avoids an extra array access). |
Review details
- Files reviewed: 5/5 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Each of three conditions re-tests something the surrounding expression has already established, so PHPStan reports the redundant operand as always true: 1. In `wp_render_typography_support()`, `! empty()` already requires the `fitText` attribute to be truthy, so a following truthiness check on the same value can never fail. 2. In `_get_block_templates_files()`, the right operand of an `||` is only evaluated when `! $post_type` was false, so a leading `$post_type &&` there is always true. 3. In `Walker::display_element()`, `$newlevel` is a local variable that is only ever assigned the literal `true`, so the truthiness test adds nothing to the `isset()`. All three are simplifications with no change in behavior. This resolves two `booleanAnd.rightAlwaysTrue` occurrences and one `booleanAnd.leftAlwaysTrue` occurrence, and the corresponding PHPStan baselines are regenerated. Developed in #13086. Follow-up to r55687, r61246, r63023. Props tstokes8040. See #65817. git-svn-id: https://develop.svn.wordpress.org/trunk@63542 602fd350-edb4-49c9-b593-d223f7449a82
Each of three conditions re-tests something the surrounding expression has already established, so PHPStan reports the redundant operand as always true: 1. In `wp_render_typography_support()`, `! empty()` already requires the `fitText` attribute to be truthy, so a following truthiness check on the same value can never fail. 2. In `_get_block_templates_files()`, the right operand of an `||` is only evaluated when `! $post_type` was false, so a leading `$post_type &&` there is always true. 3. In `Walker::display_element()`, `$newlevel` is a local variable that is only ever assigned the literal `true`, so the truthiness test adds nothing to the `isset()`. All three are simplifications with no change in behavior. This resolves two `booleanAnd.rightAlwaysTrue` occurrences and one `booleanAnd.leftAlwaysTrue` occurrence, and the corresponding PHPStan baselines are regenerated. Developed in WordPress/wordpress-develop#13086. Follow-up to r55687, r61246, r63023. Props tstokes8040. See #65817. Built from https://develop.svn.wordpress.org/trunk@63542 git-svn-id: http://core.svn.wordpress.org/trunk@62718 1a063a9b-81f0-0310-95a4-ce76da25c4cd
✅ Committed in r63542 (ea146ac).
Each of these three conditions re-tests something the surrounding expression has already established, so PHPStan reports the redundant operand as always true.
wp_render_typography_support(),block-supports/typography.php:307—! empty( $x )already requires the value to be truthy, so the following&& $xcan never fail._get_block_templates_files(),block-template-utils.php:463— the right operand of the||is only evaluated when! $post_typewas false, so the leading$post_type &&is always true there.Walker::display_element(),class-wp-walker.php:167—$newlevelis a local variable assigned the literaltruetwelve lines above and never assigned anything else, so the truthiness test adds nothing to theisset().All three are simplifications with no change in behaviour.
booleanAnd.rightAlwaysTruegoes from 16 to 14 andbooleanAnd.leftAlwaysTruefrom 5 to 4. The baselines are generated, not hand-edited; neither reaches zero, so no baseline is deleted andphpstan.neon.distis unchanged.One occurrence deliberately left baselined
functions.php:6784is the same shape —0 => ( isset( $zone[0] ) && $zone[0] )— and PHPStan is right that the second operand is always true, because the enclosing loopcontinues unless$zone[0]matched an entry in$continents. It is left alone here because it is one of three parallel lines building an$existsmap, and dropping the operand from only the first breaks that symmetry to remove a tautology. That seemed a change worth discussing separately rather than folding into this one.Testing instructions
npm run typecheck:phpontrunkreports[OK] No errors, because the occurrences are baselined.npm run typecheck:phpreports[OK] No errorsacross 1289 files with both baselines regenerated.npm run test:php— 30871 tests, 4558975 assertions, 86 warnings, 44 skipped, and one failure:Tests_Script_Modules_WpScriptModules::test_default_script_module_files_exist, looking forsrc/wp-includes/js/dist/script-modules/a11y/index.js. That path is gitignored build output that is unbuilt in my checkout, and the test fails identically on unmodifiedtrunk. The counts are identical to a run without these changes.composer lintreports no errors for the three modified files.class-wp-walker.phpcarries one pre-existing filename-convention warning that is also present ontrunk.Unlike the other PRs on this ticket, this one changes executable code rather than docblocks.
Walker::display_element()in particular runs for every nav menu, category list and comment tree, which is why the full suite result above is worth checking rather than taking on the argument alone.Related
These are independent of one another and can land in any order, but several regenerate overlapping baseline files, so whichever lands after the first will need
composer phpstan:baselinesre-run against the updated trunk:booleanAnd.rightAlwaysTrue.neon, shared with this PRbooleanAnd.leftAlwaysTrue.neon, shared with this PRdeadCode.unreachable.neonandif.alwaysTrue.neon, shared with 13074Trac ticket: https://core.trac.wordpress.org/ticket/65817
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: grouping the remaining boolean PHPStan baselines by root cause, identifying this cluster, measuring the before/after error diff across the whole codebase, regenerating the baselines, and drafting this description. The change itself and the verification runs were reviewed and confirmed by me in a local development environment.
Count of removed errors: 2