Apply Rector suggestions for PHP 8.1 (#41482)

* Apply Rector suggestions

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

* Restore false positive

$missing_tables will always be an array

* Restore false positive.

Rector issue: AddDefaultValueForUndefinedVariableRector.

This time the early return is valid so there’s no need to move the $process_limit assignment to the top.

* Ask if $orders is an array

$orders is used as an array lines below. Add `is_array` just in case the “paginate” param is used which would change $orders to an object.

* Check if variable is array

Simplifies variable check given that they are used as an array lines below.

* Restore code as taxes is always an array

* Restore false positive

In the previous loop the $data variable is being built as an array of arrays. Then $value in this loop will allways be array.

* Restore false positive

get_children’s method. of a WC_Product instance will always return an array.

* Simplify variable check

Ask if varialbe is array since it only can be array or false.

* Restore false positive

WC_Blocks_Utils::get_blocks_from_page() will always return an array.

* Restore false positive

There’s a check asking if $existing_meta_data[ $meta_data->key ] is array.

* Restore false positive

$child_ids will always be an array.

* Restore false positives

WC_Product->get_visible_children() will always return an array.

* Restore false positives

WC_Product->get_children() will always return an array.

* Restore false positive

WC_Product->get_visible_children() will always return an array.

* Restore false positive

WC_Order->get_items() will always return an array.

* Restore false positive

The get_matching_rates private method will always return an array.

Updated the @return declaration to be an array instead of boolean.

* Remove unnecessary array type casting.

* Restore false positive

$this->get_children() will always return an array.

* Fix lint issues

* Remove unnecessary default assignment

* Remove unnecessary is_array check

* Remove unnecessary is_array check

* Remove unnecessary is_array check

* Change default value from null to false

* Remove unnecessary is_array check

* Update changelog entry

* Update changelog entry

* Update changelog entry

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

* Update changelog entry

* Use self for consistency

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Alfredo Sumaran 2024-02-01 05:06:12 -05:00 committed by GitHub
parent 7f556d07b2
commit 9bb7cd7b8b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 22 additions and 15 deletions

View File

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

View File

@ -29,18 +29,18 @@ class WC_Product_Factory {
return false;
}
$product_type = $this->get_product_type( $product_id );
$product_type = self::get_product_type( $product_id );
// Backwards compatibility.
if ( ! empty( $deprecated ) ) {
wc_deprecated_argument( 'args', '3.0', 'Passing args to the product factory is deprecated. If you need to force a type, construct the product class directly.' );
if ( isset( $deprecated['product_type'] ) ) {
$product_type = $this->get_classname_from_product_type( $deprecated['product_type'] );
$product_type = self::get_classname_from_product_type( $deprecated['product_type'] );
}
}
$classname = $this->get_product_classname( $product_id, $product_type );
$classname = self::get_product_classname( $product_id, $product_type );
try {
return new $classname( $product_id, $deprecated );

View File

@ -554,7 +554,7 @@ class WC_Query {
*/
private function product_query_post_clauses( $args, $wp_query ) {
$args = $this->price_filter_post_clauses( $args, $wp_query );
$args = $this->filterer->filter_by_attribute_post_clauses( $args, $wp_query, $this->get_layered_nav_chosen_attributes() );
$args = $this->filterer->filter_by_attribute_post_clauses( $args, $wp_query, self::get_layered_nav_chosen_attributes() );
return $args;
}
@ -790,7 +790,7 @@ class WC_Query {
if ( $main_query && ! $this->filterer->filtering_via_lookup_table_is_active() ) {
// Layered nav filters on terms.
foreach ( $this->get_layered_nav_chosen_attributes() as $taxonomy => $data ) {
foreach ( self::get_layered_nav_chosen_attributes() as $taxonomy => $data ) {
$tax_query[] = array(
'taxonomy' => $taxonomy,
'field' => 'slug',

View File

@ -625,8 +625,8 @@ class WC_Tracker {
$curr_tokens = preg_split( '/[ :,\-_]+/', $key );
$prev_tokens = preg_split( '/[ :,\-_]+/', $prev );
$len_curr = count( $curr_tokens );
$len_prev = count( $prev_tokens );
$len_curr = is_array( $curr_tokens ) ? count( $curr_tokens ) : 0;
$len_prev = is_array( $prev_tokens ) ? count( $prev_tokens ) : 0;
$index_unique = -1;
// Gather the common tokens.
@ -640,7 +640,7 @@ class WC_Tracker {
}
// If only one token is different, and those tokens contain digits, then that could be the unique id.
if ( count( $curr_tokens ) - count( $comm_tokens ) <= 1 && count( $comm_tokens ) > 0 && $index_unique > -1 ) {
if ( $len_curr - count( $comm_tokens ) <= 1 && count( $comm_tokens ) > 0 && $index_unique > -1 ) {
$objects[ $key ]->group_key = implode( ' ', $comm_tokens );
$objects[ $prev ]->group_key = implode( ' ', $comm_tokens );
} else {
@ -1073,7 +1073,8 @@ class WC_Tracker {
*/
public static function get_pickup_location_data() {
$pickup_location_enabled = false;
$pickup_locations_count = count( get_option( 'pickup_location_pickup_locations', array() ) );
$pickup_location_pickup_locations = get_option( 'pickup_location_pickup_locations', array() );
$pickup_locations_count = is_countable( $pickup_location_pickup_locations ) ? count( $pickup_location_pickup_locations ) : 0;
// Get the available shipping methods.
$shipping_methods = WC()->shipping()->get_shipping_methods();

View File

@ -89,6 +89,7 @@ if ( ! class_exists( 'WC_Email_New_Order' ) ) :
$order = wc_get_order( $order_id );
}
$email_already_sent = false;
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );

View File

@ -236,6 +236,7 @@ class WC_Gateway_COD extends WC_Payment_Gateway {
$data_store = WC_Data_Store::load( 'shipping-zone' );
$raw_zones = $data_store->get_zones();
$zones = array();
foreach ( $raw_zones as $raw_zone ) {
$zones[] = new WC_Shipping_Zone( $raw_zone );
@ -327,7 +328,7 @@ class WC_Gateway_COD extends WC_Payment_Gateway {
* @since 3.4.0
*
* @param array $rate_ids Rate ids to check.
* @return boolean
* @return array
*/
private function get_matching_rates( $rate_ids ) {
// First, match entries in 'method_id:instance_id' format. Then, match entries in 'method_id' format by stripping off the instance ID from the candidates.

View File

@ -418,11 +418,11 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
$result = WC_Gateway_Paypal_API_Handler::refund_transaction( $order, $amount, $reason );
if ( is_wp_error( $result ) ) {
$this->log( 'Refund Failed: ' . $result->get_error_message(), 'error' );
static::log( 'Refund Failed: ' . $result->get_error_message(), 'error' );
return new WP_Error( 'error', $result->get_error_message() );
}
$this->log( 'Refund Result: ' . wc_print_r( $result, true ) );
static::log( 'Refund Result: ' . wc_print_r( $result, true ) );
switch ( strtolower( $result->ACK ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
case 'success':
@ -450,13 +450,13 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
$result = WC_Gateway_Paypal_API_Handler::do_capture( $order );
if ( is_wp_error( $result ) ) {
$this->log( 'Capture Failed: ' . $result->get_error_message(), 'error' );
static::log( 'Capture Failed: ' . $result->get_error_message(), 'error' );
/* translators: %s: Paypal gateway error message */
$order->add_order_note( sprintf( __( 'Payment could not be captured: %s', 'woocommerce' ), $result->get_error_message() ) );
return;
}
$this->log( 'Capture Result: ' . wc_print_r( $result, true ) );
static::log( 'Capture Result: ' . wc_print_r( $result, true ) );
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
if ( ! empty( $result->PAYMENTSTATUS ) ) {