FloatValidatorTrait::mustBeFloat()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.0702

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 14
cts 16
cp 0.875
rs 8.439
c 0
b 0
f 0
cc 6
eloc 20
nc 4
nop 4
crap 6.0702
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 FloatValidatorTrait
13
{
14
    /** @var UIValidationEngine */
15
    protected $validationEngine;
16
17
    /**
18
     * Exceptions are caught in order to be processed later
19
     * @param mixed $value Float ?
20
     *
21
     * @return float Value casted into float or -1
22
     */
23
    public function mustBeFloat($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null): float
24
    {
25 1
        if (is_numeric($value)) {
26 1
            $value = (float) $value;
27
        }
28
29 1
        $this->validationEngine->validateFieldValue(
30 1
            $parentValidator ?: $this,
31 1
            function() use ($value, $propertyPath, $exceptionMessage) {
32 1
                if (is_float($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 float.',
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
        if (!is_float($value)) {
53 1
            return -1;
54
        }
55
56 1
        return $value;
57
    }
58
}
59