Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor namespaces, bump version, enforce type strictness #9

Merged
merged 4 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<element value="sift_decision_"/>
<element value="sift_decisions_"/>
<element value="wc_sift_decisions"/>
<element value="Sift_For_WooCommerce\Sift_For_WooCommerce"/>
<element value="Sift_For_WooCommerce"/>
</property>
</properties>
</rule>
Expand Down
2 changes: 1 addition & 1 deletion bin/test-payment-gateway/test-payment-gateway.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare( strict_types=1 );
/*
* Plugin Name: Simple Test Gateway for WooCommerce
* Description: A simple payment gateway that approves all checkouts in test mode
Expand Down
13 changes: 7 additions & 6 deletions sift-for-woocommerce.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
/**
* The Team51 Plugin Scaffold bootstrap file.
* Sift for WooCommerce
*
* @since 1.0.0
* @version 1.0.0
* @author WordPress.com Special Projects
* @author WooCommerce
* @license GPL-3.0-or-later
*
* @noinspection ALL
Expand All @@ -13,17 +13,18 @@
* Plugin Name: Sift For WooCommerce
* Plugin URI: https://wpspecialprojects.wordpress.com
* Description: A plugin to integrate WooCommerce with Sift Science Fraud Detection
* Version: 0.0.1-alpha
* Version: 1.0.0
* Requires at least: 6.2
* Tested up to: 6.2
* Requires PHP: 8.0
* Author: georgestephanis
* Author URI: https://wpspecialprojects.wordpress.com
* Author: WooCommerce
* Author URI: https://github.com/woocommerce/sift-for-woocommerce
* License: GPL v3 or later
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Text Domain: sift-for-woocommerce
* Domain Path: /languages
**/
declare( strict_types=1 );

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -55,4 +56,4 @@ static function () {

require_once __DIR__ . '/src/sift-for-woocommerce.php';

\Sift_For_WooCommerce\Sift_For_WooCommerce\Sift_For_WooCommerce::get_instance();
\Sift_For_WooCommerce\Sift_For_WooCommerce::get_instance();
2 changes: 1 addition & 1 deletion src/inc/abuse-decisions-actions.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Sift_For_WooCommerce\Sift_For_WooCommerce\Abuse_Decision_Actions;
namespace Sift_For_WooCommerce\Abuse_Decision_Actions;

/**
* Unblock a user from making purchases if Sift indicates that they are no longer a fraud risk.
Expand Down
7 changes: 3 additions & 4 deletions src/inc/abuse-decisions.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php
<?php declare( strict_types=1 );

namespace Sift_For_WooCommerce\Abuse_Decisions;

namespace Sift_For_WooCommerce\Sift_For_WooCommerce\Abuse_Decisions;

use function Sift_For_WooCommerce\Sift_For_WooCommerce\Abuse_Decision_Actions\{
use function Sift_For_WooCommerce\Abuse_Decision_Actions\{
unblock_user_from_purchases,
void_and_refund_user_orders,
cancel_and_remove_user_subscriptions,
Expand Down
2 changes: 1 addition & 1 deletion src/inc/payment-gateway.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare( strict_types=1 );

namespace Sift_For_WooCommerce\Sift_For_WooCommerce;
namespace Sift_For_WooCommerce;

/**
* A class representing a payment gateway plugin which normalizes the payment gateway property according to expectations in the Sift API.
Expand Down
2 changes: 1 addition & 1 deletion src/inc/payment-gateways/lib/stripe.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare( strict_types=1 );

namespace Sift_For_WooCommerce\Sift_For_WooCommerce\PaymentGateways\Lib;
namespace Sift_For_WooCommerce\PaymentGateways\Lib;

/**
* A class to share Stripe-specific logic with payment gateways that use Stripe in some way.
Expand Down
4 changes: 2 additions & 2 deletions src/inc/payment-gateways/stripe.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php declare( strict_types=1 );

namespace Sift_For_WooCommerce\Sift_For_WooCommerce\PaymentGateways;
namespace Sift_For_WooCommerce\PaymentGateways;

use Sift_For_WooCommerce\Sift_For_WooCommerce\PaymentGateways\Lib\Stripe;
use Sift_For_WooCommerce\PaymentGateways\Lib\Stripe;

/**
* Utility functions used by multiple filters when working with the woocommerce-gateway-stripe plugin.
Expand Down
2 changes: 1 addition & 1 deletion src/inc/payment-gateways/transact.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

add_filter( 'sift_for_woocommerce_woopayments_payment_gateway_string', fn() => '$stripe' );

add_filter( 'sift_for_woocommerce_woopayments_payment_type_string', array( \Sift_For_WooCommerce\Sift_For_WooCommerce\PaymentGateways\Lib\Stripe::class, 'convert_payment_type_to_sift_payment_type' ) );
add_filter( 'sift_for_woocommerce_woopayments_payment_type_string', array( \Sift_For_WooCommerce\PaymentGateways\Lib\Stripe::class, 'convert_payment_type_to_sift_payment_type' ) );

add_filter( 'sift_for_woocommerce_woopayments_payment_method_details_from_order', fn( $value, $order ) => $order ?? $value, 10, 2 );

Expand Down
4 changes: 2 additions & 2 deletions src/inc/payment-gateways/woocommerce-payments.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare( strict_types=1 );

add_filter( 'sift_for_woocommerce_woocommerce_payments_payment_gateway_string', fn() => '$stripe' );

Expand Down Expand Up @@ -29,7 +29,7 @@ function ( $value, \WC_Order $order ) {
}
);

add_filter( 'sift_for_woocommerce_woocommerce_payments_payment_type_string', array( \Sift_For_WooCommerce\Sift_For_WooCommerce\PaymentGateways\Lib\Stripe::class, 'convert_payment_type_to_sift_payment_type' ) );
add_filter( 'sift_for_woocommerce_woocommerce_payments_payment_type_string', array( \Sift_For_WooCommerce\PaymentGateways\Lib\Stripe::class, 'convert_payment_type_to_sift_payment_type' ) );
add_filter( 'sift_for_woocommerce_woocommerce_payments_card_last4', fn( $value, $woocommerce_payments_payment_method_details ) => $woocommerce_payments_payment_method_details['card']['last4'] ?? $value, 10, 2 );
add_filter( 'sift_for_woocommerce_woocommerce_payments_card_bin', fn( $value, $woocommerce_payments_payment_method_details ) => $woocommerce_payments_payment_method_details['card']['iin'] ?? $value, 10, 2 );

Expand Down
2 changes: 1 addition & 1 deletion src/inc/payment-method.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare( strict_types=1 );

namespace Sift_For_WooCommerce\Sift_For_WooCommerce;
namespace Sift_For_WooCommerce;

/**
* A class representing a payment method according to expectations in the Sift API.
Expand Down
2 changes: 1 addition & 1 deletion src/inc/payment-type.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare( strict_types=1 );

namespace Sift_For_WooCommerce\Sift_For_WooCommerce;
namespace Sift_For_WooCommerce;

/**
* A class representing a payment type which normalizes the payment type property according to expectations in the Sift API.
Expand Down
4 changes: 2 additions & 2 deletions src/inc/rest-api-webhooks.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
<?php declare( strict_types=1 );

namespace Sift_For_WooCommerce\Sift_For_WooCommerce\Rest_Api_Webhooks;
namespace Sift_For_WooCommerce\Rest_Api_Webhooks;

/**
* Register the routes for our webhook catchers with the REST API.
Expand Down
2 changes: 1 addition & 1 deletion src/inc/sift-object-validator.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare( strict_types = 1 );

namespace Sift_For_WooCommerce\Sift_For_WooCommerce\Sift;
namespace Sift_For_WooCommerce\Sift;

if ( ! defined( 'ABSPATH' ) ) {
exit;
Expand Down
2 changes: 1 addition & 1 deletion src/inc/sift-order.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare( strict_types=1 );

namespace Sift_For_WooCommerce\Sift_For_WooCommerce;
namespace Sift_For_WooCommerce;

/**
* A class representing a Woo order which normalizes order-related data according to expectations in the Sift API.
Expand Down
4 changes: 2 additions & 2 deletions src/inc/sift-property.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);
<?php declare( strict_types=1 );

namespace Sift_For_WooCommerce\Sift_For_WooCommerce;
namespace Sift_For_WooCommerce;

/**
* An abstract class which is the basis for a string or object representing a property as a complex data type in the Sift API.
Expand Down
4 changes: 2 additions & 2 deletions src/inc/tracking-js.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
<?php declare( strict_types=1 );

namespace Sift_For_WooCommerce\Sift_For_WooCommerce\Tracking_Js;
namespace Sift_For_WooCommerce\Tracking_Js;

/**
* This function can run multiple times, but will only print once.
Expand Down
2 changes: 1 addition & 1 deletion src/inc/verification-status.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare( strict_types=1 );

namespace Sift_For_WooCommerce\Sift_For_WooCommerce;
namespace Sift_For_WooCommerce;

/**
* A class representing a payment type which normalizes the payment type property according to expectations in the Sift API.
Expand Down
6 changes: 3 additions & 3 deletions src/inc/wc-settings-tab.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
<?php declare( strict_types=1 );

// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

namespace Sift_For_WooCommerce\Sift_For_WooCommerce\WC_Settings_Tab;
namespace Sift_For_WooCommerce\WC_Settings_Tab;

use Sift_For_WooCommerce\Sift_For_WooCommerce\Sift_For_WooCommerce;
use Sift_For_WooCommerce\Sift_For_WooCommerce;

/**
* Filter to slip in our settings tab.
Expand Down
22 changes: 15 additions & 7 deletions src/inc/woocommerce-actions.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
<?php declare( strict_types=1 );

// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

namespace Sift_For_WooCommerce\Sift_For_WooCommerce\WooCommerce_Actions;
namespace Sift_For_WooCommerce\WooCommerce_Actions;

use WC_Order_Item_Product;
use Sift_For_WooCommerce\Sift_For_WooCommerce\Sift\SiftObjectValidator;
use Sift_For_WooCommerce\Sift_For_WooCommerce\Sift_Order;
use Sift_For_WooCommerce\Sift\SiftObjectValidator;
use Sift_For_WooCommerce\Sift_Order;

/**
* Class Events
Expand Down Expand Up @@ -139,6 +139,10 @@ public static function login_success( string $username, object $user ) {
'$time' => intval( 1000 * microtime( true ) ),
);

if ( empty( $properties['$session_id'] ) ) {
unset( $properties['$session_id'] );
}

try {
SiftObjectValidator::validate_login( $properties );
} catch ( \Exception $e ) {
Expand All @@ -161,6 +165,10 @@ public static function login_success( string $username, object $user ) {
*/
public static function login_failure( string $username, \WP_Error $error ) {
$attempted_user = get_user_by( 'login', $username );
$user_id = null;
if ( is_object( $attempted_user ) ) {
$user_id = (string) $attempted_user->ID ?? null;
}

switch ( $error->get_error_code() ) {
case 'invalid_email':
Expand All @@ -178,7 +186,7 @@ public static function login_failure( string $username, \WP_Error $error ) {
$failure_reason = null;
}
$properties = array(
'$user_id' => (string) $attempted_user->ID ?? null,
'$user_id' => $user_id,
'$login_status' => '$failure',
'$session_id' => WC()->session->get_customer_unique_id(),
'$browser' => self::get_client_browser(), // alternately, `$app` for details of the app if not a browser.
Expand Down Expand Up @@ -523,7 +531,7 @@ public static function update_order( string $order_id, \WC_Order $order ) {
'$order_id' => $order_id,
'$verification_phone_number'
=> $order->get_billing_phone(),
'$amount' => self::get_transaction_micros( $order->get_total() ),
'$amount' => self::get_transaction_micros( floatval( $order->get_total() ) ),
'$currency_code' => get_woocommerce_currency(),
'$items' => $items,
'$payment_methods' => self::get_order_payment_methods( $order ),
Expand Down Expand Up @@ -592,7 +600,7 @@ public static function maybe_log_change_order_status( string $order_id, string $
public static function transaction( \WC_Order $order, string $status, string $transaction_type ) {
$properties = array(
'$user_id' => (string) $order->get_user_id(),
'$amount' => self::get_transaction_micros( $order->get_total() ), // Gotta multiply it up to give an integer.
'$amount' => self::get_transaction_micros( floatval( $order->get_total() ) ), // Gotta multiply it up to give an integer.
'$currency_code' => $order->get_currency(),
'$order_id' => (string) $order->get_id(),
'$transaction_type' => $transaction_type,
Expand Down
5 changes: 3 additions & 2 deletions src/sift-for-woocommerce.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Sift_For_WooCommerce\Sift_For_WooCommerce;
namespace Sift_For_WooCommerce;

require_once __DIR__ . '/inc/wc-settings-tab.php';
require_once __DIR__ . '/inc/rest-api-webhooks.php';
Expand All @@ -10,6 +10,7 @@
require_once __DIR__ . '/inc/payment-method.php';
require_once __DIR__ . '/inc/verification-status.php';
require_once __DIR__ . '/inc/payment-gateways/load.php';
require_once __DIR__ . '/inc/payment-gateway.php';
require_once __DIR__ . '/inc/sift-order.php';
require_once __DIR__ . '/inc/sift-object-validator.php';
require_once __DIR__ . '/inc/abuse-decisions.php';
Expand Down Expand Up @@ -47,7 +48,7 @@ protected function __construct() {

add_action( 'login_header', __NAMESPACE__ . '\Tracking_Js\print_sift_tracking_js' ); // Allow Sift tracking of login page visits.

\Sift_For_WooCommerce\Sift_For_WooCommerce\WooCommerce_Actions\Events::hooks();
\Sift_For_WooCommerce\WooCommerce_Actions\Events::hooks();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/AddsItemToCartEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
*
* @package Sift_For_WooCommerce
*/
declare( strict_types=1 );

require_once 'EventTest.php';

// phpcs:disable Universal.Arrays.DisallowShortArraySyntax.Found, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound

use Sift_For_WooCommerce\Sift_For_WooCommerce\WooCommerce_Actions\Events;
use Sift_For_WooCommerce\WooCommerce_Actions\Events;

/**
* Test case.
Expand Down
3 changes: 2 additions & 1 deletion tests/CreateAccountEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
*
* @package Sift_For_WooCommerce
*/
declare( strict_types=1 );

require_once 'EventTest.php';

// phpcs:disable Universal.Arrays.DisallowShortArraySyntax.Found, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound

use Sift_For_WooCommerce\Sift_For_WooCommerce\WooCommerce_Actions\Events;
use Sift_For_WooCommerce\WooCommerce_Actions\Events;

/**
* Test case.
Expand Down
3 changes: 2 additions & 1 deletion tests/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
*
* @package Sift_For_WooCommerce
*/
declare( strict_types=1 );

// phpcs:disable Universal.Arrays.DisallowShortArraySyntax.Found

use Sift_For_WooCommerce\Sift_For_WooCommerce\WooCommerce_Actions\Events;
use Sift_For_WooCommerce\WooCommerce_Actions\Events;

/**
* Events test case.
Expand Down
3 changes: 2 additions & 1 deletion tests/LinkSessionToUserEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
*
* @package Sift_For_WooCommerce
*/
declare( strict_types=1 );

require_once 'EventTest.php';

// phpcs:disable Universal.Arrays.DisallowShortArraySyntax.Found, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound

use Sift_For_WooCommerce\Sift_For_WooCommerce\WooCommerce_Actions\Events;
use Sift_For_WooCommerce\WooCommerce_Actions\Events;

/**
* Test case.
Expand Down
3 changes: 2 additions & 1 deletion tests/LoginEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
*
* @package Sift_For_WooCommerce
*/
declare( strict_types=1 );

require_once 'EventTest.php';

// phpcs:disable Universal.Arrays.DisallowShortArraySyntax.Found, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound

use Sift_For_WooCommerce\Sift_For_WooCommerce\WooCommerce_Actions\Events;
use Sift_For_WooCommerce\WooCommerce_Actions\Events;

/**
* Test case.
Expand Down
3 changes: 2 additions & 1 deletion tests/OrderStatusEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
*
* @package Sift_For_WooCommerce
*/
declare( strict_types=1 );

require_once 'EventTest.php';

// phpcs:disable Universal.Arrays.DisallowShortArraySyntax.Found, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound

use Sift_For_WooCommerce\Sift_For_WooCommerce\WooCommerce_Actions\Events;
use Sift_For_WooCommerce\WooCommerce_Actions\Events;

/**
* Test case.
Expand Down
3 changes: 2 additions & 1 deletion tests/PaymentGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
/**
* Class PaymentGatewayTest
*/
declare( strict_types=1 );

use Sift_For_WooCommerce\Sift_For_WooCommerce\Payment_Gateway;
use Sift_For_WooCommerce\Payment_Gateway;

require_once __DIR__ . '/../src/inc/payment-gateway.php';
require_once __DIR__ . '/../src/inc/payment-gateways/index.php';
Expand Down
Loading
Loading