UuidValidatorTrait::mustBeUuid()   B
last analyzed

Complexity

Conditions 6
Paths 2

Size

Total Lines 32
Code Lines 19

Duplication

Lines 20
Ratio 62.5 %

Code Coverage

Tests 13
CRAP Score 6.0852

Importance

Changes 0
Metric Value
dl 20
loc 32
rs 8.439
c 0
b 0
f 0
ccs 13
cts 15
cp 0.8667
cc 6
eloc 19
nc 2
nop 4
crap 6.0852
1
<?php
2
3
namespace Imedia\Ammit\UI\Resolver\Validator\Implementation\Pragmatic;
4
5
use Imedia\Ammit\UI\Resolver\Validator\InvalidArgumentException;
6
use Imedia\Ammit\Domain\UuidValidation;
7
use Imedia\Ammit\UI\Resolver\UIValidationEngine;
8
use Imedia\Ammit\UI\Resolver\Validator\UIValidatorInterface;
9
10
/**
11
 * @author Guillaume MOREL <[email protected]>
12
 */
13
trait UuidValidatorTrait
14
{
15
    /** @var UIValidationEngine */
16
    protected $validationEngine;
17
18
    /**
19
     * Domain should be responsible for id format
20
     * Exceptions are caught in order to be processed later
21
     * @param mixed $value UUID ?
22
     *
23
     * @return string Casted to string
24
     */
25
    public function mustBeUuid($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null): string
26
    {
27 1
        if (null === $value || !is_string($value)) {
28 1
            $value = '';
29
        }
30
31 1
        $this->validationEngine->validateFieldValue(
32 1
            $parentValidator ?: $this,
33 1 View Code Duplication
            function() use ($value, $propertyPath, $exceptionMessage) {
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...
34 1
                $uuidValidation = new UuidValidation();
35 1
                if ($uuidValidation->isUuidValid($value)) {
36 1
                    return;
37
                }
38
39 1
                if (null === $exceptionMessage) {
40
                    $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...
41
                        'Value "%s" is not a valid UUID.',
42
                        $value
43
                    );
44
                }
45
46 1
                throw new InvalidArgumentException(
47
                    $exceptionMessage,
48 1
                    0,
49
                    $propertyPath,
50
                    $value
51
                );
52 1
            }
53
        );
54
55 1
        return (string) $value;
56
    }
57
}
58