Skip to content

Code Quality: Fix the deadCode.unreachable PHPStan errors and remove the baseline - #16224

Closed
mukeshpanchal27 wants to merge 7 commits into
WordPress:trunkfrom
mukeshpanchal27:fix/deadcode-unreachable-baseline
Closed

mukeshpanchal27 wants to merge 7 commits into
WordPress:trunkfrom
mukeshpanchal27:fix/deadcode-unreachable-baseline

Conversation

@mukeshpanchal27

@mukeshpanchal27 mukeshpanchal27 commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

✅ Committed in r65773 (fa31aae).


Trac ticket: https://core.trac.wordpress.org/ticket/66739

Description

This PR resolves all 9 deadCode.unreachable PHPStan errors and deletes tests/phpstan/baselines/deadCode.unreachable.neon.

PHPStan reports deadCode.unreachable for 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.unreachable comment 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 in src/wp-includes/media.php and src/wp-includes/post.php.

Removed (1)

  • wp_ajax_set_attachment_thumbnail() in src/wp-admin/includes/ajax-actions.php: the final wp_send_json_error(), after an if/else whose branches both send a response. Unlike the fallbacks kept below, it guards nothing: it could only run if a custom wp_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)

File Statement Why it stays
wp-admin/about.php, wp-admin/credits.php, wp-includes/capabilities.php Translatable strings after return; They exist so the strings are added to the POT file.
wp-admin/includes/ajax-actions.php break after wp_send_json_success() in wp_ajax_image_editor() Without it, PHPCS reports "There must be a comment when fall-through is intentional".
wp-admin/includes/ajax-actions.php wp_die( 0 ) after ajax_response() in wp_ajax_fetch_list() The wp_list_table_class_name filter (since 6.1) can replace the list table with a plugin class whose ajax_response() returns. This ends the request in that case.
wp-admin/post.php exit after redirect_post() in the post case Same PHPCS error without it.
wp-admin/post.php exit after redirect_post() in the editpost case PHPCS does not require it, but without it the case would appear to fall through into trash.
wp-includes/pluggable.php die() after wp_nonce_ays() in check_admin_referer() It stops a request with an invalid nonce even if a custom wp_die() handler returns.

I confirmed the PHPCS errors by removing each break and exit locally.

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.php a 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 in wp_send_json_error() or wp_send_json_success().
  • wp_ajax_fetch_list(): the kept wp_die( 0 ) ends the request even when a filtered list table's ajax_response() returns.
  • wp_ajax_image_editor(): every path ends in wp_die(), check_ajax_referer(), wp_send_json_error(), or wp_send_json_success().

PHPStan checks a PHPDoc @return never and reports return.never when 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:default and composer phpstan:strict report no errors, with PHPStan 2.3.7.
  • PHPCS reports no errors in the changed files. The one warning, in pluggable.php, is on an unchanged line and also appears on trunk.
  • 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.

…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>
@mukeshpanchal27 mukeshpanchal27 self-assigned this Nov 25, 2026
@mukeshpanchal27
mukeshpanchal27 marked this pull request as ready for review November 25, 2026 05:22
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

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:

Props mukesh27, westonruter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread src/wp-admin/includes/ajax-actions.php Resolved
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>
@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the unreachable-code baseline cleanup.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

westonruter and others added 5 commits November 24, 2026 23:21
…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
wporg-sync pushed a commit that referenced this pull request Nov 25, 2026
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
wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Nov 25, 2026
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
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.

3 participants