Repository navigation
Code Quality: Fix the deadCode.unreachable PHPStan errors and remove the baseline - #16224
mukeshpanchal27 wants to merge 7 commits into
Conversation
…emove the baseline. Two of the nine errors are statements that can never run, and they are removed: * In `wp_ajax_fetch_list()`, `wp_die( 0 )` follows `WP_List_Table::ajax_response()`, which always ends with `die()`. * In `wp_ajax_set_attachment_thumbnail()`, `wp_send_json_error()` follows an `if`/`else` whose branches both send a response. The other seven are kept on purpose, so they are ignored in `phpstan.neon.dist` with an explanation rather than left in a baseline: * Translatable strings after `return;` in `about.php`, `credits.php`, and `capabilities.php`, which are there to be added to the POT file. * A `break` or `exit` that ends a `switch` case after a call that never returns, in `ajax-actions.php` and `post.php`. * The `die()` after `wp_nonce_ays()` in `check_admin_referer()`, which stops a request with an invalid nonce even if a custom `wp_die()` handler returns. This empties the `deadCode.unreachable` baseline, which is deleted. See #66739. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. 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. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
There was a problem hiding this comment.
🟡 Changes recommended
Removing the wp_die( 0 ) fallback allows a plugin-provided ajax_response() implementation that returns to continue into subsequent AJAX callbacks, changing previously prevented behavior.
1 open issue
What changed in this PR
This PR addresses nine PHPStan deadCode.unreachable reports by removing two statements and documenting seven intentional exceptions outside the baseline.
Changes:
- Removes trailing termination calls from two AJAX handlers.
- Adds scoped PHPStan ignores with explanations for intentionally retained statements.
- Deletes the dedicated unreachable-code baseline.
| File | Description |
|---|---|
tests/phpstan/baselines/deadCode.unreachable.neon |
Deletes the baseline for nine unreachable-code reports. |
src/wp-admin/includes/ajax-actions.php |
Removes trailing calls from two AJAX handlers. |
phpstan.neon.dist |
Documents and ignores seven intentional unreachable statements. |
Review effort: Balanced
The `wp_list_table_class_name` filter can replace the list table with a plugin class whose `ajax_response()` returns, so the `wp_die( 0 )` after it still ends the request in that case. It is restored and ignored in `phpstan.neon.dist` with the other intentional statements. See #66739. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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. |
…l()`. With the unreachable trailing `wp_send_json_error()` removed, every path through the function now ends in `wp_send_json_error()` or `wp_send_json_success()`, so it never returns. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 1706954c-041a-47f6-8cda-8e2730cb7c13
The unreachable statements that are kept on purpose were ignored by per-file counts in `phpstan.neon.dist`. A count cannot tell which statement it covers, so fixing one kept statement while adding a genuine unreachable one in the same file would go unreported. The reasons also sat far from the code they explain. Each is now ignored with an inline `@phpstan-ignore deadCode.unreachable` comment on the statement itself, giving the reason, as core already does for other identifiers. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 1706954c-041a-47f6-8cda-8e2730cb7c13
Every path through the function ends the request. The `wp_die( 0 )` after `ajax_response()` ends it even when the `wp_list_table_class_name` filter supplies a class whose `ajax_response()` returns. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 1706954c-041a-47f6-8cda-8e2730cb7c13
Every path through the function ends the request, in `wp_die()`, `check_ajax_referer()`, `wp_send_json_error()`, or `wp_send_json_success()`. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 1706954c-041a-47f6-8cda-8e2730cb7c13
Only one of the unreachable statements in the baseline could never run, the trailing `wp_send_json_error()` in `wp_ajax_set_attachment_thumbnail()`, which would only have appended a second response after one had already been sent, and it is removed. The others are kept on purpose: * Translatable strings after a `return`, so that they are added to the POT file. * An `exit` or `break` that keeps a `switch` case from reading as falling through. * The `wp_die()` fallback for a list table supplied by the `wp_list_table_class_name` filter whose `ajax_response()` returns. * The `die()` that stops a request with an invalid nonce even if a custom `wp_die()` handler returns. Since these should never be fixed, they are ignored inline with a reason rather than left in a baseline, which would invite contributors to remove them. Unlike the counted entries used for the HTML API in r64294, an inline ignore names the exact statement, so a genuine unreachable statement added to the same file is still reported. The Ajax handlers involved are also documented as never returning. Developed in #16224. Follow-up to r29029, r63023, r64294. Props mukesh27, westonruter. See #66739. git-svn-id: https://develop.svn.wordpress.org/trunk@65773 602fd350-edb4-49c9-b593-d223f7449a82
Only one of the unreachable statements in the baseline could never run, the trailing `wp_send_json_error()` in `wp_ajax_set_attachment_thumbnail()`, which would only have appended a second response after one had already been sent, and it is removed. The others are kept on purpose: * Translatable strings after a `return`, so that they are added to the POT file. * An `exit` or `break` that keeps a `switch` case from reading as falling through. * The `wp_die()` fallback for a list table supplied by the `wp_list_table_class_name` filter whose `ajax_response()` returns. * The `die()` that stops a request with an invalid nonce even if a custom `wp_die()` handler returns. Since these should never be fixed, they are ignored inline with a reason rather than left in a baseline, which would invite contributors to remove them. Unlike the counted entries used for the HTML API in r64294, an inline ignore names the exact statement, so a genuine unreachable statement added to the same file is still reported. The Ajax handlers involved are also documented as never returning. Developed in WordPress/wordpress-develop#16224. Follow-up to r29029, r63023, r64294. Props mukesh27, westonruter. See #66739. Built from https://develop.svn.wordpress.org/trunk@65773 git-svn-id: http://core.svn.wordpress.org/trunk@64802 1a063a9b-81f0-0310-95a4-ce76da25c4cd

✅ Committed in r65773 (fa31aae).
Trac ticket: https://core.trac.wordpress.org/ticket/66739
Description
This PR resolves all 9
deadCode.unreachablePHPStan errors and deletestests/phpstan/baselines/deadCode.unreachable.neon.PHPStan reports
deadCode.unreachablefor a statement that follows code that always ends the request. One of the errors is a statement that can never run, so it is removed. The other eight are kept on purpose. They are ignored with an inline@phpstan-ignore deadCode.unreachablecomment on the statement itself, giving the reason, rather than staying in a baseline, which invites contributors to remove them.An inline ignore is used instead of a counted entry in
phpstan.neon.dist(as 273f693 did for the HTML API) because it names the exact statement. A per-file count cannot tell which statement it covers, so if a kept statement were removed and a genuine unreachable one added to the same file, the count would still match and the new error would go unreported. The reason also sits next to the code it explains. Core already uses inline ignores with reasons for other identifiers, such as insrc/wp-includes/media.phpandsrc/wp-includes/post.php.Removed (1)
wp_ajax_set_attachment_thumbnail()insrc/wp-admin/includes/ajax-actions.php: the finalwp_send_json_error(), after anif/elsewhose branches both send a response. Unlike the fallbacks kept below, it guards nothing: it could only run if a customwp_die()handler returned, and then it would append a second, error response after the success response that had already been sent.Kept and ignored inline (8)
wp-admin/about.php,wp-admin/credits.php,wp-includes/capabilities.phpreturn;wp-admin/includes/ajax-actions.phpbreakafterwp_send_json_success()inwp_ajax_image_editor()wp-admin/includes/ajax-actions.phpwp_die( 0 )afterajax_response()inwp_ajax_fetch_list()wp_list_table_class_namefilter (since 6.1) can replace the list table with a plugin class whoseajax_response()returns. This ends the request in that case.wp-admin/post.phpexitafterredirect_post()in thepostcasewp-admin/post.phpexitafterredirect_post()in theeditpostcasetrash.wp-includes/pluggable.phpdie()afterwp_nonce_ays()incheck_admin_referer()wp_die()handler returns.I confirmed the PHPCS errors by removing each
breakandexitlocally.For the three translatable-string cases, the ignore goes at the end of the first string's line, since that is the statement PHPStan reports. In
capabilities.phpa comment on the line above would come between the/* translators: */comment and the_x()call it belongs to.@return never(3)The three Ajax handlers touched here now have
@return never, since every path through each one ends the request:wp_ajax_set_attachment_thumbnail(): every path now ends inwp_send_json_error()orwp_send_json_success().wp_ajax_fetch_list(): the keptwp_die( 0 )ends the request even when a filtered list table'sajax_response()returns.wp_ajax_image_editor(): every path ends inwp_die(),check_ajax_referer(),wp_send_json_error(), orwp_send_json_success().PHPStan checks a PHPDoc
@return neverand reportsreturn.neverwhen a function can return, and it reports none for these. The other Ajax handlers in the file are left for a separate change, since annotating them is type coverage rather than dead code.Testing
composer phpstan:defaultandcomposer phpstan:strictreport no errors, with PHPStan 2.3.7.pluggable.php, is on an unchanged line and also appears ontrunk.npm run test:php -- --group ajax: 398 tests pass (1 skipped).Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5 (medium effort)
Used for: Finding and classifying the errors, checking the PHPCS and PHPStan behavior, editing the code and configuration, and drafting this PR. I reviewed the changes.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.