如何用更少的代码行编写以下开关案例?

时间:2019-01-29 15:23:42

标签: php

如何用更少的行编写以下开关案例代码?它占用了大量的空间和线条,但是我不确定写这个的更好的方法是什么。 这是我要根据各种距离范围计算在WordPress后端中设置的价格的功能。 如果距离小于300英里,则在后端为各种距离范围设置固定的费率;如果距离大于300英里,则我要使用每英里的价格。该代码完全满足我的需要,但是我觉得可以使用更紧凑的方式编写此代码?

谢谢!

以下是代码:

function calculatePrice($distance_value_complete, $fixed_rate, $variable_rate) {

    if (floatval($distance_value_complete) < 300 ):

        switch (true) {

            case floatval($distance_value_complete) < 2:
                $vehicle_price1 = $fixed_rate['upto_2_mile'];
                break;

            case ( (floatval($distance_value_complete) >= 2) && (floatval($distance_value_complete) < 4) ):
                $vehicle_price1 = $fixed_rate['from_2_to_4_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 4) && (floatval($distance_value_complete) < 9) ):
                $vehicle_price1 = $fixed_rate['from_4_to_9_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 9) && (floatval($distance_value_complete) < 14) ):
                $vehicle_price1 = $fixed_rate['from_9_to_14_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 14) && (floatval($distance_value_complete) < 19) ):
                $vehicle_price1 = $fixed_rate['from_14_to_19_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 19) && (floatval($distance_value_complete) < 25) ):
            $vehicle_price1 = $fixed_rate['from_19_to_25_miles'];
            break;

            case ( (floatval($distance_value_complete) >= 25) && (floatval($distance_value_complete) < 29) ):
                $vehicle_price1 = $fixed_rate['from_25_to_29_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 29) && (floatval($distance_value_complete) < 35) ):
                $vehicle_price1 = $fixed_rate['from_29_to_35_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 35) && (floatval($distance_value_complete) < 40) ):
                $vehicle_price1 = $fixed_rate['from_35_to_40_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 40) && (floatval($distance_value_complete) < 45) ):
                $vehicle_price1 = $fixed_rate['from_40_to_45_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 45) && (floatval($distance_value_complete) < 50) ):
                $vehicle_price1 = $fixed_rate['from_45_to_50_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 50) && (floatval($distance_value_complete) < 56) ):
                $vehicle_price1 = $fixed_rate['from_50_to_56_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 56) && (floatval($distance_value_complete) < 62) ):
                $vehicle_price1 = $fixed_rate['from_56_to_62_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 62) && (floatval($distance_value_complete) < 70) ):
                $vehicle_price1 = $fixed_rate['from_62_to_70_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 70) && (floatval($distance_value_complete) < 80) ):
                $vehicle_price1 = $fixed_rate['from_70_to_80_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 80) && (floatval($distance_value_complete) < 90) ):
                $vehicle_price1 = $fixed_rate['from_80_to_90_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 90) && (floatval($distance_value_complete) < 100) ):
                $vehicle_price1 = $fixed_rate['from_90_to_100_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 100) && (floatval($distance_value_complete) < 110) ):
                $vehicle_price1 = $fixed_rate['from_100_to_110_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 110) && (floatval($distance_value_complete) < 120) ):
                $vehicle_price1 = $fixed_rate['from_110_to_120_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 120) && (floatval($distance_value_complete) < 130) ):
                $vehicle_price1 = $fixed_rate['from_120_to_130_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 130) && (floatval($distance_value_complete) < 140) ):
                $vehicle_price1 = $fixed_rate['from_130_to_140_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 140) && (floatval($distance_value_complete) < 150) ):
                $vehicle_price1 = $fixed_rate['from_140_to_150_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 150) && (floatval($distance_value_complete) < 160) ):
                $vehicle_price1 = $fixed_rate['from_150_to_160_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 160) && (floatval($distance_value_complete) < 170) ):
                $vehicle_price1 = $fixed_rate['from_160_to_170_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 170) && (floatval($distance_value_complete) < 180) ):
                $vehicle_price1 = $fixed_rate['from_170_to_180_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 180) && (floatval($distance_value_complete) < 190) ):
                $vehicle_price1 = $fixed_rate['from_180_to_190_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 190) && (floatval($distance_value_complete) < 200) ):
                $vehicle_price1 = $fixed_rate['from_190_to_200_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 200) && (floatval($distance_value_complete) < 210) ):
                $vehicle_price1 = $fixed_rate['from_200_to_210_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 210) && (floatval($distance_value_complete) < 220) ):
                $vehicle_price1 = $fixed_rate['from_210_to_220_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 220) && (floatval($distance_value_complete) < 230) ):
                $vehicle_price1 = $fixed_rate['from_220_to_230_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 230) && (floatval($distance_value_complete) < 240) ):
                $vehicle_price1 = $fixed_rate['from_230_to_240_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 240) && (floatval($distance_value_complete) < 250) ):
                $vehicle_price1 = $fixed_rate['from_240_to_250_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 250) && (floatval($distance_value_complete) < 260) ):
                $vehicle_price1 = $fixed_rate['from_250_to_260_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 260) && (floatval($distance_value_complete) < 270) ):
                $vehicle_price1 = $fixed_rate['from_260_to_270_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 270) && (floatval($distance_value_complete) < 280) ):
                $vehicle_price1 = $fixed_rate['from_270_to_280_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 280) && (floatval($distance_value_complete) < 290) ):
                $vehicle_price1 = $fixed_rate['from_280_to_290_miles'];
                break;

            case ( (floatval($distance_value_complete) >= 290) && (floatval($distance_value_complete) < 300) ):
                $vehicle_price1 = $fixed_rate['from_290_to_300_miles'];
                break;
        }

    else:

        switch (true) {

            case floatval($distance_value_complete) >= 300:
                $vehicle_price1 = floatval($distance_value_complete) * $variable_rate['from_300_and_above'];
                break;
        }

    endif;

    return $vehicle_price1;
}

2 个答案:

答案 0 :(得分:1)

在这种情况下,您必须处理复杂的比较表达式,而只是不使用x

switch

测试结果:

<?php

function calculatePrice($distance_value_complete, $fixed_rate, $variable_rate)
{
    $distance_completed = floatval($distance_value_complete);

    static $minimum_distance = 2;
    static $maximum_distance = 300;

    if ($distance_completed >= $maximum_distance) {
        return $distance_completed * $variable_rate['from_' . $maximum_distance . '_and_above'];;
    }


    if ($distance_completed < $minimum_distance) {
        return $fixed_rate['upto_' . $minimum_distance . '_mile'];
    }

    static $fixed_distances = array(
        4, 9, 14, 19, 25, 29, 35, 40, 45, 50, 56, 62, 70, 80, 90, 100, 
        110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 
        210, 220, 230, 240, 250, 260, 270, 280, 290, 300
    );

    $last_distance = $minimum_distance;
    foreach ($fixed_distances as $distance) {
        if ($distance_completed < $distance) {
            return $fixed_rate['from_' . $last_distance . '_to_' . $distance . '_miles'];
        }
        $last_distance = $distance;
    }

    // This should never happen, unless you modify $fixed_distances and forget to change the early return conditions
    throw new \Exception(sprintf('Distance %f out of range', $distance_completed));
}

答案 1 :(得分:0)

因为您有一个取决于单个int值的费率名称,所以我将创建一个映射所有可用费率的数组,然后将其传递给具有您要检查的值的函数,诸如此类(映射不完整,php7 .2必需)

$conditionMap = [
    [
        'value' => 'upto_2_mile',
        'condition' => '<2'
    ],
    [
        'value' => 'from_2_to_4_miles',
        'condition' => '<4'
    ],
    [
        'value' => 'from_4_to_9_miles',
        'condition' => '<9'
    ],
    [
        'value' => 'from_9_to_14_miles',
        'condition' => '<14'
    ],
    [
        'value' => 'from_14_to_19_miles',
        'condition' => '<19'
    ],
    [
        'value' => 'from_19_to_25_miles',
        'condition' => '<25'
    ],
    [
        'value' => 'from_25_to_29_miles',
        'condition' => '<29'
    ]
];

function getRate(int $value, array $conditionMap) : string {
    $rate = 'Not found';
    foreach ($conditionMap as $condition) {
        $phpString = sprintf("return %s %s;", $value, $condition['condition']);
        $evaluation = eval($phpString);
        if($evaluation) {
            $rate = $condition['value'];
            break;
        }
    }

    return $rate;
}

// TEST
for($i = 0; $i<=30; $i++) {
    var_dump(sprintf("With value %s: %s", $i, getRate($i, $conditionMap)));
}

如果运行此脚本,则应在下面获取结果

string(25) "With value 0: upto_2_mile"
string(25) "With value 1: upto_2_mile"
string(31) "With value 2: from_2_to_4_miles"
string(31) "With value 3: from_2_to_4_miles"
string(31) "With value 4: from_4_to_9_miles"
string(31) "With value 5: from_4_to_9_miles"
string(31) "With value 6: from_4_to_9_miles"
string(31) "With value 7: from_4_to_9_miles"
string(31) "With value 8: from_4_to_9_miles"
string(32) "With value 9: from_9_to_14_miles"
string(33) "With value 10: from_9_to_14_miles"
string(33) "With value 11: from_9_to_14_miles"
string(33) "With value 12: from_9_to_14_miles"
string(33) "With value 13: from_9_to_14_miles"
string(34) "With value 14: from_14_to_19_miles"
string(34) "With value 15: from_14_to_19_miles"
string(34) "With value 16: from_14_to_19_miles"
string(34) "With value 17: from_14_to_19_miles"
string(34) "With value 18: from_14_to_19_miles"
string(34) "With value 19: from_19_to_25_miles"
string(34) "With value 20: from_19_to_25_miles"
string(34) "With value 21: from_19_to_25_miles"
string(34) "With value 22: from_19_to_25_miles"
string(34) "With value 23: from_19_to_25_miles"
string(34) "With value 24: from_19_to_25_miles"
string(34) "With value 25: from_25_to_29_miles"
string(34) "With value 26: from_25_to_29_miles"
string(34) "With value 27: from_25_to_29_miles"
string(34) "With value 28: from_25_to_29_miles"
string(24) "With value 29: Not found"
string(24) "With value 30: Not found"