PragmaticRawValueValidator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 62.26 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 57.14%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 66
loc 106
rs 10
c 0
b 0
f 0
ccs 20
cts 35
cp 0.5714

3 Methods

Rating   Name   Duplication   Size   Complexity  
B mustBeStringNotEmpty() 27 27 4
B mustBeEmailAddress() 19 28 5
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 AmmitPhp\Ammit\UI\Resolver\Validator;
5
6
use AmmitPhp\Ammit\UI\Resolver\Validator\Implementation\Pragmatic\StringBetweenLengthValidatorTrait;
7
use AmmitPhp\Ammit\Domain\MailMxValidation;
8
use AmmitPhp\Ammit\UI\Resolver\Validator\Implementation\Pragmatic\InArrayValidatorTrait;
9
use AmmitPhp\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
class PragmaticRawValueValidator extends RawValueValidator
17
{
18
    use UuidValidatorTrait;
19
    use InArrayValidatorTrait;
20
    use StringBetweenLengthValidatorTrait;
21
22
    /**
23
     * Domain should be responsible for string emptiness
24
     * Exceptions are caught in order to be processed later
25
     * @param mixed $value String not empty ?
26
     *
27
     * @return mixed Untouched value
28
     */
29 View Code Duplication
    public function mustBeStringNotEmpty($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...
30
    {
31 1
        $this->validationEngine->validateFieldValue(
32 1
            $parentValidator ?: $this,
33 1
            function() use ($value, $propertyPath, $exceptionMessage) {
34 1
                if (!empty($value)) {
35 1
                    return;
36
                }
37
38 1
                if (null === $exceptionMessage) {
39
                    $exceptionMessage = sprintf(
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $exceptionMessage, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
40
                        'Value "%s" is empty.',
41
                        $value
42
                    );
43
                }
44
45 1
                throw new InvalidArgumentException(
46
                    $exceptionMessage,
47 1
                    0,
48
                    $propertyPath,
49
                    $value
50
                );
51 1
            }
52
        );
53
54 1
        return $value;
55
    }
56
57
    /**
58
     * Domain should be responsible for email format
59
     * Exceptions are caught in order to be processed later
60
     * @param mixed $value Email ?
61
     *
62
     * @return mixed Untouched value
63
     */
64
    public function mustBeEmailAddress($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
65
    {
66
        $mailMxValidation = new MailMxValidation();
67
        $this->validationEngine->validateFieldValue(
68
            $parentValidator ?: $this,
69 View Code Duplication
            function() use ($value, $propertyPath, $exceptionMessage, $mailMxValidation) {
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...
70
                if ($mailMxValidation->isEmailFormatValid($value) && $mailMxValidation->isEmailHostValid($value)) {
71
                    return;
72
                }
73
74
                if (null === $exceptionMessage) {
75
                    $exceptionMessage = sprintf(
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $exceptionMessage, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
76
                        'Mail "%s" is not valid.',
77
                        $value
78
                    );
79
                }
80
81
                throw new InvalidArgumentException(
82
                    $exceptionMessage,
83
                    0,
84
                    $propertyPath,
85
                    $value
86
                );
87
            }
88
        );
89
90
        return $value;
91
    }
92
93
    /**
94
     * Domain should be responsible for regex validation
95
     * Exceptions are caught in order to be processed later
96
     * @param mixed  $value   Valid against Regex ?
97
     * @param string $pattern Regex
98
     *
99
     * @return mixed Untouched value
100
     */
101 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...
102
    {
103 1
        $this->validationEngine->validateFieldValue(
104 1
            $parentValidator ?: $this,
105 1
            function() use ($value, $pattern, $propertyPath, $exceptionMessage) {
106 1
                if (preg_match($pattern, $value)) {
107 1
                    return;
108
                }
109
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 1
            }
117
        );
118
119 1
        return $value;
120
    }
121
}
122