Fix psalm

This commit is contained in:
Emili Castells Guasch 2025-08-06 13:54:57 +02:00
parent 38f7355db1
commit 03f68dc545
No known key found for this signature in database
2 changed files with 36 additions and 19 deletions

View file

@ -17,33 +17,33 @@ class Shipping {
/**
* The name.
*
* @var string
* @var string|null
*/
private $name;
private ?string $name;
/**
* The address.
*
* @var Address
* @var Address|null
*/
private $address;
private ?Address $address;
/**
* Custom contact email address, usually added via the Contact Module.
*/
private ?string $email_address = null;
private ?string $email_address;
/**
* Custom contact phone number, usually added via the Contact Module.
*/
private ?Phone $phone_number = null;
private ?Phone $phone_number;
/**
* Shipping methods.
*
* @var ShippingOption[]
*/
private $options;
private array $options;
/**
* Shipping constructor.
@ -130,7 +130,7 @@ class Shipping {
$address = $this->address();
if ( $address ) {
$result['address'] = $this->address()->to_array();
$result['address'] = $address->to_array();
}
$contact_email = $this->email_address();

View file

@ -15,6 +15,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\PurchaseUnitSanitizer;
use WooCommerce\PayPalCommerce\Webhooks\CustomIds;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Address;
/**
* Class PurchaseUnitFactory
@ -108,21 +109,20 @@ class PurchaseUnitFactory {
* @return PurchaseUnit
*/
public function from_wc_order( \WC_Order $order ): PurchaseUnit {
$amount = $this->amount_factory->from_wc_order( $order );
$items = array_filter(
$amount = $this->amount_factory->from_wc_order( $order );
$items = array_filter(
$this->item_factory->from_wc_order( $order ),
function ( Item $item ): bool {
return $item->unit_amount()->value() >= 0;
}
);
$shipping = $this->shipping_factory->from_wc_order( $order );
if (
! $this->shipping_needed( ... array_values( $items ) ) ||
empty( $shipping->address()->country_code() ) ||
( ! $shipping->address()->postal_code() && ! $this->country_without_postal_code( $shipping->address()->country_code() ) )
) {
$shipping = $this->shipping_factory->from_wc_order( $order );
$shipping_address = $shipping->address();
if ( $this->should_disable_shipping( $items, $shipping_address ) ) {
$shipping = null;
}
$reference_id = 'default';
$description = '';
$custom_id = (string) $order->get_id();
@ -176,10 +176,12 @@ class PurchaseUnitFactory {
$shipping = null;
$customer = \WC()->customer;
if ( $this->shipping_needed( ... array_values( $items ) ) && is_a( $customer, \WC_Customer::class ) ) {
$shipping = $this->shipping_factory->from_wc_customer( \WC()->customer, $with_shipping_options );
$shipping = $this->shipping_factory->from_wc_customer( \WC()->customer, $with_shipping_options );
$shipping_address = $shipping->address();
if (
2 !== strlen( $shipping->address()->country_code() ) ||
( ! $shipping->address()->postal_code() && ! $this->country_without_postal_code( $shipping->address()->country_code() ) )
! $shipping_address ||
2 !== strlen( $shipping_address->country_code() ) ||
( ! $shipping_address->postal_code() && ! $this->country_without_postal_code( $shipping_address->country_code() ) )
) {
$shipping = null;
}
@ -337,4 +339,19 @@ class PurchaseUnitFactory {
return substr( $sanitized, 0, 22 ) ?: '';
}
/**
* Determines whether shipping should be disabled for a purchase unit.
*
* @param array $items Purchase unit items.
* @param Address|null $shipping_address The shipping address to validate.
*
* @return bool
*/
public function should_disable_shipping( array $items, ?Address $shipping_address ): bool {
return ! $this->shipping_needed( ... array_values( $items ) ) ||
! $shipping_address ||
empty( $shipping_address->country_code() ) ||
( ! $shipping_address->postal_code() && ! $this->country_without_postal_code( $shipping_address->country_code() ) );
}
}