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

StringField::validateCastValue()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 20
nc 7
nop 1
dl 0
loc 28
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
namespace frictionlessdata\tableschema\Fields;
4
5
class StringField extends BaseField
6
{
7
    public function inferProperties($val, $lenient = false)
8
    {
9
        parent::inferProperties($val, $lenient);
10
        if (!$lenient) {
11
            if (is_string($val) && strpos($val, '@') !== false) {
12
                $this->descriptor->format = 'email';
13
            }
14
        }
15
    }
16
17
    /**
18
     * @param mixed $val
19
     *
20
     * @return string
21
     *
22
     * @throws \frictionlessdata\tableschema\Exceptions\FieldValidationException;
23
     */
24
    protected function validateCastValue($val)
25
    {
26
        try {
27
            $val = (string) $val;
28
        } catch (\Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) { ... = json_encode($val); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
29
            $val = json_encode($val);
30
        }
31
        switch ($this->format()) {
32
            case 'email':
33
                if (strpos($val, '@') === false) {
34
                    throw $this->getValidationException('value is not a valid email', $val);
35
                }
36
                break;
37
            case 'uri':
38
                if (filter_var($val, FILTER_VALIDATE_URL) === false) {
39
                    throw $this->getValidationException(null, $val);
40
                }
41
                break;
42
            case 'binary':
43
                $decoded = base64_decode($val, true);
44
                if ($decoded === false) {
45
                    throw $this->getValidationException(null, $val);
46
                }
47
                break;
48
        }
49
50
        return $val;
51
    }
52
53
    public static function type()
54
    {
55
        return 'string';
56
    }
57
58
    public function getInferIdentifier($lenient = false)
59
    {
60
        $inferId = parent::getInferIdentifier();
61
        $format = $this->format();
62
        if (!$lenient && !empty($format)) {
63
            $inferId .= ':'.$this->format();
64
        }
65
66
        return $inferId;
67
    }
68
69
    protected function checkMinimumConstraint($val, $minConstraint)
70
    {
71
        throw $this->getValidationException('minimum constraint is not supported for string fields', $val);
72
    }
73
74
    protected function checkMaximumConstraint($val, $maxConstraint)
75
    {
76
        throw $this->getValidationException('maximum constraint is not supported for string fields', $val);
77
    }
78
}
79