Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/wp-includes/ms-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
wp_load_core_site_options( $site_id );
}

assert( isset( $wpdb ) );
assert( isset( $table_prefix ) );
$wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php.
$wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id );
$table_prefix = $wpdb->get_blog_prefix();
Expand Down
3 changes: 2 additions & 1 deletion src/wp-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
include WP_CONTENT_DIR . '/advanced-cache.php';

// Re-initialize any hooks added manually by advanced-cache.php.
if ( $wp_filter ) {
if ( ! empty( $wp_filter ) ) {
Copy link

@Ninos Ninos Mar 10, 2026

Choose a reason for hiding this comment

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

May use isset() instead?

if ( isset( $wp_filter ) ) {

Copy link
Member

Choose a reason for hiding this comment

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

That would be not the same though. Before it was checking for a truthy value, which is what ! empty() does. On the other hand, isset() would return true for an empty array.

Copy link

Choose a reason for hiding this comment

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

Ahh, true! I'm just trying to avoid non-strict type functions like empty() :D Following should also be possible, but what u prefer :-)

if ( $wp_filter ?? null ) {

$wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter );
}
}
Expand Down Expand Up @@ -141,6 +141,7 @@
* @global string $table_prefix The database table prefix.
*/
if ( ! isset( $GLOBALS['table_prefix'] ) ) {
assert( isset( $table_prefix ) );
Copy link

@Ninos Ninos Mar 10, 2026

Choose a reason for hiding this comment

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

To be honest, I don't understand this part, assert() is a test function & shouldn't be used in productive code...?

What u want to achieve here? phpstan's crying because $table_prefix is may undefined? If so, you could change to following:

if ( ! isset( $GLOBALS['table_prefix'] ) ) {
    `$GLOBALS['table_prefix'] = $table_prefix ?? null;`
}

Tested till php7.0, so should not break with any versions from last century... :D

Copy link
Member

Choose a reason for hiding this comment

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

It is for PHPStan, yes.

Conventional wisdom is that assert() isn't used in production code, and yet https://www.php.net/manual/en/function.assert.php indicates they "are optimised away to have zero cost in production".

The goal of using assert() was to reduce the cyclomatic complexity raised by @siliconforks in #11189 (comment)

Copy link

Choose a reason for hiding this comment

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

Thx, in my opinion it's better to rly solve the issue instead of hiding it (may undefined variable). I think my recommendation above should be fine for that 👼

Choose a reason for hiding this comment

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

Thx, in my opinion it's better to rly solve the issue instead of hiding it (may undefined variable)

Where is the actual issue, though? I don't see any issue here. By the time this code runs, both $wpdb and $table_prefix have already been defined. PHPStan doesn't know that, so it gives a warning, but that's just a false positive.

Is there some way that either $wpdb or $table_prefix (or both) could be undefined at this point? If there is, how? What are the steps needed to reproduce this?

Copy link
Member

Choose a reason for hiding this comment

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

As I understand from the custom bootstrap code used by @Ninos, it's possible that $GLOBALS['table_prefix'] is not set, and that the bootstrap logic defines a non-global $table_prefix which is expected here. However, it could be that the bootstrap logic actually neglects to assign a $table_prefix variable. In this case, an error would happen.

Honestly, this would be extremely unlikely.

Copy link

Choose a reason for hiding this comment

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

@siliconforks the "issue" here is, that a variable can be undefined, which is at least a notice, but so an error. Also if it makes no sense to not set any of both variables (gobal or "local"), the code can throw a notice/error, from runtime perspective, this is some small anomaly :D

In my opinions, it's cleaner code to not get any notices & errors, even if something is missconfigured or failed. It's not much improvements, but adding some small ?? null will not hurt here I think 👼

$GLOBALS['table_prefix'] = $table_prefix;
}

Expand Down
Loading