Fix PHPStan undefined variable errors at level 3#11189
Fix PHPStan undefined variable errors at level 3#11189huzaifaalmesbah wants to merge 3 commits intoWordPress:trunkfrom
Conversation
|
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. |
|
Thanks for the PR. Are these issues only at level 3 or do they also occur at level 1? I have #11151 as well which is for fixing issues at rule level 1. I wonder if we could consolidate fixes rather than fix them one-by-one which will add a lot of overhead for committing. |
|
I've updated #11151 to now use an allow-list for unknown globals. This PR almost allows us to eliminate the three variables referenced in that PR. |
@westonruter Sounds good. should I close this as redundant since #11151 already handles these via baseline, or would you like me to also fix $table_prefix in wp-includes/ms-settings.php? |
|
It seems @SergeyBiryukov found an alternative fix for the issue with So if you want to revert your change to |
| * @global bool $public Deprecated. Whether the site found on load is public. | ||
| * Use `get_site()->public` instead. | ||
| * @global string $table_prefix Database table prefix. | ||
| * @global wpdb $wpdb WordPress database abstraction object. |
There was a problem hiding this comment.
Global populated by require_wp_db().
| * Use `get_current_network_id()` instead. | ||
| * @global bool $public Deprecated. Whether the site found on load is public. | ||
| * Use `get_site()->public` instead. | ||
| * @global string $table_prefix Database table prefix. |
There was a problem hiding this comment.
Global populated in wp-config.php.
| * @global string $table_prefix Database table prefix. | ||
| */ | ||
| global $wp_version, $wp_db_version, $tinymce_version, $required_php_version, $required_php_extensions, $required_mysql_version, $wp_local_package; | ||
| global $wp_version, $wp_db_version, $tinymce_version, $required_php_version, $required_php_extensions, $required_mysql_version, $wp_local_package, $wp_filter, $table_prefix; |
There was a problem hiding this comment.
There's something curious below in how the $table_prefix is set:
wordpress-develop/src/wp-settings.php
Lines 140 to 147 in 30d971c
This to me looks like dead code. But in fact, it is pretty new: 61ae275
It was developed for Core-63627.
I wonder if explicitly globalizing $table_prefix that this could break how this was intended to be used, if $table_prefix is loaded in a lexical scope somehow which is distinct from the $table_prefix which is also a global. I'm not sure how this would work, but it gives me pause.
@SergeyBiryukov can you shed some light on this as you made that commit?
It may be safer to do this patch instead:
diff --git a/src/wp-settings.php b/src/wp-settings.php
index 023cdccd5e..87fa935aa1 100644
--- a/src/wp-settings.php
+++ b/src/wp-settings.php
@@ -100,7 +100,7 @@ if ( WP_CACHE && apply_filters( 'enable_loading_advanced_cache_dropin', true ) &
include WP_CONTENT_DIR . '/advanced-cache.php';
// Re-initialize any hooks added manually by advanced-cache.php.
- if ( $wp_filter ) {
+ if ( isset( $wp_filter ) ) {
$wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter );
}
}
@@ -140,7 +140,7 @@ require_wp_db();
*
* @global string $table_prefix The database table prefix.
*/
-if ( ! isset( $GLOBALS['table_prefix'] ) ) {
+if ( ! isset( $GLOBALS['table_prefix'] ) && isset( $table_prefix ) ) {
$GLOBALS['table_prefix'] = $table_prefix;
}But let's get confirmation.
There was a problem hiding this comment.
Question raised: https://core.trac.wordpress.org/ticket/63627#comment:6
Summary
Fixes PHPStan level 3 errors for undefined variables in:
wp-admin/comment.php- Added@global int $comment_idPHPDocwp-settings.php- Added@global array $wp_filterand@global string $table_prefixPHPDocTesting
./vendor/bin/phpstan analyse --level=3 src/wp-admin/comment.php src/wp-settings.phpTrac: https://core.trac.wordpress.org/ticket/64238
Use of AI Tools
Used AI tools for research and assistance in preparing the commit.