InArrayValidatorTrait::mustBeInArray()   B
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 28
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 4.1967

Importance

Changes 0
Metric Value
dl 28
loc 28
rs 8.5806
c 0
b 0
f 0
ccs 10
cts 13
cp 0.7692
cc 4
eloc 17
nc 1
nop 5
crap 4.1967
1
<?php
2
3
namespace AmmitPhp\Ammit\UI\Resolver\Validator\Implementation\Pragmatic;
4
5
use AmmitPhp\Ammit\UI\Resolver\Validator\InvalidArgumentException;
6
use AmmitPhp\Ammit\UI\Resolver\UIValidationEngine;
7
use AmmitPhp\Ammit\UI\Resolver\Validator\UIValidatorInterface;
8
9 View Code Duplication
trait InArrayValidatorTrait
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
10
{
11
    /** @var UIValidationEngine */
12
    protected $validationEngine;
13
14
    /**
15
     * Exceptions are caught in order to be processed later
16
     * @param mixed $value Array ?
17
     *
18
     * @return mixed
19
     */
20
    public function mustBeInArray($value, array $availableValues, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
21
    {
22 1
        $this->validationEngine->validateFieldValue(
23 1
            $parentValidator ?: $this,
24 1
            function() use ($value, $availableValues, $propertyPath, $exceptionMessage) {
25 1
                if (in_array($value, $availableValues, true)) {
26 1
                    return;
27
                }
28
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
                        '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 1
            }
44
        );
45
46 1
        return $value;
47
    }
48
}
49