Completed
Push — master ( b7bf63...a45e2b )
by Guillaume
05:21 queued 02:53
created

StringValidatorTrait::mustBeString()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 31
Code Lines 18

Duplication

Lines 31
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 6.105

Importance

Changes 0
Metric Value
dl 31
loc 31
ccs 12
cts 14
cp 0.8571
rs 8.439
c 0
b 0
f 0
cc 6
eloc 18
nc 2
nop 4
crap 6.105
1
<?php
2
3
namespace Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure;
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 View Code Duplication
trait StringValidatorTrait
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...
13
{
14
    /** @var UIValidationEngine */
15
    protected $validationEngine;
16
17
    /**
18
     * Exceptions are caught in order to be processed later
19
     * @param mixed $value String ?
20
     *
21
     * @return string Casted to string
22
     */
23
    public function mustBeString($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null): string
24
    {
25 1
        $this->validationEngine->validateFieldValue(
26 1
            $parentValidator ?: $this,
27 1
            function() use ($value, $propertyPath, $exceptionMessage) {
28 1
                if (is_string($value)) {
29 1
                    return;
30
                }
31
32 1
                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
                        'The value "%s" is not a string.',
35
                        $value
36
                    );
37
                }
38
39 1
                throw new InvalidArgumentException(
40
                    $exceptionMessage,
41 1
                    0,
42
                    $propertyPath,
43
                    $value
44
                );
45 1
            }
46
        );
47
48 1
        if (null === $value || !is_string($value)) {
49 1
            return '';
50
        }
51
52 1
        return (string) $value;
53
    }
54
}
55