在另一个函数中分离重复的代码(代码优化)

时间:2017-06-09 07:16:23

标签: php

我有这个功能,它正在计算产品税率,它必须考虑不同的税收规则......问题是这个代码是有效的,但现在我不想进行代码优化,因为一部分正在重复。如果您仔细检查,您会看到foreach ($taxationRules as $taxRule) {重复两次,但必须应用不同的规则(添加或删除税率)

如果可能的话,你们可以帮我解决如何正确分离重复代码的问题。

这里还有一件事很重要!!所有税收规则必须首先添加税,然后(在另一个foreach中)取消税,因为没有税收规则优先权。这就是我复制代码的原因,而不只是在第一个foreach

中包含$taxRule['TaxationRule']['Taxation_rule_disabled_tax_rate']
private static function getProductTaxRatesByTaxationRules($cartItem) {
    $shippingAddressData = self::$cart['ShippingAddress']['UserAddress'];
    $taxationRules = self::$cart['TaxationRules'];
    $currentUser = LoggedInUser::get();

    // Prepare data to compare with taxation rules
    $regularUser = false;
    if (!$currentUser['is_company']) {
        $regularUser = true;
    }
    $compareData = [
        'regular_user' => $regularUser,
        'company' => $currentUser['is_company'],
        'product_id' => $cartItem['product_id'],
        'subtype_id' => $cartItem['productIndex']['subtype_id'],
        'country_id' => $shippingAddressData['country_id'],
        'country_state_id' => $shippingAddressData['country_state_id']
    ];

    $taxRates = $cartItem['productIndex']['tax_rate'];

    foreach ($taxationRules as $taxRule) {

        if ($taxRule['TaxationRule']['user_type'] == 'regular' && !$compareData['regular_user']) {
            continue;
        }
        if ($taxRule['TaxationRule']['user_type'] == 'companies' && !$compareData['company']) {
            continue;
        }
        if (!empty($taxRule['TaxationRule']['Taxation_rule_product'])) {
            if (!in_array($compareData['product_id'], $taxRule['TaxationRule']['Taxation_rule_product'])) {
                continue;
            }
        }
        if (!empty($taxRule['TaxationRule']['Taxation_rule_product_subtype'])) {
            if (!in_array($compareData['subtype_id'], $taxRule['TaxationRule']['Taxation_rule_product_subtype'])) {
                continue;
            }
        }
        if (!empty($taxRule['TaxationRule']['TaxationRuleCountryState'])) {
            $found = false;
            foreach ($taxRule['TaxationRule']['TaxationRuleCountryState'] as $countryStates) {
                if (isset($countryStates['country_state_id']) && !is_null($countryStates['country_state_id'])) {
                    if ($countryStates['country_state_id'] == $compareData['country_state_id']) {
                        $found = true;
                    }
                } else {
                    if ($countryStates['country_id'] == $compareData['country_id']) {
                        $found = true;
                    }
                }
            }
            if (!$found) {
                continue;
            }
        }
        // If any of flags aren't true, then add Taxation_rule_enabled_tax_rate and
        // check if there are enabled tax rates that needs to be added
        if (!empty($taxRule['TaxationRule']['Taxation_rule_enabled_tax_rate'])) {
            foreach ($taxRule['TaxationRule']['Taxation_rule_enabled_tax_rate'] as $includeTax) {
                foreach ($taxRule['TaxRate'] as $taxData) {
                    if ($taxData['id'] == $includeTax) {
                        $taxRates[] = array(
                            'id' => $taxData['id'],
                            'value' => $taxData['taxrate']
                        );
                    }
                }
            }
        }
    }

    foreach ($taxationRules as $taxRule) {
        if ($taxRule['TaxationRule']['user_type'] == 'regular' && !$compareData['regular_user']) {
            continue;
        }
        if ($taxRule['TaxationRule']['user_type'] == 'companies' && !$compareData['company']) {
            continue;
        }
        if (!empty($taxRule['TaxationRule']['Taxation_rule_product'])) {
            if (!in_array($compareData['product_id'], $taxRule['TaxationRule']['Taxation_rule_product'])) {
                continue;
            }
        }
        if (!empty($taxRule['TaxationRule']['Taxation_rule_product_subtype'])) {
            if (!in_array($compareData['subtype_id'], $taxRule['TaxationRule']['Taxation_rule_product_subtype'])) {
                continue;
            }
        }
        if (!empty($taxRule['TaxationRule']['TaxationRuleCountryState'])) {
            $found = false;
            foreach ($taxRule['TaxationRule']['TaxationRuleCountryState'] as $countryStates) {
                if (isset($countryStates['country_state_id']) && !is_null($countryStates['country_state_id'])) {
                    if ($countryStates['country_state_id'] == $compareData['country_state_id']) {
                        $found = true;
                    }
                } else {
                    if ($countryStates['country_id'] == $compareData['country_id']) {
                        $found = true;
                    }
                }
            }
            if (!$found) {
                continue;
            }
        }

        // If any of flags aren't true, then remove Taxation_rule_disabled_tax_rate
        // Check if there are disabled tax rates that needed to be removed
        if (!empty($taxRule['TaxationRule']['Taxation_rule_disabled_tax_rate'])) {
            foreach ($taxRule['TaxationRule']['Taxation_rule_disabled_tax_rate'] as $excludeTaxId) {
                foreach ($taxRates as $key => $value) {
                    if ($value['id'] == $excludeTaxId) {
                        unset($taxRates[$key]);
                        break;
                    }
                }
            }
        }
    }

    return $taxRates;
}

如果您需要任何其他信息,请告诉我,我会提供。谢谢!

3 个答案:

答案 0 :(得分:1)

对于像foreach这样的小比较部分:

foreach ($taxRule['TaxationRule']['Taxation_rule_product'] as $taxProductId) {
        if ($compareData['product_id'] == $taxProductId) {
            $found = true;
        }
}

您可以使用in_array功能,可以在in_array function

找到更多信息

答案 1 :(得分:1)

return START_STICKY

答案 2 :(得分:1)

    private static function getProductTaxRatesByTaxationRules($cartItem) {
        $shippingAddressData = self::$cart['ShippingAddress']['UserAddress'];
        $taxationRules = self::$cart['TaxationRules'];
        $currentUser = LoggedInUser::get();

        // Prepare data to compare with taxation rules
        $regularUser = false;
        if (!$currentUser['is_company']) {
            $regularUser = true;
        }
        $compareData = [
            'regular_user' => $regularUser,
            'company' => $currentUser['is_company'],
            'product_id' => $cartItem['product_id'],
            'subtype_id' => $cartItem['productIndex']['subtype_id'],
            'country_id' => $shippingAddressData['country_id'],
            'country_state_id' => $shippingAddressData['country_state_id']
        ];

        $taxRates = $cartItem['productIndex']['tax_rate'];

        foreach ($taxationRules as $taxRule) {
            $result = validateData($taxRule, $compareData);

            // If any of flags aren't true, then remove Taxation_rule_disabled_tax_rate
            // Check if there are disabled tax rates that needed to be removed
            if ($result && !empty($taxRule['TaxationRule']['Taxation_rule_disabled_tax_rate'])) {
                foreach ($taxRule['TaxationRule']['Taxation_rule_disabled_tax_rate'] as $excludeTaxId) {
                    foreach ($taxRates as $key => $value) {
                        if ($value['id'] == $excludeTaxId) {
                            unset($taxRates[$key]);
                            break;
                        }
                    }
                }
            }

            // If any of flags aren't true, then add Taxation_rule_enabled_tax_rate and
            // check if there are enabled tax rates that needs to be added
            if ($result && !empty($taxRule['TaxationRule']['Taxation_rule_enabled_tax_rate'])) {
                foreach ($taxRule['TaxationRule']['Taxation_rule_enabled_tax_rate'] as $includeTax) {
                    foreach ($taxRule['TaxRate'] as $taxData) {
                        if ($taxData['id'] == $includeTax) {
                            $taxRates[] = array(
                                'id' => $taxData['id'],
                                'value' => $taxData['taxrate']
                            );
                        }
                    }
                }
            }
        }

        return $taxRates;
    }

    private function validateData($taxRule, $compareData) {
        $result = false;
        if ($taxRule['TaxationRule']['user_type'] == 'regular' && !$compareData['regular_user']) {
            $result = true;
        }
        if ($taxRule['TaxationRule']['user_type'] == 'companies' && !$compareData['company']) {
            $result = true;
        }
        if (!empty($taxRule['TaxationRule']['Taxation_rule_product'])) {
            if (!in_array($compareData['product_id'], $taxRule['TaxationRule']['Taxation_rule_product'])) {
                $result = true;
            }
        }
        if (!empty($taxRule['TaxationRule']['Taxation_rule_product_subtype'])) {
            if (!in_array($compareData['subtype_id'], $taxRule['TaxationRule']['Taxation_rule_product_subtype'])) {
                $result = true;
            }
        }
        if (!empty($taxRule['TaxationRule']['TaxationRuleCountryState'])) {
            foreach ($taxRule['TaxationRule']['TaxationRuleCountryState'] as $countryStates) {
                if (isset($countryStates['country_state_id']) && !is_null($countryStates['country_state_id'])) {
                    if ($countryStates['country_state_id'] == $compareData['country_state_id']) {
                        $result = true;
                    }
                } else {
                    if ($countryStates['country_id'] == $compareData['country_id']) {
                        $result = true;
                    }
                }
            }
        }
        return $result;
    }