DurationField   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 40
Duplicated Lines 27.5 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 11
loc 40
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateCastValue() 11 14 3
A type() 0 4 1
B checkAllowedValues() 0 17 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace frictionlessdata\tableschema\Fields;
4
5
use Carbon\CarbonInterval;
6
7
class DurationField extends BaseField
8
{
9
    protected function validateCastValue($val)
10
    {
11 View Code Duplication
        if (!is_string($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...
12
            throw $this->getValidationException('must be string', $val);
13
        } else {
14
            $val = trim($val);
15
            try {
16
                // we create a dateInterval first, because it's more restrictive
17
                return CarbonInterval::instance(new \DateInterval($val));
18
            } catch (\Exception $e) {
19
                throw $this->getValidationException($e->getMessage(), $val);
20
            }
21
        }
22
    }
23
24
    public static function type()
25
    {
26
        return 'duration';
27
    }
28
29
    protected function checkAllowedValues($allowedValues, $val)
30
    {
31
        foreach ($allowedValues as $allowedValue) {
32
            if (
33
                $val->years == $allowedValue->years
34
                && $val->months == $allowedValue->months
35
                && $val->days == $allowedValue->days
36
                && $val->hours == $allowedValue->hours
37
                && $val->minutes == $allowedValue->minutes
38
                && $val->seconds == $allowedValue->seconds
39
            ) {
40
                return true;
41
            }
42
        }
43
44
        return false;
45
    }
46
}
47