mustHaveLengthBetween()   B
last analyzed

Complexity

Conditions 6
Paths 2

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.439
c 0
b 0
f 0
ccs 0
cts 7
cp 0
cc 6
eloc 21
nc 2
nop 6
crap 42
1
<?php
2
3
namespace Imedia\Ammit\UI\Resolver\Validator\Implementation\Pragmatic;
4
5
use Imedia\Ammit\Domain\StringValidation;
6
use Imedia\Ammit\UI\Resolver\Validator\InvalidArgumentException;
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 StringBetweenLengthValidatorTrait
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 String ?
22
     *
23
     * @return mixed Untouched value
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
24
     */
25
    public function mustHaveLengthBetween($value, int $min, int $max, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
26
    {
27
        $this->validationEngine->validateFieldValue(
28
            $parentValidator ?: $this,
29
            function() use ($value, $min, $max, $propertyPath, $exceptionMessage) {
30
                $stringValidation = new StringValidation();
31
                if ($stringValidation->isStringBetweenValid($value, $min, $max)) {
32
                    return;
33
                }
34
35
                if (null === $exceptionMessage) {
36
                    $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...
37
                        'Value "%s" must have between %d and %d chars.',
38
                        $value,
39
                        $min,
40
                        $max
41
                    );
42
                }
43
44
                throw new InvalidArgumentException(
45
                    $exceptionMessage,
46
                    0,
47
                    $propertyPath,
48
                    $value
49
                );
50
            }
51
        );
52
53
        if (null === $value || !is_string($value)) {
54
            return '';
55
        }
56
57
        return (string) $value;
58
    }
59
}
60