wp-cli / role-command

Adds, removes, lists, and resets roles and capabilities.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`reset` throws fatal in PHP 8

trepmal opened this issue · comments

Bug Report

Describe the current, buggy behavior

Running wp role reset <role> for missing role (or using --all) results in fatal error in PHP 8.

Describe how other contributors can replicate this bug

  • run wp role delete editor
  • run wp role reset editor
  • see Error: There has been a critical error on this website.Learn more about troubleshooting WordPress.type1messageUncaught TypeError: array_diff_key(): Argument #2 must be of type array, null given in phar:///usr/local/bin/wp/vendor/wp-cli/role-command/src/Role_Command.php:362

$restored_cap = array_diff_key( $after[ $role_key ]->capabilities, $before[ $role_key ]->capabilities );
$removed_cap = array_diff_key( $before[ $role_key ]->capabilities, $after[ $role_key ]->capabilities );

This only affects the reporting of the reset, the role is successfully restored ahead of the error.

Describe what you would expect as the correct outcome

No error. Success message/notice.

Let us know what environment you are running this on

$ wp --info
OS:	Linux 5.10.0-16-amd64 #1 SMP Debian 5.10.127-2 (2022-07-23) x86_64
Shell:	/bin/bash
PHP binary:	/usr/local/php8.1/bin/php
PHP version:	8.1.16
php.ini used:	/usr/local/php8.1/conf/php.ini
MySQL binary:	/usr/bin/mysql
MySQL version:	mysql  Ver 15.1 Distrib 10.5.18-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper
SQL modes:	ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
WP-CLI root dir:	phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	/chroot/var/www
WP-CLI packages dir:	
WP-CLI global config:	
WP-CLI project config:	
WP-CLI version:	2.6.0

Provide a possible solution

Plenty of variations:

$before_caps = $before[ $role_key ] ? $before[ $role_key ]->capabilities : [];
$restored_cap = array_diff_key( $after[ $role_key ]->capabilities, $before_caps );
$removed_cap  = array_diff_key( $before_caps, $after[ $role_key ]->capabilities );

Thanks for the report, @trepmal !