FormatTrait   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 98
Duplicated Lines 18.37 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 18
loc 98
rs 10
c 1
b 0
f 1
wmc 19
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateDateTime() 0 10 2
addError() 0 1 ?
C validateFormat() 18 61 17

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 namespace CMPayments\SchemaValidator\Validators;
2
3
use CMPayments\SchemaValidator\BaseValidator;
4
use CMPayments\SchemaValidator\Exceptions\ValidateException;
5
6
/**
7
 * Class FormatTrait
8
 *
9
 * @package CMPayments\SchemaValidator\Validators
10
 * @Author  Boy Wijnmaalen <[email protected]>
11
 */
12
trait FormatTrait
13
{
14
    /**
15
     * Validates $data against a specific $schema->format
16
     *
17
     * @param $data
18
     * @param $schema
19
     * @param string $path
20
     */
21
    public function validateFormat($data, $schema, $path)
22
    {
23
        if (!isset($schema->format)) {
24
            return;
25
        }
26
27
        switch ($schema->format) {
28
29
            // DATE
30 View Code Duplication
            case BaseValidator::DATE:
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...
31
                if (!$date = $this->validateDateTime($data, 'Y-m-d')) {
32
33
                    $this->addError(ValidateException::ERROR_USER_FORMAT_INVALID_DATE, [$data, $path]);
34
                }
35
                break;
36
37
            // DATETIME
38
            case BaseValidator::DATETIME:
39
                if (!$this->validateDateTime($data, 'Y-m-d\TH:i:s\Z')
40
                    && !$this->validateDateTime($data, 'Y-m-d\TH:i:s.u\Z')
41
                    && !$this->validateDateTime($data, \DateTime::RFC3339)
42
                    && !$this->validateDateTime($data, \DateTime::ISO8601)
43
                ) {
44
                    $this->addError(ValidateException::ERROR_USER_FORMAT_INVALID_DATETIME, [$data, $path]);
45
                }
46
                break;
47
48
            // EMAIL
49
            case BaseValidator::EMAIL:
50
                if (is_null(filter_var($data, FILTER_VALIDATE_EMAIL, FILTER_NULL_ON_FAILURE))) {
51
52
                    $this->addError(ValidateException::ERROR_USER_FORMAT_INVALID_EMAIL, [$data, $path]);
53
                }
54
                break;
55
56
            // TIME
57 View Code Duplication
            case BaseValidator::TIME:
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...
58
                if (!$this->validateDateTime($data, 'H:i:s')) {
59
60
                    $this->addError(ValidateException::ERROR_USER_FORMAT_INVALID_TIME, [$data, $path]);
61
                }
62
                break;
63
64
            // UTC_SECONDS (in epoch seconds)
65 View Code Duplication
            case BaseValidator::UTC_SECONDS:
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...
66
                if (!$this->validateDateTime((string)$data, 'U')) { // U = Seconds since the Unix Epoch
67
68
                    $this->addError(ValidateException::ERROR_USER_FORMAT_INVALID_UTC_SECONDS, [$data, $path]);
69
                }
70
                break;
71
72
            // URL
73
            case BaseValidator::URL:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
74
75
                if (filter_var($data, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED) === false) {
76
77
                    $this->addError(ValidateException::ERROR_USER_FORMAT_INVALID_URL, [$data, $path]);
78
                }
79
                break;
80
        }
81
    }
82
83
    /**
84
     * Validate a $datetime string and match it againt a $format
85
     *
86
     * @param $datetime
87
     * @param string $format
88
     *
89
     * @return bool
90
     */
91
    protected function validateDateTime($datetime, $format)
92
    {
93
        $dt = \DateTime::createFromFormat($format, $datetime);
94
95
        if (!$dt) {
96
            return false;
97
        }
98
99
        return $datetime === $dt->format($format);
100
    }
101
102
    /**
103
     * @param int   $code
104
     * @param array $args
105
     *
106
     * @return mixed
107
     */
108
    abstract public function addError($code, array $args = []);
109
}
110