Skip to content

Commit fa31aae

Browse files
committed
Code Quality: Resolve the deadCode.unreachable PHPStan errors.
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
1 parent 916de6f commit fa31aae

7 files changed

Lines changed: 14 additions & 60 deletions

File tree

‎src/wp-admin/about.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@
294294
// These are strings we may use to describe maintenance/security releases, where we aim for no new strings.
295295
return;
296296

297-
__( 'Maintenance Release' );
297+
__( 'Maintenance Release' ); // @phpstan-ignore deadCode.unreachable (Placed after the return so that the strings are added to the POT file without running.)
298298
__( 'Maintenance Releases' );
299299

300300
__( 'Security Release' );

‎src/wp-admin/credits.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
return;
123123

124124
// These are strings returned by the API that we want to be translatable.
125-
__( 'Project Leaders' );
125+
__( 'Project Leaders' ); // @phpstan-ignore deadCode.unreachable (Placed after the return so that the strings are added to the POT file without running.)
126126
/* translators: %s: The current WordPress version number. */
127127
__( 'Core Contributors to WordPress %s' );
128128
__( 'Noteworthy Contributors' );

‎src/wp-admin/includes/ajax-actions.php‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ function wp_ajax_nopriv_heartbeat() {
7979
* Handles fetching a list table via AJAX.
8080
*
8181
* @since 3.1.0
82+
*
83+
* @return never
8284
*/
8385
function wp_ajax_fetch_list() {
8486
$list_class = $_GET['list_args']['class'];
@@ -95,7 +97,7 @@ function wp_ajax_fetch_list() {
9597

9698
$wp_list_table->ajax_response();
9799

98-
wp_die( 0 );
100+
wp_die( 0 ); // @phpstan-ignore deadCode.unreachable (The wp_list_table_class_name filter can supply a class whose ajax_response() returns.)
99101
}
100102

101103
/**
@@ -2684,6 +2686,8 @@ function wp_ajax_upload_attachment() {
26842686
* Handles image editing via AJAX.
26852687
*
26862688
* @since 3.1.0
2689+
*
2690+
* @return never
26872691
*/
26882692
function wp_ajax_image_editor() {
26892693
$attachment_id = (int) $_POST['postid'];
@@ -2705,7 +2709,7 @@ function wp_ajax_image_editor() {
27052709
}
27062710

27072711
wp_send_json_success( $message );
2708-
break;
2712+
break; // @phpstan-ignore deadCode.unreachable (Ends the case so that it does not read as falling through, which PHPCS reports.)
27092713
case 'scale':
27102714
$message = wp_save_image( $attachment_id );
27112715
break;
@@ -2804,6 +2808,8 @@ function wp_ajax_get_post_thumbnail_html() {
28042808
* @since 4.0.0
28052809
*
28062810
* @see set_post_thumbnail()
2811+
*
2812+
* @return never
28072813
*/
28082814
function wp_ajax_set_attachment_thumbnail() {
28092815
if ( empty( $_POST['urls'] ) || ! is_array( $_POST['urls'] ) ) {
@@ -2849,8 +2855,6 @@ function wp_ajax_set_attachment_thumbnail() {
28492855
} else {
28502856
wp_send_json_success();
28512857
}
2852-
2853-
wp_send_json_error();
28542858
}
28552859

28562860
/**

‎src/wp-admin/post.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
check_admin_referer( 'add-' . $post_type );
126126
$post_id = 'postajaxpost' === $action ? edit_post() : write_post();
127127
redirect_post( $post_id );
128-
exit;
128+
exit; // @phpstan-ignore deadCode.unreachable (Ends the case so that it does not read as falling through, which PHPCS reports.)
129129

130130
case 'edit':
131131
$editing = true;
@@ -245,7 +245,7 @@
245245

246246
redirect_post( $post_id ); // Send user on their way while we keep working.
247247

248-
exit;
248+
exit; // @phpstan-ignore deadCode.unreachable (Ends the case so that it does not read as falling through into the trash case.)
249249

250250
case 'trash':
251251
check_admin_referer( 'trash-post_' . $post_id );

‎src/wp-includes/capabilities.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ function wp_maybe_grant_site_health_caps( $allcaps, $caps, $args, $user ) {
13651365

13661366
// Dummy gettext calls to get strings in the catalog.
13671367
/* translators: User role for administrators. */
1368-
_x( 'Administrator', 'User role' );
1368+
_x( 'Administrator', 'User role' ); // @phpstan-ignore deadCode.unreachable (Placed after the return so that the strings are added to the POT file without running.)
13691369
/* translators: User role for editors. */
13701370
_x( 'Editor', 'User role' );
13711371
/* translators: User role for authors. */

‎src/wp-includes/pluggable.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,7 @@ function check_admin_referer( $action = -1, $query_arg = '_wpnonce' ) {
13991399

14001400
if ( ! $result && ! ( -1 === $action && str_starts_with( $referer, $adminurl ) ) ) {
14011401
wp_nonce_ays( $action );
1402-
die();
1402+
die(); // @phpstan-ignore deadCode.unreachable (Stops a request with an invalid nonce even if a custom wp_die() handler returns.)
14031403
}
14041404

14051405
return $result;

‎tests/phpstan/baselines/deadCode.unreachable.neon‎

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)