Completed
Push — master ( abd78d...b723d6 )
by Guillaume
26:11 queued 17:30
created

IntegerValidatorTrait::mustBeIntegerOrEmpty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 4
crap 6
1
<?php
2
3
namespace Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure;
4
5
use Imedia\Ammit\UI\Resolver\Validator\InvalidArgumentException;
6
use Imedia\Ammit\UI\Resolver\UIValidationEngine;
7
use Imedia\Ammit\UI\Resolver\Validator\UIValidatorInterface;
8
9
/**
10
 * @author Guillaume MOREL <[email protected]>
11
 */
12
trait IntegerValidatorTrait
13
{
14
    /** @var UIValidationEngine */
15
    protected $validationEngine;
16
17
    /**
18
     * Exceptions are caught in order to be processed later
19
     * @param mixed $value Integer ?
20
     *
21
     * @return int|null Value casted into int or -1 or null
22
     */
23
    public function mustBeIntegerOrEmpty($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
24
    {
25
        if ($value === '') {
26
            return null;
27
        }
28
29
        return $this->mustBeInteger($value, $propertyPath, $parentValidator, $exceptionMessage);
30
    }
31
32
    /**
33
     * Exceptions are caught in order to be processed later
34
     * @param mixed $value Integer ?
35
     *
36
     * @return int Value casted into int or -1
37
     */
38
    public function mustBeInteger($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null): int
39
    {
40 1
        if (is_numeric($value)) {
41 1
            $value = (int) $value;
42
        }
43
44 1
        $this->validationEngine->validateFieldValue(
45 1
            $parentValidator ?: $this,
46 1
            function() use ($value, $propertyPath, $exceptionMessage) {
47 1
                if (is_int($value)) {
48 1
                    return;
49
                }
50
51 1
                if (null === $exceptionMessage) {
52
                    $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...
53
                        'Value "%s" is not an integer.',
54
                        $value
55
                    );
56
                }
57
58 1
                throw new InvalidArgumentException(
59
                    $exceptionMessage,
60 1
                    0,
61
                    $propertyPath,
62
                    $value
63
                );
64 1
            }
65
        );
66
67 1
        if (!is_int($value)) {
68 1
            return -1;
69
        }
70
71 1
        return $value;
72
    }
73
}
74