Skip to content

Commit

Permalink
Banning unused 'use' statements and Yoda conditions (#161)
Browse files Browse the repository at this point in the history
* Banning unused 'use' statements and yoda conditions
* Linting out unused 'use' statements and yoda conditions
  • Loading branch information
aldavigdis authored Jul 14, 2024
1 parent a9a29aa commit f568849
Show file tree
Hide file tree
Showing 25 changed files with 233 additions and 231 deletions.
6 changes: 6 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
-->
<exclude name="WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase"></exclude>

<exclude name="WordPress.PHP.YodaConditions.NotYoda"></exclude>

<exclude-pattern>./tests/**</exclude-pattern>
</rule>

Expand Down Expand Up @@ -107,6 +109,10 @@
<rule ref="SlevomatCodingStandard.Operators.DisallowEqualOperators">
</rule>

<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses"></rule>

<rule ref="Generic.ControlStructures.DisallowYodaConditions"></rule>

<!--
Exclude the vendor direcotry because of course
-->
Expand Down
10 changes: 5 additions & 5 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ class Config {
* 3. The 1984_woo_dk_api_key WP option
*/
public static function get_dk_api_key(): string|false {
if ( true === defined( 'DK_API_KEY' ) ) {
if ( defined( 'DK_API_KEY' ) ) {
return constant( 'DK_API_KEY' );
}

if ( false !== getenv( 'DK_API_KEY' ) ) {
if ( getenv( 'DK_API_KEY' ) ) {
return getenv( 'DK_API_KEY' );
}

Expand All @@ -63,7 +63,7 @@ public static function get_dk_api_key(): string|false {
* @param string $value The API key value.
*/
public static function set_dk_api_key( string $value ): bool {
if ( 0 === preg_match( '/' . self::DK_API_KEY_REGEX . '/', $value ) ) {
if ( preg_match( '/' . self::DK_API_KEY_REGEX . '/', $value ) === 0 ) {
return false;
}
return update_option( '1984_woo_dk_api_key', $value );
Expand All @@ -87,7 +87,7 @@ public static function set_payment_mapping(
): bool {
$dk_payment_method = ImportSalesPayments::find_by_id( $dk_id );

if ( false === $dk_payment_method ) {
if ( ! $dk_payment_method ) {
return false;
}

Expand Down Expand Up @@ -117,7 +117,7 @@ public static function get_payment_mapping(
string $woo_id,
bool $empty_object = true
): stdClass {
if ( true === $empty_object ) {
if ( $empty_object ) {
$default = (object) array(
'woo_id' => '',
'dk_id' => '',
Expand Down
2 changes: 1 addition & 1 deletion src/Cron/Hourly.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Hourly {
* Saves all products from the DK API.
*/
public static function run(): void {
if ( false !== Config::get_dk_api_key() ) {
if ( Config::get_dk_api_key() ) {
ImportSalesPayments::get_methods();
ImportCurrencies::save_all_from_dk();
ImportProducts::save_all_from_dk();
Expand Down
14 changes: 7 additions & 7 deletions src/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function set_rate(
string $currency,
int|float $rate
): bool|WP_Error {
if ( 1 !== preg_match( self::CURRENCY_CODE_REGEX, $currency ) ) {
if ( preg_match( self::CURRENCY_CODE_REGEX, $currency ) !== 1 ) {
return self::invalid_currency_code_error( $currency );
}

Expand All @@ -58,15 +58,15 @@ public static function set_rate(
public static function get_rate(
string $currency
): float|WP_Error {
if ( 1 !== preg_match( self::CURRENCY_CODE_REGEX, $currency ) ) {
if ( preg_match( self::CURRENCY_CODE_REGEX, $currency ) !== 1 ) {
return self::invalid_currency_code_error( $currency );
}

$option_name = '1984_woo_dk_currency_rate_' . strtolower( $currency );

$rate = get_option( $option_name, 0 );

if ( false === $rate ) {
if ( ! $rate ) {
return self::rate_not_set_error( $currency );
}

Expand Down Expand Up @@ -96,19 +96,19 @@ public static function convert(
$to = Config::get_dk_currency();
}

if ( 1 !== preg_match( self::CURRENCY_CODE_REGEX, $from ) ) {
if ( preg_match( self::CURRENCY_CODE_REGEX, $from ) !== 1 ) {
return self::invalid_currency_code_error( $from );
}

if ( 1 !== preg_match( self::CURRENCY_CODE_REGEX, $to ) ) {
if ( preg_match( self::CURRENCY_CODE_REGEX, $to ) !== 1 ) {
return self::invalid_currency_code_error( $to );
}

$from_rate = get_option(
'1984_woo_dk_currency_rate_' . strtolower( $from ),
);

if ( false === $from_rate ) {
if ( ! $from_rate ) {
return self::rate_not_set_error( $from );
}

Expand All @@ -127,7 +127,7 @@ public static function convert(
'1984_woo_dk_currency_rate_' . strtolower( $to )
);

if ( false === $to_rate ) {
if ( ! $to_rate ) {
return self::rate_not_set_error( $to );
}

Expand Down
14 changes: 6 additions & 8 deletions src/Export/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function create_in_dk( WC_Customer $customer ): bool|WP_Error {
return $result;
}

if ( 200 !== $result->response_code ) {
if ( $result->response_code !== 200 ) {
return false;
}

Expand Down Expand Up @@ -80,7 +80,7 @@ public static function create_in_dk_from_order(
return $result;
}

if ( 200 !== $result->response_code ) {
if ( $result->response_code !== 200 ) {
return false;
}

Expand Down Expand Up @@ -113,7 +113,7 @@ public static function update_in_dk( WC_Customer $customer ): bool|WP_Error {
return $result;
}

if ( 200 !== $result->response_code ) {
if ( $result->response_code !== 200 ) {
return false;
}

Expand Down Expand Up @@ -148,7 +148,7 @@ public static function is_in_dk( WC_Customer|string $customer ): bool|WP_Error {
return $result;
}

if ( 200 !== $result->response_code ) {
if ( $result->response_code !== 200 ) {
return false;
}

Expand All @@ -168,9 +168,7 @@ public static function is_in_dk( WC_Customer|string $customer ): bool|WP_Error {
public static function has_dk_customer_number(
WC_Customer $customer
): bool {
if ( true === empty(
$customer->get_meta( '1984_woo_dk_customer_number' )
) ) {
if ( empty( $customer->get_meta( '1984_woo_dk_customer_number' ) ) ) {
return false;
}

Expand All @@ -193,7 +191,7 @@ public static function assume_dk_customer_number(
WC_Customer $customer
): string {
$kennitala = (string) $customer->get_meta( 'kennitala', true, 'edit' );
if ( false === empty( $kennitala ) ) {
if ( ! empty( $kennitala ) ) {
return $kennitala;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Export/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function create_in_dk(
return $result;
}

if ( 200 !== $result->response_code ) {
if ( $result->response_code !== 200 ) {
return false;
}

Expand All @@ -65,7 +65,7 @@ public static function get_from_dk(
return $result;
}

if ( 200 !== $result->response_code ) {
if ( $result->response_code !== 200 ) {
return false;
}

Expand Down
26 changes: 12 additions & 14 deletions src/Export/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace NineteenEightyFour\NineteenEightyWoo\Export;

use Automattic\WooCommerce\Admin\API\Customers;
use NineteenEightyFour\NineteenEightyWoo\Config;
use NineteenEightyFour\NineteenEightyWoo\Export\Order as ExportOrder;
use NineteenEightyFour\NineteenEightyWoo\Export\Customer as ExportCustomer;
Expand Down Expand Up @@ -42,7 +41,7 @@ public static function create_in_dk(

$invoice_number = self::get_dk_invoice_number( $wc_order );

if ( false === empty( $invoice_number ) ) {
if ( ! empty( $invoice_number ) ) {
return false;
}

Expand All @@ -58,7 +57,7 @@ public static function create_in_dk(
return $result;
}

if ( 200 !== $result->response_code ) {
if ( $result->response_code !== 200 ) {
return false;
}

Expand Down Expand Up @@ -90,7 +89,7 @@ public static function reverse_in_dk(
$api_request = new DKApiRequest();
$invoice_number = self::get_dk_invoice_number( $wc_order );

if ( true === empty( $invoice_number ) ) {
if ( empty( $invoice_number ) ) {
return false;
}

Expand All @@ -108,7 +107,7 @@ public static function reverse_in_dk(
return $result;
}

if ( 200 !== $result->response_code ) {
if ( $result->response_code !== 200 ) {
return false;
}

Expand Down Expand Up @@ -136,7 +135,7 @@ public static function email_in_dk(
string $invoice_type = 'debit'
): bool|WP_Error {
if (
false === in_array(
! in_array(
$invoice_type,
array( 'debit', 'credit' ),
true
Expand All @@ -147,7 +146,7 @@ public static function email_in_dk(

$to = $wc_order->get_billing_email();

if ( true === empty( $to ) ) {
if ( empty( $to ) ) {
return false;
}

Expand All @@ -164,15 +163,15 @@ public static function email_in_dk(

$api_request = new DKApiRequest();

if ( 'debit' === $invoice_type ) {
if ( $invoice_type === 'debit' ) {
$invoice_number = self::get_dk_invoice_number( $wc_order );
}

if ( 'credit' === $invoice_type ) {
if ( $invoice_type === 'credit' ) {
$invoice_number = self::get_dk_credit_invoice_number( $wc_order );
}

if ( true === empty( $invoice_number ) ) {
if ( empty( $invoice_number ) ) {
return false;
}

Expand All @@ -185,11 +184,11 @@ public static function email_in_dk(
return $result;
}

if ( 200 !== $result->response_code ) {
if ( $result->response_code !== 200 ) {
return false;
}

if ( 200 === $result->response_code ) {
if ( $result->response_code === 200 ) {
return true;
}

Expand All @@ -214,8 +213,7 @@ public static function to_dk_invoice_body(
$wc_order->get_payment_method()
);

if ( true === $wc_order->is_paid() ) {

if ( $wc_order->is_paid() ) {
$invoice_body['Payments'] = array(
array(
'ID' => $payment_mapping->dk_id,
Expand Down
23 changes: 9 additions & 14 deletions src/Export/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@

namespace NineteenEightyFour\NineteenEightyWoo\Export;

use Automattic\WooCommerce\Admin\API\Customers;
use NineteenEightyFour\NineteenEightyWoo\Brick\Math\BigDecimal;
use NineteenEightyFour\NineteenEightyWoo\Service\DKApiRequest;
use NineteenEightyFour\NineteenEightyWoo\Config;
use NineteenEightyFour\NineteenEightyWoo\Hooks\KennitalaField;
use NineteenEightyFour\NineteenEightyWoo\Helpers\Order as OrderHelper;
use NineteenEightyFour\NineteenEightyWoo\Export\Customer as ExportCustomer;
use NineteenEightyFour\NineteenEightyWoo\Import\ProductVariations;
use WC_Customer;
use WC_Order;
use WC_Product_Variation;
use WC_Order_Item_Product;
use WP_Error;

Expand Down Expand Up @@ -49,7 +44,7 @@ public static function create_in_dk(
return $result;
}

if ( 200 !== $result->response_code ) {
if ( $result->response_code !== 200 ) {
return false;
}

Expand All @@ -67,7 +62,7 @@ public static function create_in_dk(
* WP_Error if here was a connection error.
*/
public static function is_in_dk( WC_Order $wc_order ): bool|WP_Error {
if ( true === empty( self::get_dk_order_number( $wc_order ) ) ) {
if ( empty( self::get_dk_order_number( $wc_order ) ) ) {
return false;
}

Expand All @@ -81,7 +76,7 @@ public static function is_in_dk( WC_Order $wc_order ): bool|WP_Error {
return $result;
}

if ( 200 !== $result->response_code ) {
if ( $result->response_code !== 200 ) {
return false;
}

Expand Down Expand Up @@ -177,8 +172,8 @@ public static function to_dk_order_body( WC_Order $wc_order ): array {
$variation = wc_get_product( $order_item_product->get_variation_id() );

if (
'product_variation' === $origin &&
false !== $variation
$origin === 'product_variation' &&
$variation !== false
) {
$attributes = ProductVariations::attributes_to_woocommerce_variation_attributes( $variant_code );

Expand All @@ -202,7 +197,7 @@ public static function to_dk_order_body( WC_Order $wc_order ): array {
$order_props['Lines'][] = $order_line_item;
}

if ( 0 < count( $wc_order->get_fees() ) ) {
if ( count( $wc_order->get_fees() ) > 0 ) {
foreach ( $wc_order->get_fees() as $fee ) {
$sanitized_name = str_replace( '&nbsp;', '', $fee->get_name() );

Expand All @@ -216,9 +211,9 @@ public static function to_dk_order_body( WC_Order $wc_order ): array {
}
}

if ( 0 < count( $wc_order->get_shipping_methods() ) ) {
if ( count( $wc_order->get_shipping_methods() ) > 0 ) {
foreach ( $wc_order->get_shipping_methods() as $shipping_method ) {
if ( 0.0 === floatval( $shipping_method->get_total() ) ) {
if ( floatval( $shipping_method->get_total() ) === 0.0 ) {
continue;
}

Expand All @@ -238,7 +233,7 @@ public static function to_dk_order_body( WC_Order $wc_order ): array {
}
}

if ( 0 < $wc_order->get_total_discount() ) {
if ( $wc_order->get_total_discount() > 0 ) {
$order_props['Discount'] = $wc_order->get_total_discount();
}

Expand Down
Loading

0 comments on commit f568849

Please sign in to comment.