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

TimeField   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
C validateCastValue() 0 30 7
A type() 0 4 1
A getNativeTime() 0 9 2
1
<?php
2
3
namespace frictionlessdata\tableschema\Fields;
4
5
use Carbon\Carbon;
6
7
/**
8
 * Class TimeField
9
 * casts to array [hour, minute, second].
10
 */
11
class TimeField extends BaseField
12
{
13
    protected function validateCastValue($val)
14
    {
15
        switch ($this->format()) {
16
            case 'default':
17
                $time = explode(':', $val);
18
                if (count($time) != 3) {
19
                    throw $this->getValidationException(null, $val);
20
                } else {
21
                    list($hour, $minute, $second) = $time;
22
23
                    return $this->getNativeTime($hour, $minute, $second);
24
                }
25
                break;
0 ignored issues
show
Unused Code introduced by
break; 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...
26
            case 'any':
27
                try {
28
                    $dt = Carbon::parse($val);
29
                } catch (\Exception $e) {
30
                    throw $this->getValidationException($e->getMessage(), $val);
31
                }
32
33
                return $this->getNativeTime($dt->hour, $dt->minute, $dt->second);
34
            default:
35
                $date = strptime($val, $this->format());
36
                if ($date === false || $date['unparsed'] != '') {
37
                    throw $this->getValidationException(null, $val);
38
                } else {
39
                    return $this->getNativeTime($date['tm_hour'], $date['tm_min'], $date['tm_sec']);
40
                }
41
        }
42
    }
43
44
    public static function type()
45
    {
46
        return 'time';
47
    }
48
49
    protected function getNativeTime($hour, $minute, $second)
50
    {
51
        $parts = [$hour, $minute, $second];
52
        foreach ($parts as &$part) {
53
            $part = (int) $part;
54
        }
55
56
        return $parts;
57
    }
58
}
59