PragmaticRawValueValidator::mustBeStringNotEmpty()   B
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 27
Code Lines 16

Duplication

Lines 27
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 4.074

Importance

Changes 0
Metric Value
dl 27
loc 27
rs 8.5806
c 0
b 0
f 0
ccs 10
cts 12
cp 0.8333
cc 4
eloc 16
nc 1
nop 4
crap 4.074
1
<?php
2
declare(strict_types = 1);
3
4
namespace Imedia\Ammit\UI\Resolver\Validator;
5
6
use Imedia\Ammit\UI\Resolver\Validator\Implementation\Pragmatic\StringBetweenLengthValidatorTrait;
7
use Imedia\Ammit\Domain\MailMxValidation;
8
use Imedia\Ammit\UI\Resolver\Validator\Implementation\Pragmatic\InArrayValidatorTrait;
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
    use InArrayValidatorTrait;
22
    use StringBetweenLengthValidatorTrait;
23
24
    /**
25
     * Domain should be responsible for string emptiness
26
     * Exceptions are caught in order to be processed later
27
     * @param mixed $value String not empty ?
28
     *
29
     * @return mixed Untouched value
30
     */
31 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...
32
    {
33 1
        $this->validationEngine->validateFieldValue(
34 1
            $parentValidator ?: $this,
35 1
            function() use ($value, $propertyPath, $exceptionMessage) {
36 1
                if (!empty($value)) {
37 1
                    return;
38
                }
39
40 1
                if (null === $exceptionMessage) {
41
                    $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...
42
                        'Value "%s" is empty.',
43
                        $value
44
                    );
45
                }
46
47 1
                throw new InvalidArgumentException(
48
                    $exceptionMessage,
49 1
                    0,
50
                    $propertyPath,
51
                    $value
52
                );
53 1
            }
54
        );
55
56 1
        return $value;
57
    }
58
59
    /**
60
     * Domain should be responsible for email format
61
     * Exceptions are caught in order to be processed later
62
     * @param mixed $value Email ?
63
     *
64
     * @return mixed Untouched value
65
     */
66
    public function mustBeEmailAddress($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
67
    {
68
        $mailMxValidation = new MailMxValidation();
69
        $this->validationEngine->validateFieldValue(
70
            $parentValidator ?: $this,
71 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...
72
                if ($mailMxValidation->isEmailFormatValid($value) && $mailMxValidation->isEmailHostValid($value)) {
73
                    return;
74
                }
75
76
                if (null === $exceptionMessage) {
77
                    $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...
78
                        'Mail "%s" is not valid.',
79
                        $value
80
                    );
81
                }
82
83
                throw new InvalidArgumentException(
84
                    $exceptionMessage,
85
                    0,
86
                    $propertyPath,
87
                    $value
88
                );
89
            }
90
        );
91
92
        return $value;
93
    }
94
95
    /**
96
     * Domain should be responsible for regex validation
97
     * Exceptions are caught in order to be processed later
98
     * @param mixed  $value   Valid against Regex ?
99
     * @param string $pattern Regex
100
     *
101
     * @return mixed Untouched value
102
     */
103 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...
104
    {
105 1
        $this->validationEngine->validateFieldValue(
106 1
            $parentValidator ?: $this,
107 1
            function() use ($value, $pattern, $propertyPath, $exceptionMessage) {
108 1
                if (preg_match($pattern, $value)) {
109 1
                    return;
110
                }
111
112 1
                throw new InvalidArgumentException(
113 1
                    $exceptionMessage ?: sprintf('Value "%s" not valid against regex "%s".', $value, $pattern),
114 1
                    0,
115
                    $propertyPath,
116
                    $value
117
                );
118 1
            }
119
        );
120
121 1
        return $value;
122
    }
123
}
124