Skip to content

Code Quality: Declare WP globals and initialize variables for PHPStan level 1#11979

Open
huzaifaalmesbah wants to merge 1 commit into
WordPress:trunkfrom
huzaifaalmesbah:fix/phpstan-level1-undefined-vars
Open

Code Quality: Declare WP globals and initialize variables for PHPStan level 1#11979
huzaifaalmesbah wants to merge 1 commit into
WordPress:trunkfrom
huzaifaalmesbah:fix/phpstan-level1-undefined-vars

Conversation

@huzaifaalmesbah
Copy link
Copy Markdown
Member

Resolves PHPStan level 1 variable.undefined errors in four user/plugin admin files (98 of 564 project-wide level-1 errors). The project's enforced level (0) continues to pass.

  • Declare @global docblocks + global statements for core globals ($wpdb, $current_user, $wp_roles, $_wp_admin_css_colors, $status, $page, $plugins, $user_ID, $usersearch, $blog_id).
  • Initialize $redirect in wp-admin/users.php for the empty( $_REQUEST ) branch.
  • Initialize $user_id, $old_user_data at the top of wp_insert_user() and $manage_url in _wp_privacy_send_request_confirmation_notification() so they're always defined.
  • Bug fix: wp-admin/user-edit.php:166 referenced an undefined $user_login in the multisite signup-email update query; corrected to $user->user_login.

Files changed

File Errors before (level 1) After
src/wp-admin/plugins.php 40 0
src/wp-admin/users.php 25 0
src/wp-admin/user-edit.php 15 0
src/wp-includes/user.php 18 0

Verification

vendor/bin/phpstan analyse --level=1 --memory-limit=2G \
  --configuration=phpstan.neon.dist \
  src/wp-admin/plugins.php \
  src/wp-admin/users.php \
  src/wp-admin/user-edit.php \
  src/wp-includes/user.php

Expected: [OK] No errors

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

…atisfy PHPStan level 1.

Add `@global` docblocks and `global` declarations in `wp-admin/plugins.php`,
`wp-admin/users.php`, and `wp-admin/user-edit.php` so variables provided by
`WP_Plugins_List_Table`, `WP_Users_List_Table`, `$wpdb`, `$current_user`,
`$wp_roles`, `$_wp_admin_css_colors`, and related core globals are visible to
static analysis. Initialize `$redirect` in `wp-admin/users.php` for the empty
request branch.

In `wp-includes/user.php`, initialize `$user_id` and `$old_user_data` at the
top of `wp_insert_user()`, and `$manage_url` in
`_wp_privacy_send_request_confirmation_notification()` so they are always
defined regardless of branch.

Also fix a latent bug in `wp-admin/user-edit.php` where the multisite signup
email update used a bare `$user_login` variable that was never defined; it
should reference `$user->user_login`.
@github-actions
Copy link
Copy Markdown

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.

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

@github-actions
Copy link
Copy Markdown

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.

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.

1 participant