Completed
Push — master ( fbe049...cd378b )
by Ori
07:11
created

NumberField::validateCastValue()   C

Complexity

Conditions 15
Paths 51

Size

Total Lines 35
Code Lines 23

Duplication

Lines 10
Ratio 28.57 %

Importance

Changes 0
Metric Value
cc 15
eloc 23
nc 51
nop 1
dl 10
loc 35
rs 5.0504
c 0
b 0
f 0

How to fix   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 frictionlessdata\tableschema\Fields;
4
5
class NumberField extends BaseField
6
{
7
    /**
8
     * @param mixed $val
9
     *
10
     * @return float
11
     *
12
     * @throws \frictionlessdata\tableschema\Exceptions\FieldValidationException;
13
     */
14
    protected function validateCastValue($val)
15
    {
16
        $isPercent = false;
17
        if (is_string($val)) {
18
            if (substr($val, -1) == '%') {
19
                $val = rtrim($val, '%');
20
                $isPercent = true;
21
            }
22
            if (isset($this->descriptor()->groupChar)) {
23
                $val = str_replace($this->descriptor()->groupChar, '', $val);
24
            }
25
            if (isset($this->descriptor()->decimalChar) && $this->descriptor()->decimalChar != '.') {
26
                $val = str_replace($this->descriptor()->decimalChar, '.', $val);
27
            }
28
            if (isset($this->descriptor()->currency) && $this->descriptor()->currency) {
29
                $newval = '';
30
                foreach (str_split($val) as $chr) {
31
                    if (is_numeric($chr) || $chr == '.' || $chr == '+' || $chr == '-') {
32
                        $newval .= $chr;
33
                    }
34
                }
35
                $val = $newval;
36
            }
37
        }
38 View Code Duplication
        if (!is_numeric($val)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
            throw $this->getValidationException('value must be numeric', $val);
40
        } else {
41
            $val = (float) $val;
42
            if ($isPercent) {
43
                $val = $val / 100;
44
            }
45
46
            return $val;
47
        }
48
    }
49
50
    public static function type()
51
    {
52
        return 'number';
53
    }
54
}
55