Completed
Pull Request — master (#41)
by Guillaume
03:05
created

RawValueValidator   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 181
Duplicated Lines 54.14 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 5
dl 98
loc 181
ccs 54
cts 54
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A mustBeString() 15 15 2
A mustBeBoolean() 0 16 2
A mustBeArray() 15 15 2
A mustBeFloat() 15 15 2
A mustBeInteger() 15 15 2
A mustBeDate() 19 19 4
A mustBeDateTime() 19 19 4
A createUIValidationException() 0 4 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
declare(strict_types = 1);
3
4
namespace Imedia\Ammit\UI\Resolver\Validator;
5
6
use Assert\Assertion;
7
use Assert\InvalidArgumentException;
8
use Imedia\Ammit\Domain\DateValidation;
9
use Imedia\Ammit\UI\Resolver\Exception\UIValidationException;
10
use Imedia\Ammit\UI\Resolver\UIValidationEngine;
11
12
/**
13
 * @author Guillaume MOREL <[email protected]>
14
 */
15
class RawValueValidator implements UIValidatorInterface
16
{
17
    /** @var UIValidationEngine */
18
    protected $validationEngine;
19
20
    public function __construct(UIValidationEngine $validationEngine)
21
    {
22 1
        $this->validationEngine = $validationEngine;
23 1
    }
24
25
    /**
26
     * Exceptions are caught in order to be processed later
27
     * @param mixed $value String ?
28
     *
29
     * @return mixed Untouched value
30
     */
31 View Code Duplication
    public function mustBeString($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
32
    {
33 1
        $this->validationEngine->validateFieldValue(
34 1
            $parentValidator ?: $this,
35 1
            function() use ($value, $propertyPath, $exceptionMessage) {
36 1
                Assertion::string(
37
                    $value,
38
                    $exceptionMessage,
39
                    $propertyPath
40
                );
41 1
            }
42
        );
43
44 1
        return $value;
45
    }
46
47
    /**
48
     * Exceptions are caught in order to be processed later
49
     * @param mixed $value Boolean ?
50
     *
51
     * @return mixed Untouched value
52
     */
53
    public function mustBeBoolean($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
54
    {
55 1
        $this->validationEngine->validateFieldValue(
56 1
            $parentValidator ?: $this,
57 1
            function() use ($value, $propertyPath, $exceptionMessage) {
58 1
                Assertion::inArray(
59
                    $value,
60 1
                    [true, false, 1, 0, '1', '0', 'true', 'false'],
61
                    $exceptionMessage,
62
                    $propertyPath
63
                );
64 1
            }
65
        );
66
67 1
        return $value;
68
    }
69
70
    /**
71
     * Exceptions are caught in order to be processed later
72
     * @param mixed $value Array ?
73
     *
74
     * @return mixed Untouched value
75
     */
76 View Code Duplication
    public function mustBeArray($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
77
    {
78 1
        $this->validationEngine->validateFieldValue(
79 1
            $parentValidator ?: $this,
80 1
            function() use ($value, $propertyPath, $exceptionMessage) {
81 1
                Assertion::isArray(
82
                    $value,
83
                    $exceptionMessage,
84
                    $propertyPath
85
                );
86 1
            }
87
        );
88
89 1
        return $value;
90
    }
91
92
    /**
93
     * Exceptions are caught in order to be processed later
94
     * @param mixed $value Float ?
95
     *
96
     * @return mixed Untouched value
97
     */
98 View Code Duplication
    public function mustBeFloat($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
99
    {
100 1
        $this->validationEngine->validateFieldValue(
101 1
            $parentValidator ?: $this,
102 1
            function() use ($value, $propertyPath, $exceptionMessage) {
103 1
                Assertion::float(
104
                    $value,
105
                    $exceptionMessage,
106
                    $propertyPath
107
                );
108 1
            }
109
        );
110
111 1
        return $value;
112
    }
113
114
    /**
115
     * Exceptions are caught in order to be processed later
116
     * @param mixed $value Integer ?
117
     *
118
     * @return mixed Untouched value
119
     */
120 View Code Duplication
    public function mustBeInteger($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
121
    {
122 1
        $this->validationEngine->validateFieldValue(
123 1
            $parentValidator ?: $this,
124 1
            function() use ($value, $propertyPath, $exceptionMessage) {
125 1
                Assertion::integer(
126
                    $value,
127
                    $exceptionMessage,
128
                    $propertyPath
129
                );
130 1
            }
131
        );
132
133 1
        return $value;
134
    }
135
136
    /**
137
     * Exceptions are caught in order to be processed later
138
     * @param mixed $value Date Y-m-d ?
139
     *
140
     * @return mixed Untouched value
141
     */
142 View Code Duplication
    public function mustBeDate($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
143
    {
144 1
        $this->validationEngine->validateFieldValue(
145 1
            $parentValidator ?: $this,
146 1
            function() use ($value, $propertyPath, $exceptionMessage) {
147 1
                $dateValidation = new DateValidation();
148 1
                if (! $dateValidation->isDateValid($value)) {
149 1
                    throw new InvalidArgumentException(
150 1
                        $exceptionMessage ?: sprintf('Date "%s" format invalid, must be Y-m-d.', $value),
151 1
                        0,
152
                        $propertyPath,
153
                        $value
154
                    );
155
                }
156 1
            }
157
        );
158
159 1
        return $value;
160
    }
161
162
    /**
163
     * Exceptions are caught in order to be processed later
164
     * @param mixed $value Date Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 ?
165
     *
166
     * @return mixed Untouched value
167
     */
168 View Code Duplication
    public function mustBeDateTime($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
169
    {
170 1
        $this->validationEngine->validateFieldValue(
171 1
            $parentValidator ?: $this,
172 1
            function() use ($value, $propertyPath, $exceptionMessage) {
173 1
                $dateValidation = new DateValidation();
174 1
                if (! $dateValidation->isDateTimeValid($value)) {
175 1
                    throw new InvalidArgumentException(
176 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),
177 1
                        0,
178
                        $propertyPath,
179
                        $value
180
                    );
181
                }
182 1
            }
183
        );
184
185 1
        return $value;
186
    }
187
188
    /**
189
     * @inheritdoc
190
     */
191
    public function createUIValidationException(string $message, string $propertyPath = null): UIValidationException
192
    {
193 1
        return UIValidationException::fromRaw($message, $propertyPath);
194
    }
195
}
196