StringBetweenLengthValidatorTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 47
rs 10
c 0
b 0
f 0
ccs 0
cts 15
cp 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B mustHaveLengthBetween() 0 34 6
1
<?php
2
3
namespace AmmitPhp\Ammit\UI\Resolver\Validator\Implementation\Pragmatic;
4
5
use AmmitPhp\Ammit\Domain\StringValidation;
6
use AmmitPhp\Ammit\UI\Resolver\Validator\InvalidArgumentException;
7
use AmmitPhp\Ammit\UI\Resolver\UIValidationEngine;
8
use AmmitPhp\Ammit\UI\Resolver\Validator\UIValidatorInterface;
9
10
trait StringBetweenLengthValidatorTrait
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 String ?
19
     *
20
     * @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...
21
     */
22
    public function mustHaveLengthBetween($value, int $min, int $max, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
23
    {
24
        $this->validationEngine->validateFieldValue(
25
            $parentValidator ?: $this,
26
            function() use ($value, $min, $max, $propertyPath, $exceptionMessage) {
27
                $stringValidation = new StringValidation();
28
                if ($stringValidation->isStringBetweenValid($value, $min, $max)) {
29
                    return;
30
                }
31
32
                if (null === $exceptionMessage) {
33
                    $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...
34
                        'Value "%s" must have between %d and %d chars.',
35
                        $value,
36
                        $min,
37
                        $max
38
                    );
39
                }
40
41
                throw new InvalidArgumentException(
42
                    $exceptionMessage,
43
                    0,
44
                    $propertyPath,
45
                    $value
46
                );
47
            }
48
        );
49
50
        if (null === $value || !is_string($value)) {
51
            return '';
52
        }
53
54
        return (string) $value;
55
    }
56
}
57