diff --git a/phpstan.neon.dist b/phpstan.neon.dist
index cbaa19f3e8db1..acc8de94499fc 100644
--- a/phpstan.neon.dist
+++ b/phpstan.neon.dist
@@ -28,7 +28,6 @@ includes:
- tests/phpstan/baselines/argument.unresolvableType.neon
- tests/phpstan/baselines/assign.propertyType.neon
- tests/phpstan/baselines/binaryOp.invalid.neon
- - tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon
- tests/phpstan/baselines/booleanNot.alwaysTrue.neon
- tests/phpstan/baselines/class.notFound.neon
- tests/phpstan/baselines/deadCode.unreachable.neon
@@ -77,6 +76,16 @@ parameters:
level: 5
reportUnmatchedIgnoredErrors: true
+ # Do not assume that a function or method returns the same value each time it is called with the same
+ # arguments, unless it is marked `@phpstan-pure`. By default, PHPStan remembers the return value of any call
+ # not marked `@phpstan-impure`, so that checking it again, even after global state has changed (for example
+ # by `the_post()` or a filter), is reported as always true or always false. When that assumption is wrong,
+ # the natural way to silence the error is to delete a check that is in fact needed. With remembering
+ # disabled, the failure mode is conservative instead: a value read twice is not assumed to be the same, so
+ # the fix is to read it once into a variable. Few functions in core can be `@phpstan-pure`, since most read
+ # global state or apply filters.
+ # https://phpstan.org/blog/remembering-and-forgetting-returned-values
+ rememberPossiblyImpureFunctionValues: false
# The following ignored errors are not intended to be fixed, as distinct from the baselines
# included above.
@@ -175,20 +184,15 @@ parameters:
identifier: greaterOrEqual.alwaysFalse
count: 1
path: src/wp-includes/html-api/class-wp-html-processor.php
- -
- message: '#^Left side of && is always true\.$#'
- identifier: booleanAnd.leftAlwaysTrue
- count: 1
- path: src/wp-includes/html-api/class-wp-html-processor.php
-
message: '#^Unreachable statement \- code above always terminates\.$#'
identifier: deadCode.unreachable
- count: 15
+ count: 14
path: src/wp-includes/html-api/class-wp-html-processor.php
-
message: '#^If condition is always true\.$#'
identifier: if.alwaysTrue
- count: 2
+ count: 1
path: src/wp-includes/html-api/class-wp-html-processor.php
-
# The early return at the start of step() narrows $last_error to null, and PHPStan does not see that
diff --git a/src/wp-admin/comment.php b/src/wp-admin/comment.php
index ed93797a64faf..23a0ffd402498 100644
--- a/src/wp-admin/comment.php
+++ b/src/wp-admin/comment.php
@@ -296,14 +296,18 @@
comment_footer_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) );
}
- if ( wp_get_referer() && ! $noredir && ! str_contains( wp_get_referer(), 'comment.php' ) ) {
- $redir = wp_get_referer();
- } elseif ( wp_get_original_referer() && ! $noredir ) {
- $redir = wp_get_original_referer();
- } elseif ( in_array( $action, array( 'approvecomment', 'unapprovecomment' ), true ) ) {
- $redir = admin_url( 'edit-comments.php?p=' . absint( $comment->comment_post_ID ) );
+ $referer = wp_get_referer();
+ if ( $referer && ! $noredir && ! str_contains( $referer, 'comment.php' ) ) {
+ $redir = $referer;
} else {
- $redir = admin_url( 'edit-comments.php' );
+ $original_referer = wp_get_original_referer();
+ if ( $original_referer && ! $noredir ) {
+ $redir = $original_referer;
+ } elseif ( in_array( $action, array( 'approvecomment', 'unapprovecomment' ), true ) ) {
+ $redir = admin_url( 'edit-comments.php?p=' . absint( $comment->comment_post_ID ) );
+ } else {
+ $redir = admin_url( 'edit-comments.php' );
+ }
}
$redir = remove_query_arg( array( 'spammed', 'unspammed', 'trashed', 'untrashed', 'deleted', 'ids', 'approved', 'unapproved' ), $redir );
diff --git a/src/wp-admin/edit-comments.php b/src/wp-admin/edit-comments.php
index 46cf66d393985..0ddc6da7ac852 100644
--- a/src/wp-admin/edit-comments.php
+++ b/src/wp-admin/edit-comments.php
@@ -46,9 +46,12 @@
$doaction = $_REQUEST['action'];
} elseif ( isset( $_REQUEST['ids'] ) ) {
$comment_ids = array_map( 'absint', explode( ',', $_REQUEST['ids'] ) );
- } elseif ( wp_get_referer() ) {
- wp_safe_redirect( wp_get_referer() );
- exit;
+ } else {
+ $referer = wp_get_referer();
+ if ( $referer ) {
+ wp_safe_redirect( $referer );
+ exit;
+ }
}
$approved = 0;
diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php
index 076549b218b82..94fb7bb6fbd25 100644
--- a/src/wp-admin/includes/ajax-actions.php
+++ b/src/wp-admin/includes/ajax-actions.php
@@ -2301,9 +2301,10 @@ function wp_ajax_widgets_order() {
// Save widgets order for all sidebars.
if ( is_array( $_POST['sidebars'] ) ) {
- $sidebars = array();
+ $sidebars = array();
+ $posted_sidebars = wp_unslash( $_POST['sidebars'] );
- foreach ( wp_unslash( $_POST['sidebars'] ) as $key => $val ) {
+ foreach ( $posted_sidebars as $key => $val ) {
$sidebar = array();
if ( ! empty( $val ) ) {
diff --git a/src/wp-admin/includes/class-bulk-upgrader-skin.php b/src/wp-admin/includes/class-bulk-upgrader-skin.php
index d81eba885ddcd..466aaace1d33d 100644
--- a/src/wp-admin/includes/class-bulk-upgrader-skin.php
+++ b/src/wp-admin/includes/class-bulk-upgrader-skin.php
@@ -131,10 +131,11 @@ public function error( $errors ) {
}
if ( is_wp_error( $errors ) ) {
- $messages = array();
+ $messages = array();
+ $error_data = $errors->get_error_data();
foreach ( $errors->get_error_messages() as $emessage ) {
- if ( $errors->get_error_data() && is_string( $errors->get_error_data() ) ) {
- $messages[] = $emessage . ' ' . esc_html( strip_tags( $errors->get_error_data() ) );
+ if ( $error_data && is_string( $error_data ) ) {
+ $messages[] = $emessage . ' ' . esc_html( strip_tags( $error_data ) );
} else {
$messages[] = $emessage;
}
diff --git a/src/wp-admin/includes/class-theme-installer-skin.php b/src/wp-admin/includes/class-theme-installer-skin.php
index bd320ccb401c2..33c13d6c7de67 100644
--- a/src/wp-admin/includes/class-theme-installer-skin.php
+++ b/src/wp-admin/includes/class-theme-installer-skin.php
@@ -248,8 +248,9 @@ private function do_overwrite() {
echo '
' . esc_html__( 'This theme is already installed.' ) . '
';
// Check errors for active theme.
- if ( is_wp_error( $current_theme_data->errors() ) ) {
- $this->feedback( 'current_theme_has_errors', $current_theme_data->errors()->get_error_message() );
+ $current_theme_errors = $current_theme_data->errors();
+ if ( is_wp_error( $current_theme_errors ) ) {
+ $this->feedback( 'current_theme_has_errors', $current_theme_errors->get_error_message() );
}
$this->is_downgrading = version_compare( $current_theme_data['Version'], $new_theme_data['Version'], '>' );
diff --git a/src/wp-admin/includes/class-theme-upgrader.php b/src/wp-admin/includes/class-theme-upgrader.php
index 7e571e17f5c70..d876cbeec12e2 100644
--- a/src/wp-admin/includes/class-theme-upgrader.php
+++ b/src/wp-admin/includes/class-theme-upgrader.php
@@ -128,14 +128,15 @@ public function check_parent_theme_filter( $install_result, $hook_extra, $child_
// Check to see if we need to install a parent theme.
$theme_info = $this->theme_info();
- if ( ! $theme_info->parent() ) {
+ $parent_theme = $theme_info->parent();
+ if ( ! $parent_theme ) {
return $install_result;
}
$this->skin->feedback( 'parent_theme_search' );
- if ( ! $theme_info->parent()->errors() ) {
- $this->skin->feedback( 'parent_theme_currently_installed', $theme_info->parent()->display( 'Name' ), $theme_info->parent()->display( 'Version' ) );
+ if ( ! $parent_theme->errors() ) {
+ $this->skin->feedback( 'parent_theme_currently_installed', $parent_theme->display( 'Name' ), $parent_theme->display( 'Version' ) );
// We already have the theme, fall through.
return $install_result;
}
diff --git a/src/wp-admin/includes/class-wp-automatic-updater.php b/src/wp-admin/includes/class-wp-automatic-updater.php
index d60507cd3cfbf..01792a80445b5 100644
--- a/src/wp-admin/includes/class-wp-automatic-updater.php
+++ b/src/wp-admin/includes/class-wp-automatic-updater.php
@@ -935,6 +935,7 @@ protected function after_core_update( $update_result ) {
}
$error_code = $result->get_error_code();
+ $error_data = $result->get_error_data();
/*
* Any of these WP_Error codes are critical failures, as in they occurred after we started to copy core files.
@@ -943,10 +944,10 @@ protected function after_core_update( $update_result ) {
$critical = false;
if ( 'disk_full' === $error_code || str_contains( $error_code, '__copy_dir' ) ) {
$critical = true;
- } elseif ( 'rollback_was_required' === $error_code && is_wp_error( $result->get_error_data()->rollback ) ) {
+ } elseif ( 'rollback_was_required' === $error_code && is_wp_error( $error_data->rollback ) ) {
// A rollback is only critical if it failed too.
$critical = true;
- $rollback_result = $result->get_error_data()->rollback;
+ $rollback_result = $error_data->rollback;
} elseif ( str_contains( $error_code, 'do_rollback' ) ) {
$critical = true;
}
@@ -956,7 +957,7 @@ protected function after_core_update( $update_result ) {
'attempted' => $core_update->current,
'current' => $wp_version,
'error_code' => $error_code,
- 'error_data' => $result->get_error_data(),
+ 'error_data' => $error_data,
'timestamp' => time(),
'critical' => true,
);
@@ -1004,7 +1005,7 @@ protected function after_core_update( $update_result ) {
'attempted' => $core_update->current,
'current' => $wp_version,
'error_code' => $error_code,
- 'error_data' => $result->get_error_data(),
+ 'error_data' => $error_data,
'timestamp' => time(),
'retry' => in_array( $error_code, $transient_failures, true ),
)
diff --git a/src/wp-admin/includes/class-wp-filesystem-base.php b/src/wp-admin/includes/class-wp-filesystem-base.php
index 75ab85cd63d34..4972c8421cf11 100644
--- a/src/wp-admin/includes/class-wp-filesystem-base.php
+++ b/src/wp-admin/includes/class-wp-filesystem-base.php
@@ -606,8 +606,6 @@ public function chgrp( $file, $group, $recursive = false ) {
* @param bool $recursive Optional. If set to true, changes file permissions recursively.
* Default false.
* @return bool True on success, false on failure.
- *
- * @phpstan-impure
*/
public function chmod( $file, $mode = false, $recursive = false ) {
return false;
diff --git a/src/wp-admin/includes/class-wp-ms-themes-list-table.php b/src/wp-admin/includes/class-wp-ms-themes-list-table.php
index be0039b5ea965..82accb6f1b26c 100644
--- a/src/wp-admin/includes/class-wp-ms-themes-list-table.php
+++ b/src/wp-admin/includes/class-wp-ms-themes-list-table.php
@@ -139,6 +139,7 @@ public function prepare_items() {
'broken' => $this->is_site_themes ? array() : wp_get_themes( array( 'errors' => true ) ),
);
+ $auto_updates = array();
if ( $this->show_autoupdates ) {
$auto_updates = (array) get_site_option( 'auto_update_themes', array() );
@@ -731,10 +732,11 @@ public function column_name( $theme ) {
public function column_description( $theme ) {
global $status, $totals;
- if ( $theme->errors() ) {
+ $theme_errors = $theme->errors();
+ if ( $theme_errors ) {
$pre = 'broken' === $status ? '' . __( 'Broken Theme:' ) . ' ' : '';
wp_admin_notice(
- $pre . $theme->errors()->get_error_message(),
+ $pre . $theme_errors->get_error_message(),
array(
'type' => 'error',
'additional_classes' => array( 'inline' ),
@@ -779,11 +781,12 @@ public function column_description( $theme ) {
);
}
- if ( $theme->parent() ) {
+ $parent_theme = $theme->parent();
+ if ( $parent_theme ) {
$theme_meta[] = sprintf(
/* translators: %s: Theme name. */
__( 'Child theme of %s' ),
- '' . $theme->parent()->display( 'Name' ) . ''
+ '' . $parent_theme->display( 'Name' ) . ''
);
}
diff --git a/src/wp-admin/includes/class-wp-plugins-list-table.php b/src/wp-admin/includes/class-wp-plugins-list-table.php
index 6aa9c57cd0c03..6dc9dd5f42321 100644
--- a/src/wp-admin/includes/class-wp-plugins-list-table.php
+++ b/src/wp-admin/includes/class-wp-plugins-list-table.php
@@ -184,6 +184,7 @@ public function prepare_items() {
}
}
+ $show_network_active = false;
if ( ! $screen->in_admin( 'network' ) ) {
$show = current_user_can( 'manage_network_plugins' );
/**
diff --git a/src/wp-admin/includes/class-wp-posts-list-table.php b/src/wp-admin/includes/class-wp-posts-list-table.php
index d461aacd312ae..fb0309281a112 100644
--- a/src/wp-admin/includes/class-wp-posts-list-table.php
+++ b/src/wp-admin/includes/class-wp-posts-list-table.php
@@ -1815,9 +1815,8 @@ public function inline_edit() {
post_type, 'author' ) ) {
- $authors_dropdown = '';
-
if ( current_user_can( $post_type_object->cap->edit_others_posts ) ) {
$dropdown_name = 'post_author';
$dropdown_class = 'authors';
diff --git a/src/wp-admin/includes/class-wp-site-health.php b/src/wp-admin/includes/class-wp-site-health.php
index 90bb512de7758..1c6e3ae49607d 100644
--- a/src/wp-admin/includes/class-wp-site-health.php
+++ b/src/wp-admin/includes/class-wp-site-health.php
@@ -133,8 +133,9 @@ public function enqueue_scripts() {
$test['test']
);
- if ( method_exists( $this, $test_function ) && is_callable( array( $this, $test_function ) ) ) {
- $health_check_js_variables['site_status']['direct'][] = $this->perform_test( array( $this, $test_function ) );
+ $test_callback = array( $this, $test_function );
+ if ( method_exists( $this, $test_function ) && is_callable( $test_callback ) ) {
+ $health_check_js_variables['site_status']['direct'][] = $this->perform_test( $test_callback );
continue;
}
}
@@ -621,7 +622,8 @@ public function get_test_theme_version() {
if ( $has_unused_themes && ! is_multisite() ) {
// This is a child theme, so we want to be a bit more explicit in our messages.
- if ( $active_theme->parent() ) {
+ $parent_theme = $active_theme->parent();
+ if ( $parent_theme ) {
// Recommend removing inactive themes, except a default theme, your current one, and the parent theme.
$result['status'] = 'recommended';
@@ -643,7 +645,7 @@ public function get_test_theme_version() {
/* translators: 1: The currently active theme. 2: The active theme's parent theme. */
__( 'To enhance your site’s security, you should consider removing any themes you are not using. You should keep your active theme, %1$s, and %2$s, its parent theme.' ),
$active_theme->name,
- $active_theme->parent()->name
+ $parent_theme->name
)
);
} else {
@@ -663,7 +665,7 @@ public function get_test_theme_version() {
__( 'To enhance your site’s security, you should consider removing any themes you are not using. You should keep %1$s, the default WordPress theme, %2$s, your active theme, and %3$s, its parent theme.' ),
$default_theme ? $default_theme->name : WP_DEFAULT_THEME,
$active_theme->name,
- $active_theme->parent()->name
+ $parent_theme->name
)
);
}
@@ -1679,7 +1681,8 @@ public function get_test_scheduled_events() {
$this->wp_schedule_test_init();
- if ( is_wp_error( $this->has_missed_cron() ) ) {
+ $has_missed_cron = $this->has_missed_cron();
+ if ( is_wp_error( $has_missed_cron ) ) {
$result['status'] = 'critical';
$result['label'] = __( 'It was not possible to check your scheduled events' );
@@ -1689,10 +1692,10 @@ public function get_test_scheduled_events() {
sprintf(
/* translators: %s: The error message returned while from the cron scheduler. */
__( 'While trying to test your site’s scheduled events, the following error was returned: %s' ),
- $this->has_missed_cron()->get_error_message()
+ $has_missed_cron->get_error_message()
)
);
- } elseif ( $this->has_missed_cron() ) {
+ } elseif ( $has_missed_cron ) {
$result['status'] = 'recommended';
$result['label'] = __( 'A scheduled event has failed' );
@@ -3379,8 +3382,9 @@ public function wp_cron_scheduled_check() {
$test['test']
);
- if ( method_exists( $this, $test_function ) && is_callable( array( $this, $test_function ) ) ) {
- $results[] = $this->perform_test( array( $this, $test_function ) );
+ $test_callback = array( $this, $test_function );
+ if ( method_exists( $this, $test_function ) && is_callable( $test_callback ) ) {
+ $results[] = $this->perform_test( $test_callback );
continue;
}
}
diff --git a/src/wp-admin/includes/class-wp-upgrader-skin.php b/src/wp-admin/includes/class-wp-upgrader-skin.php
index 89fde5fc44e1e..ae35fda006b3c 100644
--- a/src/wp-admin/includes/class-wp-upgrader-skin.php
+++ b/src/wp-admin/includes/class-wp-upgrader-skin.php
@@ -180,9 +180,10 @@ public function error( $errors ) {
if ( is_string( $errors ) ) {
$this->feedback( $errors );
} elseif ( is_wp_error( $errors ) && $errors->has_errors() ) {
+ $error_data = $errors->get_error_data();
foreach ( $errors->get_error_messages() as $message ) {
- if ( $errors->get_error_data() && is_string( $errors->get_error_data() ) ) {
- $this->feedback( $message . ' ' . esc_html( strip_tags( $errors->get_error_data() ) ) );
+ if ( $error_data && is_string( $error_data ) ) {
+ $this->feedback( $message . ' ' . esc_html( strip_tags( $error_data ) ) );
} else {
$this->feedback( $message );
}
diff --git a/src/wp-admin/includes/file.php b/src/wp-admin/includes/file.php
index 79a08bb2b9cba..832be7aba9445 100644
--- a/src/wp-admin/includes/file.php
+++ b/src/wp-admin/includes/file.php
@@ -447,10 +447,11 @@ function wp_edit_theme_plugin_file( $args ) {
return new WP_Error( 'nonce_failure' );
}
- if ( $theme->errors() && 'theme_no_stylesheet' === $theme->errors()->get_error_code() ) {
+ $theme_errors = $theme->errors();
+ if ( $theme_errors && 'theme_no_stylesheet' === $theme_errors->get_error_code() ) {
return new WP_Error(
'theme_no_stylesheet',
- __( 'The requested theme does not exist.' ) . ' ' . $theme->errors()->get_error_message()
+ __( 'The requested theme does not exist.' ) . ' ' . $theme_errors->get_error_message()
);
}
diff --git a/src/wp-admin/includes/misc.php b/src/wp-admin/includes/misc.php
index 0b412dd56511f..47abc0512aa47 100644
--- a/src/wp-admin/includes/misc.php
+++ b/src/wp-admin/includes/misc.php
@@ -612,8 +612,9 @@ function wp_reset_vars( $vars ) {
*/
function show_message( $message ) {
if ( is_wp_error( $message ) ) {
- if ( $message->get_error_data() && is_string( $message->get_error_data() ) ) {
- $message = $message->get_error_message() . ': ' . $message->get_error_data();
+ $error_data = $message->get_error_data();
+ if ( $error_data && is_string( $error_data ) ) {
+ $message = $message->get_error_message() . ': ' . $error_data;
} else {
$message = $message->get_error_message();
}
diff --git a/src/wp-admin/includes/theme.php b/src/wp-admin/includes/theme.php
index 44e6651f09985..1b89546a066db 100644
--- a/src/wp-admin/includes/theme.php
+++ b/src/wp-admin/includes/theme.php
@@ -690,9 +690,8 @@ function wp_prepare_themes_for_js( $themes = null ) {
$slug = $theme->get_stylesheet();
$encoded_slug = urlencode( $slug );
- $parent = false;
- if ( $theme->parent() ) {
- $parent = $theme->parent();
+ $parent = $theme->parent();
+ if ( $parent ) {
$parents[ $slug ] = $parent->get_stylesheet();
$parent = $parent->display( 'Name' );
}
diff --git a/src/wp-admin/menu.php b/src/wp-admin/menu.php
index 36b10211daa1b..9923a665e91df 100644
--- a/src/wp-admin/menu.php
+++ b/src/wp-admin/menu.php
@@ -35,11 +35,12 @@
$submenu['index.php'][5] = array( __( 'My Sites' ), 'read', 'my-sites.php' );
}
-if ( ! is_multisite() || current_user_can( 'update_core' ) ) {
+$is_multisite = is_multisite();
+if ( ! $is_multisite || current_user_can( 'update_core' ) ) {
$update_data = wp_get_update_data();
}
-if ( ! is_multisite() ) {
+if ( ! $is_multisite ) {
if ( current_user_can( 'update_core' ) ) {
$capability = 'update_core';
} elseif ( current_user_can( 'update_plugins' ) ) {
@@ -211,7 +212,7 @@
$menu[60] = array( __( 'Appearance' ), $appearance_capability, 'themes.php', '', 'menu-top menu-icon-appearance', 'menu-appearance', 'dashicons-admin-appearance' );
$count = '';
-if ( ! is_multisite() && current_user_can( 'update_themes' ) ) {
+if ( ! $is_multisite && current_user_can( 'update_themes' ) ) {
if ( ! isset( $update_data ) ) {
$update_data = wp_get_update_data();
}
@@ -314,7 +315,7 @@ function _add_plugin_file_editor_to_tools() {
}
$count = '';
-if ( ! is_multisite() && current_user_can( 'update_plugins' ) ) {
+if ( ! $is_multisite && current_user_can( 'update_plugins' ) ) {
if ( ! isset( $update_data ) ) {
$update_data = wp_get_update_data();
}
@@ -340,7 +341,7 @@ function _add_plugin_file_editor_to_tools() {
}
}
-unset( $update_data );
+unset( $update_data, $is_multisite );
if ( current_user_can( 'list_users' ) ) {
$menu[70] = array( __( 'Users' ), 'list_users', 'users.php', '', 'menu-top menu-icon-users', 'menu-users', 'dashicons-admin-users' );
diff --git a/src/wp-admin/theme-editor.php b/src/wp-admin/theme-editor.php
index 68d6f9a7e966a..7a653fe9cb887 100644
--- a/src/wp-admin/theme-editor.php
+++ b/src/wp-admin/theme-editor.php
@@ -73,8 +73,9 @@
wp_die( __( 'The requested theme does not exist.' ) );
}
-if ( $theme->errors() && 'theme_no_stylesheet' === $theme->errors()->get_error_code() ) {
- wp_die( __( 'The requested theme does not exist.' ) . ' ' . $theme->errors()->get_error_message() );
+$theme_errors = $theme->errors();
+if ( $theme_errors && 'theme_no_stylesheet' === $theme_errors->get_error_code() ) {
+ wp_die( __( 'The requested theme does not exist.' ) . ' ' . $theme_errors->get_error_message() );
}
$allowed_files = array();
@@ -293,7 +294,8 @@