Альтернатива для слишком большого количества переключателей для уменьшения цикломатической сложности? - PullRequest
0 голосов
/ 31 августа 2018

У меня есть функция для проверки типа значения $ допустимо или нет. Мой текущий код - это простой случай переключения, в котором слишком много случаев превышает цикломатическую сложность до 17. Мне нужно добавить больше случаев, а также уменьшить сложность.

 /**
 * Check type of attribute value
 * @param $type
 * @param $value
 * @return bool
 */
public function typeCheck($type, $value)
{
    $this->value = $value;
    switch ($type) {
        case 'string':
            return is_string($value) || is_integer($value) || is_bool($value);
        case 'StringNotNull':
            return is_string($value);
        case 'LongStringNotNull':
            return is_string($value);
        case 'SuperLongStringNotNull':
            return is_string($value);
        case 'FortyStringNotNull':
            return is_string($value) && (strlen($value) < 41);
        case 'integer':
            return is_numeric($value);
        case 'positiveInteger':
            return is_numeric($value) && ($value > 0);
        case 'boolean':
            return is_bool($value) || ($value == 'true' || $value = 'false');
        case 'float':
            return is_numeric($value);
        case 'decimal':
            return is_numeric($value);
        case 'PositiveDimension':
            return is_numeric($value) && ($value > 0);
        case 'Dimension':
            return is_numeric($value) && (strlen($value) < 13);
        case 'Barcode':
            $validator = $this->getBarcodeValidator();
            $validator->setBarcode($value);
            return is_string($value) && (strlen($value) < 17 && $validator->isValid());
        case 'dateTime':
            return true;
        case 'normalizedString':
            $this->value = strip_tags($value);
            return is_string($value);
        default:
            return is_string($value);
    }
}

Есть способ лучше?

Ответы [ 2 ]

0 голосов
/ 31 августа 2018

Вы можете заменить коммутатор структурой данных:

private 

public function typeCheck($type, $value) {

    $typeTestMap = [
        'string' => function($value) { return is_string($value) || is_integer($value) || is_bool($value); },
        'FortyStringNotNull' => function($value) { return is_string($value) && (strlen($value) < 41); },
        ...
    ];

    if (isset($typeTestMap[$type])) {
        return $typeTestMap[$type]($value);
    } else {
        return is_string($value);
    }
}
0 голосов
/ 31 августа 2018

Ну, вы можете сгруппировать те, которые имеют те же функции:

public function typeCheck($type, $value)
{
    $this->value = $value;
    switch ($type) {
        case 'string':
            return is_string($value) || is_integer($value) || is_bool($value);
        case 'FortyStringNotNull':
            return is_string($value) && (strlen($value) < 41);
        case 'positiveInteger':
            return is_numeric($value) && ($value > 0);
        case 'boolean':
            return is_bool($value) || ($value == 'true' || $value = 'false');
        case 'float':
        case 'decimal':
        case 'integer':
            return is_numeric($value);
        case 'PositiveDimension':
            return is_numeric($value) && ($value > 0);
        case 'Dimension':
            return is_numeric($value) && (strlen($value) < 13);
        case 'Barcode':
            $validator = $this->getBarcodeValidator();
            $validator->setBarcode($value);
            return is_string($value) && (strlen($value) < 17 && $validator->isValid());
        case 'dateTime':
            return true;
        case 'normalizedString':
            $this->value = strip_tags($value);
            return is_string($value);
        case 'StringNotNull':
        case 'LongStringNotNull':
        case 'SuperLongStringNotNull':
        default:
            return is_string($value);
    }
}

Это немного уменьшит ваш CRAP индекс. Однако, если вы используете OO, вам, возможно, стоит подумать об использовании шаблона Strategy, посмотрите здесь для получения дополнительной информации https://phpenthusiast.com/blog/strategy-pattern-the-power-of-interface

...