Completed
Push — master ( f77682...c45988 )
by Guillaume
02:29
created

DateValidatorTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 81
Duplicated Lines 24.69 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 20
loc 81
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C mustBeDate() 10 28 7
C mustBeDateTime() 10 30 7
A createDefaultDateTime() 0 6 1

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 Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure;
4
5
use Assert\InvalidArgumentException;
6
use Imedia\Ammit\Domain\DateValidation;
7
use Imedia\Ammit\UI\Resolver\Validator\UIValidatorInterface;
8
9
/**
10
 * @author Guillaume MOREL <[email protected]>
11
 */
12
trait DateValidatorTrait
13
{
14
    /**
15
     * Exceptions are caught in order to be processed later
16
     * @param mixed $value Date Y-m-d ?
17
     *
18
     * @return \DateTime
19
     */
20
    public function mustBeDate($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
21
    {
22 1
        $dateValidation = new DateValidation();
23 1
        $this->validationEngine->validateFieldValue(
0 ignored issues
show
Bug introduced by
The property validationEngine does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24 1
            $parentValidator ?: $this,
25 1 View Code Duplication
            function() use ($value, $propertyPath, $exceptionMessage, $dateValidation) {
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...
26 1
                if (null === $value || ! $dateValidation->isDateValid($value)) {
27 1
                    throw new InvalidArgumentException(
28 1
                        $exceptionMessage ?: sprintf('Date "%s" format invalid, must be Y-m-d.', $value),
29 1
                        0,
30
                        $propertyPath,
31
                        $value
32
                    );
33
                }
34 1
            }
35
        );
36
37 1
        if (null === $value) {
38 1
            return $this->createDefaultDateTime(); // Invalid
39
        }
40
41 1
        $date = $dateValidation->createDateFromString($value);
42 1
        if ($date instanceof \DateTime) {
43 1
            return $date->setTime(0, 0, 0); // Valid
44
        }
45
46 1
        return $this->createDefaultDateTime(); // Invalid
47
    }
48
49
    /**
50
     * Exceptions are caught in order to be processed later
51
     * @param mixed $value Date Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 ?
52
     *
53
     * @return \DateTime|false
54
     */
55
    public function mustBeDateTime($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
56
    {
57 1
        $dateValidation = new DateValidation();
58
59 1
        $this->validationEngine->validateFieldValue(
60 1
            $parentValidator ?: $this,
61 1 View Code Duplication
            function() use ($value, $propertyPath, $exceptionMessage, $dateValidation) {
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...
62 1
                if (null === $value || ! $dateValidation->isDateTimeValid($value)) {
63 1
                    throw new InvalidArgumentException(
64 1
                        $exceptionMessage ?: sprintf('Datetime "%s" format invalid, must be Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00.', $value),
65 1
                        0,
66
                        $propertyPath,
67
                        $value
68
                    );
69
                }
70 1
            }
71
        );
72
73 1
        if (null === $value) {
74 1
            return $this->createDefaultDateTime(); // Invalid
75
        }
76
77 1
        $date = $dateValidation->createDateTimeFromString($value);
78
79 1
        if ($date instanceof \DateTime) {
80 1
            return $date; // Valid
81
        }
82
83 1
        return $this->createDefaultDateTime();  // Invalid
84
    }
85
86
    private function createDefaultDateTime(): \DateTime
87
    {
88 1
        $date = new \DateTime();
89
90 1
        return $date->setTime(0, 0, 0);
91
    }
92
}
93