Apply Rector suggestions for PHP 8.1 (#41253)

* Apply Rector suggestions

* Add changefile(s) from automation for the following project(s): woocommerce

* Apply feedback suggestions

* Add missing if

* Align assignments to satisfy the linter

* Move count call outside the loop to satisfy the linter

* Escape output to satisfy the linter

* Remove unnecessary assignment

* Remove unnecessary is_countable check

* Remove unnecessary array casting

* Restore variable name

* Update changelog entry

* change static to self for consistency
This commit is contained in:
Alfredo Sumaran 2024-02-19 15:29:16 -05:00 committed by GitHub
parent 5b420159cb
commit 0407569382
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 47 additions and 44 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
Apply type checks and enhancements for PHP 8.1

View File

@ -293,11 +293,11 @@ class WC_Admin_Importers {
'position' => 'done',
'percentage' => 100,
'url' => add_query_arg( array( '_wpnonce' => wp_create_nonce( 'woocommerce-csv-importer' ) ), admin_url( 'edit.php?post_type=product&page=product_importer&step=done' ) ),
'imported' => count( $results['imported'] ),
'imported_variations' => count( $results['imported_variations'] ),
'failed' => count( $results['failed'] ),
'updated' => count( $results['updated'] ),
'skipped' => count( $results['skipped'] ),
'imported' => is_countable( $results['imported'] ) ? count( $results['imported'] ) : 0,
'imported_variations' => is_countable( $results['imported_variations'] ) ? count( $results['imported_variations'] ) : 0,
'failed' => is_countable( $results['failed'] ) ? count( $results['failed'] ) : 0,
'updated' => is_countable( $results['updated'] ) ? count( $results['updated'] ) : 0,
'skipped' => is_countable( $results['skipped'] ) ? count( $results['skipped'] ) : 0,
)
);
} else {
@ -305,11 +305,11 @@ class WC_Admin_Importers {
array(
'position' => $importer->get_file_position(),
'percentage' => $percent_complete,
'imported' => count( $results['imported'] ),
'imported_variations' => count( $results['imported_variations'] ),
'failed' => count( $results['failed'] ),
'updated' => count( $results['updated'] ),
'skipped' => count( $results['skipped'] ),
'imported' => is_countable( $results['imported'] ) ? count( $results['imported'] ) : 0,
'imported_variations' => is_countable( $results['imported_variations'] ) ? count( $results['imported_variations'] ) : 0,
'failed' => is_countable( $results['failed'] ) ? count( $results['failed'] ) : 0,
'updated' => is_countable( $results['updated'] ) ? count( $results['updated'] ) : 0,
'skipped' => is_countable( $results['skipped'] ) ? count( $results['skipped'] ) : 0,
)
);
}

View File

@ -192,7 +192,7 @@ class WC_Admin_Webhooks {
$webhook_id = absint( $_GET['delete'] );
if ( $webhook_id ) {
$this->bulk_delete( array( $webhook_id ) );
self::bulk_delete( array( $webhook_id ) );
}
}
}

View File

