Fix Psalm errors

This commit is contained in:
Narek Zakarian 2022-08-16 18:46:17 +04:00
parent 09420c0adb
commit 9b81ac54f2
5 changed files with 65 additions and 48 deletions

View file

@ -30,6 +30,11 @@ return array(
);
},
'order-tracking.module.url' => static function ( ContainerInterface $container ): string {
/**
* The path cannot be false.
*
* @psalm-suppress PossiblyFalseArgument
*/
return plugins_url(
'/modules/ppcp-order-tracking/',
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'

View file

@ -14,14 +14,14 @@ use Psr\Log\LoggerInterface;
use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatus;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Factory\SellerStatusFactory;
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait;
/**
* The OrderTrackingEndpoint.
*
* @psalm-type SupportedStatuses = 'SHIPPED'|'ON_HOLD'|'DELIVERED'|'CANCELLED'
* @psalm-type TrackingInfo = array{transaction_id: string, status: SupportedStatuses, tracking_number?: string, carrier?: string}
* @psalm-type RequestValues = array{transaction_id: string, status: SupportedStatuses, order_id: int, action: 'create'|'update', tracking_number?: string, carrier?: string}
@ -61,27 +61,6 @@ class OrderTrackingEndpoint {
*/
private $logger;
/**
* The seller status factory.
*
* @var SellerStatusFactory
*/
private $seller_status_factory;
/**
* The partner ID.
*
* @var string
*/
private $partner_id;
/**
* The merchant ID.
*
* @var string
*/
private $merchant_id;
/**
* PartnersEndpoint constructor.
*
@ -105,7 +84,7 @@ class OrderTrackingEndpoint {
/**
* Handles the request.
*/
public function handle_request() {
public function handle_request(): void {
try {
$data = $this->request_data->read_request( $this->nonce() );
$action = $data['action'];
@ -164,6 +143,11 @@ class OrderTrackingEndpoint {
throw $error;
}
/**
* Need to ignore Method WP_Error::offsetGet does not exist
*
* @psalm-suppress UndefinedMethod
*/
$json = json_decode( $response['body'] );
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( 200 !== $status_code ) {
@ -185,7 +169,7 @@ class OrderTrackingEndpoint {
throw $error;
}
update_post_meta( $order_id, '_ppcp_paypal_tracking_number', $data['tracking_number'] );
update_post_meta( $order_id, '_ppcp_paypal_tracking_number', $data['tracking_number'] ?? '' );
}
/**
@ -194,6 +178,7 @@ class OrderTrackingEndpoint {
* @param int $wc_order_id The order ID.
* @return array|null The tracking information.
* @psalm-return TrackingInfo|null
* @throws RuntimeException If problem getting.
*/
public function get_tracking_information( int $wc_order_id ) : ?array {
$wc_order = wc_get_order( $wc_order_id );
@ -227,6 +212,11 @@ class OrderTrackingEndpoint {
throw $error;
}
/**
* Need to ignore Method WP_Error::offsetGet does not exist
*
* @psalm-suppress UndefinedMethod
*/
$data = json_decode( $response['body'] );
$status_code = (int) wp_remote_retrieve_response_code( $response );
@ -234,12 +224,7 @@ class OrderTrackingEndpoint {
return null;
}
return array(
'transaction_id' => $data->transaction_id ?? '',
'status' => $data->status ?? '',
'tracking_number' => $data->tracking_number ?? '',
'carrier' => $data->carrier ?? '',
);
return $this->extract_tracking_information( (array) $data );
}
/**
@ -279,6 +264,11 @@ class OrderTrackingEndpoint {
throw $error;
}
/**
* Need to ignore Method WP_Error::offsetGet does not exist
*
* @psalm-suppress UndefinedMethod
*/
$json = json_decode( $response['body'] );
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( 204 !== $status_code ) {
@ -300,7 +290,7 @@ class OrderTrackingEndpoint {
throw $error;
}
update_post_meta( $order_id, '_ppcp_paypal_tracking_number', $data['tracking_number'] );
update_post_meta( $order_id, '_ppcp_paypal_tracking_number', $data['tracking_number'] ?? '' );
}
/**
@ -313,20 +303,33 @@ class OrderTrackingEndpoint {
}
/**
* Extracts the needed tracking information from given request data.
* Extracts the needed tracking information from given data.
*
* @param array $data The request data map.
* @psalm-param RequestValues $data
* @return array A map of tracking information keys to values.
* @psalm-return TrackingInfo
* @throws RuntimeException If problem extracting.
*/
protected function extract_tracking_information( array $data ): array {
return array(
'transaction_id' => $data['transaction_id'] ?? '',
'tracking_number' => $data['tracking_number'] ?? '',
'status' => $data['status'] ?? '',
'carrier' => $data['carrier'] ?? '',
if ( empty( $data['transaction_id'] ) || empty( $data['status'] ) ) {
$this->logger->log( 'warning', 'Missing transaction_id or status.' );
throw new RuntimeException( 'Missing transaction_id or status.' );
}
$tracking_info = array(
'transaction_id' => $data['transaction_id'],
'status' => $data['status'],
);
if ( ! empty( $data['status'] ) ) {
$tracking_info['status'] = $data['status'];
}
if ( ! empty( $data['carrier'] ) ) {
$tracking_info['carrier'] = $data['carrier'];
}
return $tracking_info;
}
/**

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\OrderTracking;
use WC_Order;
use WooCommerce\PayPalCommerce\OrderTracking\Endpoint\OrderTrackingEndpoint;
use WP_Post;
@ -71,13 +72,17 @@ class MetaBoxRenderer {
*
* @param WP_Post $post The post object.
*/
public function render( WP_Post $post ) {
$wc_order = wc_get_order( $post->ID );
public function render( WP_Post $post ): void {
$wc_order = wc_get_order( $post->ID );
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return;
}
$tracking_info = $this->order_tracking_endpoint->get_tracking_information( $post->ID );
$tracking_is_not_added = empty( $tracking_info );
$transaction_id = $tracking_info['transaction_id'] ?? $wc_order->get_transaction_id() ?? '';
$transaction_id = $tracking_info['transaction_id'] ?? $wc_order->get_transaction_id() ?: '';
$tracking_number = $tracking_info['tracking_number'] ?? '';
$status_value = $tracking_info['status'] ?? 'SHIPPED';
$carrier_value = $tracking_info['carrier'] ?? '';
@ -107,18 +112,18 @@ class MetaBoxRenderer {
<option value=""><?php echo esc_html__( 'Select Carrier', 'woocommerce-paypal-payments' ); ?></option>
<?php
foreach ( $carriers as $carrier ) :
$country = $carrier['name'] ?? '';
$carriers = $carrier['items'] ?? '';
$country = $carrier['name'] ?? '';
$carrier_items = $carrier['items'] ?? array();
?>
<optgroup label="<?php echo esc_attr( $country ); ?>">
<?php foreach ( $carriers as $carrier_code => $carrier_name ) : ?>
<?php foreach ( $carrier_items as $carrier_code => $carrier_name ) : ?>
<option value="<?php echo esc_attr( $carrier_code ); ?>" <?php selected( $carrier_value, $carrier_code ); ?>><?php echo esc_html( $carrier_name ); ?></option>
<?php endforeach; ?>
</optgroup>
<?php endforeach; ?>
</select>
</p>
<input type="hidden" class="ppcp-order_id" name="<?php echo esc_attr( self::NAME_PREFIX ); ?>[order_id]" value="<?php echo esc_html( $post->ID ); ?>"/>
<input type="hidden" class="ppcp-order_id" name="<?php echo esc_attr( self::NAME_PREFIX ); ?>[order_id]" value="<?php echo intval( $post->ID ); ?>"/>
<p>
<button type="button" class="button submit_tracking_info" data-action="<?php echo esc_attr( $action ); ?>"><?php echo esc_html( ucfirst( $action ) ); ?></button></p>
<?php

View file

@ -145,9 +145,13 @@ class OrderTrackingModule implements ModuleInterface {
return;
}
$wc_order = wc_get_order( $order_id );
$wc_order = wc_get_order( $order_id );
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return;
}
$transaction_id = $wc_order->get_transaction_id();
if ( ! is_a( $wc_order, WC_Order::class ) || empty( $transaction_id ) ) {
if ( empty( $transaction_id ) ) {
return;
}

View file

@ -235,7 +235,7 @@ class SettingsListener {
*
* @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found.
*/
public function listen() {
public function listen(): void {
if ( ! $this->is_valid_update_request() ) {
return;