Completed
Push — master ( c45988...b7bf63 )
by Guillaume
02:28
created

InArrayValidatorTrait::mustBeInArray()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.25

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 9
cts 12
cp 0.75
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 1
nop 5
crap 4.25
1
<?php
2
3
namespace Imedia\Ammit\UI\Resolver\Validator\Implementation\Pragmatic;
4
5
use Assert\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 InArrayValidatorTrait
13
{
14
    /** @var UIValidationEngine */
15
    protected $validationEngine;
16
17
    /**
18
     * Exceptions are caught in order to be processed later
19
     * @param mixed $value Array ?
20
     *
21
     * @return mixed
22
     */
23
    public function mustBeInArray($value, array $availableValues, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
24
    {
25 1
        $this->validationEngine->validateFieldValue(
26 1
            $parentValidator ?: $this,
27 1
            function() use ($value, $availableValues, $propertyPath, $exceptionMessage) {
28 1
                if (!in_array($value, $availableValues, true)) {
29 1
                    if (null === $exceptionMessage) {
30
                        $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...
31
                            'The value "%s" is not valid. Available values are "%s".',
32
                            $value,
33
                            implode('", "', $availableValues)
34
                        );
35
                    }
36
37 1
                    throw new InvalidArgumentException(
38
                        $exceptionMessage,
39 1
                        0,
40
                        $propertyPath,
41
                        $value
42
                    );
43
                }
44 1
            }
45
        );
46
47 1
        return $value;
48
    }
49
}
50