@ -408,7 +408,7 @@ class WC_Helper {
}
$filters = array_fill_keys( array_keys( self::get_filters() ), 0 );
if ( empty( $subscriptions ) ) {
if ( ! is_array( $subscriptions ) || empty( $subscriptions ) ) {
return array();
}
@ -1461,7 +1461,7 @@ class WC_Helper {
if ( is_readable( $txt ) ) {
$txt = file_get_contents( $txt );
$txt = preg_split( '#\s#', $txt );
if ( count( $txt ) >= 2 ) {
if ( is_array( $txt ) && count( $txt ) >= 2 ) {
$header = sprintf( '%d:%s', $txt[0], $txt[1] );
}
}

View File

@ -146,8 +146,8 @@ class WC_Tax_Rate_Importer extends WP_Importer {
if ( false !== $handle ) {
$header = fgetcsv( $handle, 0, $this->delimiter );
if ( 10 === count( $header ) ) {
$count = is_countable( $header ) ? count( $header ) : 0;
if ( 10 === $count ) {
$row = fgetcsv( $handle, 0, $this->delimiter );

View File

@ -80,7 +80,7 @@ if ( ! defined( 'ABSPATH' ) ) {
</thead>
<tbody>
<?php
if ( count( $errors ) ) {
if ( is_array( $errors ) && count( $errors ) ) {
foreach ( $errors as $error ) {
if ( ! is_wp_error( $error ) ) {
continue;

View File

@ -136,8 +136,7 @@ class WC_Admin_List_Table_Coupons extends WC_Admin_List_Table {
*/
protected function render_products_column() {
$product_ids = $this->object->get_product_ids();
if ( count( $product_ids ) > 0 ) {
if ( is_array( $product_ids ) && count( $product_ids ) > 0 ) {
echo esc_html( implode( ', ', $product_ids ) );
} else {
echo '&ndash;';

View File

@ -15,8 +15,7 @@ $arrow_img_url = WC_ADMIN_IMAGES_FOLDER_URL . '/product_data/no-variati
?>
<div id="variable_product_options" class="panel wc-metaboxes-wrapper hidden">
<div id="variable_product_options_inner">
<?php if ( ! count( $variation_attributes ) ) : ?>
<?php if ( ! isset( $variation_attributes ) || ! is_array( $variation_attributes ) || count( $variation_attributes ) === 0 ) : ?>
<div class="add-attributes-container">
<div class="add-attributes-message">

View File

@ -343,7 +343,7 @@ class WC_Report_Sales_By_Category extends WC_Admin_Report {
$index = 0;
foreach ( $chart_data as $data ) {
$color = isset( $this->chart_colours[ $index ] ) ? $this->chart_colours[ $index ] : $this->chart_colours[0];
$width = $this->barwidth / sizeof( $chart_data );
$width = $this->barwidth / count( $chart_data );
$offset = ( $width * $index );
$series = $data['data'];

View File

@ -178,7 +178,7 @@ class WC_Settings_Tax extends WC_Settings_Page {
public function output_tax_rates() {
global $current_section;
$current_class = $this->get_current_tax_class();
$current_class = self::get_current_tax_class();
$countries = array();
foreach ( WC()->countries->get_allowed_countries() as $value => $label ) {
@ -320,7 +320,7 @@ class WC_Settings_Tax extends WC_Settings_Page {
// phpcs:disable WordPress.Security.NonceVerification.Missing -- this is called via "do_action('woocommerce_settings_save_'...") in base class, where nonce is verified first.
global $wpdb;
$current_class = sanitize_title( $this->get_current_tax_class() );
$current_class = sanitize_title( self::get_current_tax_class() );
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.NonceVerification.Missing
$posted_countries = wc_clean( wp_unslash( $_POST['tax_rate_country'] ) );

View File

@ -25,6 +25,10 @@ $logging = $report['logging'];
$wp_pages = $report['pages'];
$plugin_updates = new WC_Plugin_Updates();
$untested_plugins = $plugin_updates->get_untested_plugins( WC()->version, Constants::get_constant( 'WC_SSR_PLUGIN_UPDATE_RELEASE_VERSION_TYPE' ) );
$active_plugins_count = is_countable( $active_plugins ) ? count( $active_plugins ) : 0;
$inactive_plugins_count = is_countable( $inactive_plugins ) ? count( $inactive_plugins ) : 0;
?>
<div class="updated woocommerce-message inline">
<p>
@ -575,7 +579,7 @@ $untested_plugins = $plugin_updates->get_untested_plugins( WC()->version, Cons
<table class="wc_status_table widefat" cellspacing="0">
<thead>
<tr>
<th colspan="3" data-export-label="Active Plugins (<?php echo count( $active_plugins ); ?>)"><h2><?php esc_html_e( 'Active plugins', 'woocommerce' ); ?> (<?php echo count( $active_plugins ); ?>)</h2></th>
<th colspan="3" data-export-label="Active Plugins (<?php echo esc_attr( $active_plugins_count ); ?>)"><h2><?php esc_html_e( 'Active plugins', 'woocommerce' ); ?> (<?php echo esc_attr( $active_plugins_count ); ?>)</h2></th>
</tr>
</thead>
<tbody>
@ -585,7 +589,7 @@ $untested_plugins = $plugin_updates->get_untested_plugins( WC()->version, Cons
<table class="wc_status_table widefat" cellspacing="0">
<thead>
<tr>
<th colspan="3" data-export-label="Inactive Plugins (<?php echo count( $inactive_plugins ); ?>)"><h2><?php esc_html_e( 'Inactive plugins', 'woocommerce' ); ?> (<?php echo count( $inactive_plugins ); ?>)</h2></th>
<th colspan="3" data-export-label="Inactive Plugins (<?php echo esc_attr( $inactive_plugins_count ); ?>)"><h2><?php esc_html_e( 'Inactive plugins', 'woocommerce' ); ?> (<?php echo esc_attr( $inactive_plugins_count ); ?>)</h2></th>
</tr>
</thead>
<tbody>
@ -593,12 +597,13 @@ $untested_plugins = $plugin_updates->get_untested_plugins( WC()->version, Cons
</tbody>
</table>
<?php
if ( 0 < count( $dropins_mu_plugins['dropins'] ) ) :
$dropins_count = is_countable( $dropins_mu_plugins['dropins'] ) ? count( $dropins_mu_plugins['dropins'] ) : 0;
if ( 0 < $dropins_count ) :
?>
<table class="wc_status_table widefat" cellspacing="0">
<thead>
<tr>
<th colspan="3" data-export-label="Dropin Plugins (<?php echo count( $dropins_mu_plugins['dropins'] ); ?>)"><h2><?php esc_html_e( 'Dropin Plugins', 'woocommerce' ); ?> (<?php echo count( $dropins_mu_plugins['dropins'] ); ?>)</h2></th>
<th colspan="3" data-export-label="Dropin Plugins (<?php $dropins_count; ?>)"><h2><?php esc_html_e( 'Dropin Plugins', 'woocommerce' ); ?> (<?php $dropins_count; ?>)</h2></th>
</tr>
</thead>
<tbody>
@ -617,12 +622,14 @@ if ( 0 < count( $dropins_mu_plugins['dropins'] ) ) :
</table>
<?php
endif;
if ( 0 < count( $dropins_mu_plugins['mu_plugins'] ) ) :
$mu_plugins_count = is_countable( $dropins_mu_plugins['mu_plugins'] ) ? count( $dropins_mu_plugins['mu_plugins'] ) : 0;
if ( 0 < $mu_plugins_count ) :
?>
<table class="wc_status_table widefat" cellspacing="0">
<thead>
<tr>
<th colspan="3" data-export-label="Must Use Plugins (<?php echo count( $dropins_mu_plugins['mu_plugins'] ); ?>)"><h2><?php esc_html_e( 'Must Use Plugins', 'woocommerce' ); ?> (<?php echo count( $dropins_mu_plugins['mu_plugins'] ); ?>)</h2></th>
<th colspan="3" data-export-label="Must Use Plugins (<?php echo esc_attr( $mu_plugins_count ); ?>)"><h2><?php esc_html_e( 'Must Use Plugins', 'woocommerce' ); ?> (<?php echo esc_attr( $mu_plugins_count ); ?>)</h2></th>
</tr>
</thead>
<tbody>
@ -959,7 +966,7 @@ if ( 0 < count( $dropins_mu_plugins['mu_plugins'] ) ) :
<td class="help">&nbsp;</td>
<td>
<?php
$total_overrides = count( $theme['overrides'] );
$total_overrides = is_countable( $theme['overrides'] ) ? count( $theme['overrides'] ) : 0;
for ( $i = 0; $i < $total_overrides; $i++ ) {
$override = $theme['overrides'][ $i ];
if ( $override['core_version'] && ( empty( $override['version'] ) || version_compare( $override['version'], $override['core_version'], '<' ) ) ) {
@ -974,7 +981,8 @@ if ( 0 < count( $dropins_mu_plugins['mu_plugins'] ) ) :
} else {
echo esc_html( $override['file'] );
}
if ( ( count( $theme['overrides'] ) - 1 ) !== $i ) {
if ( ( $total_overrides - 1 ) !== $i ) {
echo ', ';
}
echo '<br />';

View File

@ -2064,7 +2064,8 @@ class WC_AJAX {
$children = get_terms( $taxonomy, "child_of=$id&menu_order=ASC&hide_empty=0" );
if ( $term && count( $children ) ) {
$children_count = is_countable( $children ) ? count( $children ) : 0;
if ( $term && $children_count ) {
echo 'children';
wp_die();
}

View File

@ -175,14 +175,6 @@ class WC_Deprecated_Action_Hooks extends WC_Deprecated_Hooks {
do_action( $old_hook, $order_id, $item_id, $item, $item->get_product() );
}
break;
case 'woocommerce_order_update_coupon':
case 'woocommerce_order_update_shipping':
case 'woocommerce_order_update_fee':
case 'woocommerce_order_update_tax':
if ( ! is_a( $item, 'WC_Order_Item_Product' ) ) {
do_action( $old_hook, $order_id, $item_id, $item );
}
break;
default:
do_action_ref_array( $old_hook, $new_callback_args );
break;

View File

@ -1194,7 +1194,7 @@ class WC_Geo_IP {
$this->memory_buffer = fread( $this->filehandle, $s_array['size'] );
}
} else {
$this->log( 'GeoIP API: Can not open ' . $filename, 'error' );
self::log( 'GeoIP API: Can not open ' . $filename, 'error' );
}
}
@ -1553,7 +1553,7 @@ class WC_Geo_IP {
}
}
$this->log( 'GeoIP API: Error traversing database - perhaps it is corrupt?', 'error' );
self::log( 'GeoIP API: Error traversing database - perhaps it is corrupt?', 'error' );
return false;
}
@ -1608,7 +1608,7 @@ class WC_Geo_IP {
}
}
$this->log( 'GeoIP API: Error traversing database - perhaps it is corrupt?', 'error' );
self::log( 'GeoIP API: Error traversing database - perhaps it is corrupt?', 'error' );
return false;
}
@ -1637,7 +1637,7 @@ class WC_Geo_IP {
*/
public function geoip_country_id_by_addr_v6( $addr ) {
if ( ! defined( 'AF_INET6' ) ) {
$this->log( 'GEOIP (geoip_country_id_by_addr_v6): PHP was compiled with --disable-ipv6 option' );
self::log( 'GEOIP (geoip_country_id_by_addr_v6): PHP was compiled with --disable-ipv6 option' );
return false;
}
$ipnum = inet_pton( $addr );