Failed Conditions
Pull Request — master (#4240)
by Owen
15:30
created

DataValidator::numericOperator()   F

Complexity

Conditions 19
Paths 462

Size

Total Lines 57
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 19

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 57
ccs 38
cts 38
cp 1
rs 1.0972
c 0
b 0
f 0
cc 19
nc 462
nop 3
crap 19

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Cell;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
7
use PhpOffice\PhpSpreadsheet\Exception;
8
9
/**
10
 * Validate a cell value according to its validation rules.
11
 */
12
class DataValidator
13
{
14
    /**
15
     * Does this cell contain valid value?
16
     *
17
     * @param Cell $cell Cell to check the value
18
     */
19 8
    public function isValid(Cell $cell): bool
20
    {
21 8
        if (!$cell->hasDataValidation() || $cell->getDataValidation()->getType() === DataValidation::TYPE_NONE) {
22 2
            return true;
23
        }
24
25 7
        $cellValue = $cell->getValue();
26 7
        $dataValidation = $cell->getDataValidation();
27
28 7
        if (!$dataValidation->getAllowBlank() && ($cellValue === null || $cellValue === '')) {
29 1
            return false;
30
        }
31
32 7
        $returnValue = false;
33 7
        $type = $dataValidation->getType();
34 7
        if ($type === DataValidation::TYPE_LIST) {
35 5
            $returnValue = $this->isValueInList($cell);
36 5
        } elseif ($type === DataValidation::TYPE_WHOLE) {
37 4
            if (!is_numeric($cellValue) || fmod((float) $cellValue, 1) != 0) {
38 3
                $returnValue = false;
39
            } else {
40 4
                $returnValue = $this->numericOperator($dataValidation, (int) $cellValue, $cell);
41
            }
42 4
        } elseif ($type === DataValidation::TYPE_DECIMAL || $type === DataValidation::TYPE_DATE || $type === DataValidation::TYPE_TIME) {
43 3
            if (!is_numeric($cellValue)) {
44 3
                $returnValue = false;
45
            } else {
46 3
                $returnValue = $this->numericOperator($dataValidation, (float) $cellValue, $cell);
47
            }
48 4
        } elseif ($type === DataValidation::TYPE_TEXTLENGTH) {
49 3
            $returnValue = $this->numericOperator($dataValidation, mb_strlen($cell->getValueString()), $cell);
50
        }
51
52 7
        return $returnValue;
53
    }
54
55 4
    private function numericOperator(DataValidation $dataValidation, int|float $cellValue, Cell $cell): bool
56
    {
57 4
        $calculation = null;
58 4
        $operator = $dataValidation->getOperator();
59 4
        $formula1 = $dataValidation->getFormula1();
60 4
        if (!is_numeric($formula1)) {
61 4
            $calculation = Calculation::getInstance($cell->getWorksheet()->getParent());
62 4
63 3
            try {
64 3
                $result = $calculation
65 3
                    ->calculateFormula("=$formula1", $cell->getCoordinate(), $cell);
66 3
                while (is_array($result)) {
67 3
                    $result = array_pop($result);
68 3
                }
69 3
                $formula1 = $result;
70 3
            } catch (Exception) {
71 3
                // do nothing
72 3
            }
73 3
        }
74 3
        $formula2 = 0;
75 3
        if ($operator === DataValidation::OPERATOR_BETWEEN || $operator === DataValidation::OPERATOR_NOTBETWEEN) {
76 3
            $formula2 = $dataValidation->getFormula2();
77
            if (!is_numeric($formula2)) {
78
                $calculation ??= Calculation::getInstance($cell->getWorksheet()->getParent());
79 4
80
                try {
81
                    $result = $calculation
82
                        ->calculateFormula("=$formula2", $cell->getCoordinate(), $cell);
83
                    while (is_array($result)) {
84
                        $result = array_pop($result);
85
                    }
86
                    $formula2 = $result;
87 5
                } catch (Exception) {
88
                    // do nothing
89 5
                }
90 5
            }
91
        }
92 5
        $returnValue = false;
93 5
        if ($operator === DataValidation::OPERATOR_BETWEEN) {
94
            $returnValue = $cellValue >= $formula1 && $cellValue <= $formula2;
95 5
        } elseif ($operator === DataValidation::OPERATOR_NOTBETWEEN) {
96 4
            $returnValue = $cellValue < $formula1 || $cellValue > $formula2;
97 5
        } elseif ($operator === DataValidation::OPERATOR_EQUAL) {
98
            $returnValue = $cellValue == $formula1;
99 5
        } elseif ($operator === DataValidation::OPERATOR_NOTEQUAL) {
100 5
            $returnValue = $cellValue != $formula1;
101
        } elseif ($operator === DataValidation::OPERATOR_LESSTHAN) {
102
            $returnValue = $cellValue < $formula1;
103 5
        } elseif ($operator === DataValidation::OPERATOR_LESSTHANOREQUAL) {
104 5
            $returnValue = $cellValue <= $formula1;
105 5
        } elseif ($operator === DataValidation::OPERATOR_GREATERTHAN) {
106
            $returnValue = $cellValue > $formula1;
107
        } elseif ($operator === DataValidation::OPERATOR_GREATERTHANOREQUAL) {
108 5
            $returnValue = $cellValue >= $formula1;
109 1
        }
110 1
111
        return $returnValue;
112
    }
113
114
    /**
115 1
     * Does this cell contain valid value, based on list?
116
     *
117
     * @param Cell $cell Cell to check the value
118
     */
119
    private function isValueInList(Cell $cell): bool
120
    {
121
        $cellValueString = $cell->getValueString();
122
        $dataValidation = $cell->getDataValidation();
123
124
        $formula1 = $dataValidation->getFormula1();
125
        if (!empty($formula1)) {
126
            // inline values list
127
            if ($formula1[0] === '"') {
128
                return in_array(strtolower($cellValueString), explode(',', strtolower(trim($formula1, '"'))), true);
129
            } elseif (strpos($formula1, ':') > 0) {
130
                // values list cells
131
                $matchFormula = '=MATCH(' . $cell->getCoordinate() . ', ' . $formula1 . ', 0)';
132
                $calculation = Calculation::getInstance($cell->getWorksheet()->getParent());
133
134
                try {
135
                    $result = $calculation->calculateFormula($matchFormula, $cell->getCoordinate(), $cell);
136
                    while (is_array($result)) {
137
                        $result = array_pop($result);
138
                    }
139
140
                    return $result !== ExcelError::NA();
141
                } catch (Exception) {
142
                    return false;
143
                }
144
            }
145
        }
146
147
        return true;
148
    }
149
}
150