Skip to content

Fix PHPStan undefined variable errors at level 3#11189

Open
huzaifaalmesbah wants to merge 3 commits intoWordPress:trunkfrom
huzaifaalmesbah:fix/phpstan-undefined-variables
Open

Fix PHPStan undefined variable errors at level 3#11189
huzaifaalmesbah wants to merge 3 commits intoWordPress:trunkfrom
huzaifaalmesbah:fix/phpstan-undefined-variables

Conversation

@huzaifaalmesbah
Copy link
Member

@huzaifaalmesbah huzaifaalmesbah commented Mar 6, 2026

Summary

Fixes PHPStan level 3 errors for undefined variables in:

  • wp-admin/comment.php - Added @global int $comment_id PHPDoc
  • wp-settings.php - Added @global array $wp_filter and @global string $table_prefix PHPDoc

Testing

  • Run ./vendor/bin/phpstan analyse --level=3 src/wp-admin/comment.php src/wp-settings.php
  • Before: 6 errors
  • After: 0 errors

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

Use of AI Tools

Used AI tools for research and assistance in preparing the commit.

@github-actions
Copy link

github-actions bot commented Mar 6, 2026

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props huzaifaalmesbah, westonruter.

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

@github-actions
Copy link

github-actions bot commented Mar 6, 2026

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.

@westonruter
Copy link
Member

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.

@westonruter
Copy link
Member

westonruter commented Mar 6, 2026

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.

@huzaifaalmesbah
Copy link
Member Author

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?

@westonruter
Copy link
Member

It seems @SergeyBiryukov found an alternative fix for the issue with $comment_id in r61859 (ce40cd3).

So if you want to revert your change to src/wp-admin/comment.php and then follow up with fixing wp-includes/ms-settings.php, then we can commit this PR.

* @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.
Copy link
Member

Choose a reason for hiding this comment

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

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.
Copy link
Member

Choose a reason for hiding this comment

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

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;
Copy link
Member

Choose a reason for hiding this comment

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

There's something curious below in how the $table_prefix is set:

/**
* @since 3.3.0
*
* @global string $table_prefix The database table prefix.
*/
if ( ! isset( $GLOBALS['table_prefix'] ) ) {
$GLOBALS['table_prefix'] = $table_prefix;
}

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.

Copy link
Member

Choose a reason for hiding this comment

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

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.

2 participants