vindi/vindi-woocommerce

View on GitHub
src/services/VindiHelpers.php

Summary

Maintainability
B
6 hrs
Test Coverage
<?php
 
namespace VindiPaymentGateways;
 
class VindiHelpers
{
 
 
Visibility must be declared on method "__construct"
Line indented incorrectly; expected 4 spaces, found 2
function __construct()
Line indented incorrectly; expected at least 4 spaces, found 2
{
 
Line indented incorrectly; expected at least 8 spaces, found 4
add_action('woocommerce_process_product_meta', array($this, 'wc_post_meta'));
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* Sanitize statement descriptor text.
*
* Vindi requires max of 22 characters and no
* special characters with ><"'.
*
* @since 1.0.0
* @param string $statement_descriptor
* @return string $statement_descriptor Sanitized statement descriptor
*/
Line indented incorrectly; expected 4 spaces, found 2
public static function clean_statement_descriptor($statement_descriptor = '')
Line indented incorrectly; expected at least 4 spaces, found 2
{
Line indented incorrectly; expected at least 8 spaces, found 4
$disallowed_characters = array('<', '>', '"', "'");
 
// Remove special characters.
Line indented incorrectly; expected at least 8 spaces, found 4
$statement_descriptor = str_replace($disallowed_characters, '', $statement_descriptor);
 
Line indented incorrectly; expected at least 8 spaces, found 4
$statement_descriptor = substr(trim($statement_descriptor), 0, 22);
 
Line indented incorrectly; expected at least 8 spaces, found 4
return $statement_descriptor;
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* Get Vindi amount to pay
*
* @param float $total Amount due.
* @param string $currency Accepted currency.
*
* @return float|int
*/
 
Line indented incorrectly; expected 4 spaces, found 2
public static function get_vindi_amount($total, $currency = '')
Line indented incorrectly; expected at least 4 spaces, found 2
{
Line indented incorrectly; expected 8 spaces, found 4
if (!$currency) {
Line indented incorrectly; expected at least 12 spaces, found 6
$currency = get_woocommerce_currency();
Line indented incorrectly; expected 8 spaces, found 4
}
 
Line indented incorrectly; expected at least 8 spaces, found 4
return absint(wc_format_decimal(((float) $total * 100), wc_get_price_decimals())); // In cents.
 
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* Checks if WC version is less than passed in version.
*
* @since 1.0.0
* @param string $version Version to check against.
* @return bool
*/
Line indented incorrectly; expected 4 spaces, found 2
public static function is_wc_lt($version)
Line indented incorrectly; expected at least 4 spaces, found 2
{
Line indented incorrectly; expected at least 8 spaces, found 4
return version_compare(WC_VERSION, $version, '<');
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* Save Woocommerce custom attributes
*
* @since 1.0.0
* @param string $version Version to check against.
* @return null
*/
 
Line indented incorrectly; expected 4 spaces, found 2
public static function wc_post_meta($post_id, $custom_attributes)
Line indented incorrectly; expected at least 4 spaces, found 2
{
 
// Get product
Line indented incorrectly; expected at least 8 spaces, found 4
$product = wc_get_product($post_id);
 
Avoid variables with short names like $i. Configured minimum length is 3.
Line indented incorrectly; expected at least 8 spaces, found 4
$i = 0;
 
// Loop through the attributes array
Blank line found at start of control structure
Line indented incorrectly; expected 8 spaces, found 4
foreach ($custom_attributes as $name => $value) {
 
// Check meta value exists
Line indented incorrectly; expected at least 12 spaces, found 6
$product->update_meta_data($name, $value);
 
Line indented incorrectly; expected at least 12 spaces, found 6
$i++;
Line indented incorrectly; expected 8 spaces, found 4
}
 
Line indented incorrectly; expected at least 8 spaces, found 4
$product->save();
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* Get a subscription that has an item equals as an order item, if any.
*
* @since 1.0.0
* @param WC_Order $order A WC_Order object
* @param WC_Order_Item_Product $order_item The order item
*
* @return WC_Subscription
*/
Function `get_matching_subscription` has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
Line indented incorrectly; expected 4 spaces, found 2
public static function get_matching_subscription($order, $order_item)
Line indented incorrectly; expected at least 4 spaces, found 2
{
Spaces must be used to indent lines; tabs are not allowed
$subscriptions = wcs_get_subscriptions_for_order($order, array('order_type' => 'parent'));
Line indented incorrectly; expected at least 8 spaces, found 4
$matching_subscription = null;
Line indented incorrectly; expected 8 spaces, found 4
foreach ($subscriptions as $subscription) {
Line indented incorrectly; expected 12 spaces, found 6
foreach ($subscription->get_items() as $subscription_item) {
Avoid unused local variables such as '$match_type'.
Line indented incorrectly; expected at least 16 spaces, found 8
$line_item = wcs_find_matching_line_item($order, $subscription_item, $match_type = 'match_attributes');
Line indented incorrectly; expected 16 spaces, found 8
Expected 1 space after IF keyword; 0 found
if($order_item === $line_item) {
Line indented incorrectly; expected at least 20 spaces, found 10
$matching_subscription = $subscription;
Line indented incorrectly; expected at least 20 spaces, found 10
break 2;
Line indented incorrectly; expected 16 spaces, found 8
}
Line indented incorrectly; expected 12 spaces, found 6
}
Line indented incorrectly; expected 8 spaces, found 4
}
 
Spaces must be used to indent lines; tabs are not allowed
if (null === $matching_subscription && !empty($subscriptions)) {
Spaces must be used to indent lines; tabs are not allowed
$matching_subscription = array_pop($subscriptions);
Spaces must be used to indent lines; tabs are not allowed
}
 
Spaces must be used to indent lines; tabs are not allowed
return $matching_subscription;
Spaces must be used to indent lines; tabs are not allowed
Closing brace indented incorrectly; expected 2 spaces, found 4
}
 
/**
* Get the subscription item that matches the order item.
*
* @since 1.0.0
* @param WC_Subscription $subscription The WC_Subscription object
* @param WC_Order_Item_Product $order_item The order item
Line exceeds 120 characters; contains 249 characters
* @param string $match_type Optional. The type of comparison to make. Can be 'match_product_ids' to compare product|variation IDs or 'match_attributes' to also compare by item attributes on top of matching product IDs. Default 'match_attributes'.
*
* @return WC_Order_Item_Product|bool
*/
Function `get_matching_subscription_item` has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
Line indented incorrectly; expected 4 spaces, found 2
public static function get_matching_subscription_item($subscription, $order_item, $match_type = 'match_attributes')
Line indented incorrectly; expected at least 4 spaces, found 2
{
Spaces must be used to indent lines; tabs are not allowed
$matching_item = false;
 
Line indented incorrectly; expected 8 spaces, found 4
if ('match_attributes' === $match_type) {
Line indented incorrectly; expected at least 12 spaces, found 6
$order_item_attributes = wp_list_pluck($order_item->get_formatted_meta_data('_', true), 'value', 'key');
Line indented incorrectly; expected 8 spaces, found 4
}
 
Line indented incorrectly; expected at least 8 spaces, found 4
$order_item_canonical_product_id = wcs_get_canonical_product_id($order_item);
 
Line indented incorrectly; expected 8 spaces, found 4
foreach ($subscription->get_items() as $subscription_item) {
Line indented incorrectly; expected 12 spaces, found 6
if (wcs_get_canonical_product_id($subscription_item) !== $order_item_canonical_product_id) {
Line indented incorrectly; expected at least 16 spaces, found 8
continue;
Line indented incorrectly; expected 12 spaces, found 6
}
 
// Check if we have matching meta key and value pairs loosely - they can appear in any order,
Line exceeds 120 characters; contains 162 characters
Line indented incorrectly; expected 12 spaces, found 6
if ('match_attributes' === $match_type && wp_list_pluck($subscription_item->get_formatted_meta_data('_', true), 'value', 'key') != $order_item_attributes) {
Line indented incorrectly; expected at least 16 spaces, found 8
continue;
Line indented incorrectly; expected 12 spaces, found 6
}
 
Line indented incorrectly; expected at least 12 spaces, found 6
$matching_item = $subscription_item;
Line indented incorrectly; expected at least 12 spaces, found 6
break;
Line indented incorrectly; expected 8 spaces, found 4
}
 
Spaces must be used to indent lines; tabs are not allowed
return $matching_item;
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* Sanitize user input to prevent XSS atacks.
*
* @since 1.0.0
* @param string $value. String to be sanitized.
*
* @return string
*/
Line indented incorrectly; expected 4 spaces, found 2
Opening brace should be on a new line
public static function sanitize_xss($value) {
Line indented incorrectly; expected at least 8 spaces, found 4
return htmlspecialchars(strip_tags($value));
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* Sort arrays by keys maintains index association.
Whitespace found at end of line
*
* @since 1.0.1
* @param array $arr. Array to order.
* @param string $on. String key to filter.
* @param defined $order. Order by ASC or DESC
Whitespace found at end of line
*
* @return array
*/
Function `array_sort` has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
Method `array_sort` has 27 lines of code (exceeds 25 allowed). Consider refactoring.
Avoid variables with short names like $on. Configured minimum length is 3.
Incorrect spacing between argument "$order" and equals sign; expected 1 but found 0
Incorrect spacing between default value and equals sign for argument "$order"; expected 1 but found 0
Line indented incorrectly; expected 4 spaces, found 2
public static function array_sort($array, $on, $order=SORT_ASC)
Line indented incorrectly; expected at least 4 spaces, found 2
{
Line indented incorrectly; expected at least 8 spaces, found 6
$new_array = array();
Line indented incorrectly; expected at least 8 spaces, found 6
$sortable_array = array();
Line indented incorrectly; expected 8 spaces, found 6
if (count($array) > 0) {
Line indented incorrectly; expected 12 spaces, found 10
foreach ($array as $k => $v) {
Line indented incorrectly; expected 16 spaces, found 14
if (is_array($v)) {
Line indented incorrectly; expected 20 spaces, found 18
foreach ($v as $k2 => $v2) {
Line indented incorrectly; expected 24 spaces, found 22
if ($k2 == $on) {
Line indented incorrectly; expected at least 28 spaces, found 26
$sortable_array[$k] = $v2;
Line indented incorrectly; expected 24 spaces, found 22
}
Line indented incorrectly; expected 20 spaces, found 18
}
The method array_sort uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
Line indented incorrectly; expected 16 spaces, found 14
} else {
Line indented incorrectly; expected at least 20 spaces, found 18
$sortable_array[$k] = $v;
Line indented incorrectly; expected 16 spaces, found 14
}
Line indented incorrectly; expected 12 spaces, found 10
}
Line indented incorrectly; expected 12 spaces, found 10
switch ($order) {
Line indented incorrectly; expected 16 spaces, found 14
case SORT_ASC:
Line indented incorrectly; expected at least 20 spaces, found 18
asort($sortable_array);
Terminating statement must be indented to the same level as the CASE body
break;
Line indented incorrectly; expected 16 spaces, found 14
case SORT_DESC:
Line indented incorrectly; expected at least 20 spaces, found 18
arsort($sortable_array);
Terminating statement must be indented to the same level as the CASE body
break;
Line indented incorrectly; expected 12 spaces, found 10
}
Line indented incorrectly; expected 12 spaces, found 10
foreach ($sortable_array as $k => $v) {
Line indented incorrectly; expected at least 16 spaces, found 14
$new_array[$k] = $array[$k];
Line indented incorrectly; expected 12 spaces, found 10
}
Line indented incorrectly; expected 8 spaces, found 6
}
Line indented incorrectly; expected at least 8 spaces, found 6
return $new_array;
Line indented incorrectly; expected 4 spaces, found 2
}
Expected 1 blank line at end of file; 3 found
}