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 AmmitPhp\Ammit\UI\Resolver\Validator\Implementation\Pragmatic;
4
5
use AmmitPhp\Ammit\UI\Resolver\Validator\InvalidArgumentException;
6
use AmmitPhp\Ammit\Domain\UuidValidation;
7
use AmmitPhp\Ammit\UI\Resolver\UIValidationEngine;
8
use AmmitPhp\Ammit\UI\Resolver\Validator\UIValidatorInterface;
9
10
trait UuidValidatorTrait
11
{
12
    /** @var UIValidationEngine */
13
    protected $validationEngine;
14
15
    /**
16
     * Domain should be responsible for id format
17
     * Exceptions are caught in order to be processed later
18
     * @param mixed $value UUID ?
19
     *
20
     * @return string Casted to string
21
     */
22
    public function mustBeUuid($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null): string
23
    {
24 1
        if (null === $value || !is_string($value)) {
25 1
            $value = '';
26
        }
27
28 1
        $this->validationEngine->validateFieldValue(
29 1
            $parentValidator ?: $this,
30 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...
31 1
                $uuidValidation = new UuidValidation();
32 1
                if ($uuidValidation->isUuidValid($value)) {
33 1
                    return;
34
                }
35
36 1
                if (null === $exceptionMessage) {
37
                    $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...
38
                        'Value "%s" is not a valid UUID.',
39
                        $value
40
                    );
41
                }
42
43 1
                throw new InvalidArgumentException(
44
                    $exceptionMessage,
45 1
                    0,
46
                    $propertyPath,
47
                    $value
48
                );
49 1
            }
50
        );
51
52 1
        return (string) $value;
53
    }
54
}
55