Completed
Pull Request — master (#49)
by Guillaume
03:20 queued 47s
created

PragmaticRawValueValidator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 19.05 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 57.69%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 20
loc 105
ccs 15
cts 26
cp 0.5769
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A mustHaveLengthBetween() 0 17 2
A mustBeStringNotEmpty() 0 15 2
A mustBeEmailAddress() 0 16 3
A mustBeValidAgainstRegex() 20 20 4

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\MailMxValidation;
9
use Imedia\Ammit\UI\Resolver\Validator\Implementation\Pragmatic\UuidValidatorTrait;
10
11
/**
12
 * @internal Contains Domain Validation assertions (but class won't be removed in next version)
13
 *   Domain Validation should be done in Domain
14
 *   Should be used for prototyping project knowing you are accumulating technical debt
15
 *
16
 * @author Guillaume MOREL <[email protected]>
17
 */
18
class PragmaticRawValueValidator extends RawValueValidator
19
{
20
    use UuidValidatorTrait;
21
22
    /**
23
     * Domain should be responsible for id format
24
     * Exceptions are caught in order to be processed later
25
     * @param mixed $value String ?
26
     *
27
     * @return mixed Untouched value
28
     */
29
    public function mustHaveLengthBetween($value, int $min, int $max, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
30
    {
31
        $this->validationEngine->validateFieldValue(
32
            $parentValidator ?: $this,
33
            function() use ($value, $min, $max, $propertyPath, $exceptionMessage) {
34
                Assertion::betweenLength(
35
                    $value,
36
                    $min,
37
                    $max,
38
                    $exceptionMessage,
39
                    $propertyPath
40
                );
41
            }
42
        );
43
44
        return $value;
45
    }
46
47
    /**
48
     * Domain should be responsible for string emptiness
49
     * Exceptions are caught in order to be processed later
50
     * @param mixed $value String not empty ?
51
     *
52
     * @return mixed Untouched value
53
     */
54
    public function mustBeStringNotEmpty($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
55
    {
56 1
        $this->validationEngine->validateFieldValue(
57 1
            $parentValidator ?: $this,
58 1
            function() use ($value, $propertyPath, $exceptionMessage) {
59 1
                Assertion::notEmpty(
60
                    $value,
61
                    $exceptionMessage,
62
                    $propertyPath
63
                );
64 1
            }
65
        );
66
67 1
        return $value;
68
    }
69
70
    /**
71
     * Domain should be responsible for email format
72
     * Exceptions are caught in order to be processed later
73
     * @param mixed $value Email ?
74
     *
75
     * @return mixed Untouched value
76
     */
77
    public function mustBeEmailAddress($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
78
    {
79
        $mailMxValidation = new MailMxValidation();
80
        $this->validationEngine->validateFieldValue(
81
            $parentValidator ?: $this,
82
            function() use ($value, $propertyPath, $exceptionMessage, $mailMxValidation) {
83
                Assertion::true(
84
                    $mailMxValidation->isEmailFormatValid($value) && $mailMxValidation->isEmailHostValid($value),
85
                    $exceptionMessage,
86
                    $propertyPath
87
                );
88
            }
89
        );
90
91
        return $value;
92
    }
93
94
    /**
95
     * Domain should be responsible for regex validation
96
     * Exceptions are caught in order to be processed later
97
     * @param mixed  $value   Valid against Regex ?
98
     * @param string $pattern Regex
99
     *
100
     * @return mixed Untouched value
101
     */
102 View Code Duplication
    public function mustBeValidAgainstRegex($value, string $pattern, 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...
103
    {
104 1
        $this->validationEngine->validateFieldValue(
105 1
            $parentValidator ?: $this,
106 1
            function() use ($value, $pattern, $propertyPath, $exceptionMessage) {
107 1
                if (preg_match($pattern, $value)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
108
109
                } else {
110 1
                    throw new InvalidArgumentException(
111 1
                        $exceptionMessage ?: sprintf('Value "%s" not valid against regex "%s".', $value, $pattern),
112 1
                        0,
113
                        $propertyPath,
114
                        $value
115
                    );
116
                }
117 1
            }
118
        );
119
120 1
        return $value;
121
    }
122
}
123