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

StringValidatorTrait::mustBeStringOrEmpty()   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 StringValidatorTrait
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|null Casted to string or null
22
     */
23
    public function mustBeStringOrEmpty($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
24
    {
25
        if (empty($value)) {
26
            return null;
27
        }
28
29
        return $this->mustBeString($value, $propertyPath, $parentValidator, $exceptionMessage);
30
    }
31
32
    /**
33
     * Exceptions are caught in order to be processed later
34
     * @param mixed $value String ?
35
     *
36
     * @return string Casted to string
37
     */
38
    public function mustBeString($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null): string
39
    {
40 1
        $this->validationEngine->validateFieldValue(
41 1
            $parentValidator ?: $this,
42 1
            function() use ($value, $propertyPath, $exceptionMessage) {
43 1
                if (is_string($value)) {
44 1
                    return;
45
                }
46
47 1
                if (null === $exceptionMessage) {
48
                    $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...
49
                        'Value "%s" is not a string.',
50
                        $value
51
                    );
52
                }
53
54 1
                throw new InvalidArgumentException(
55
                    $exceptionMessage,
56 1
                    0,
57
                    $propertyPath,
58
                    $value
59
                );
60 1
            }
61
        );
62
63 1
        if (null === $value || !is_string($value)) {
64 1
            return '';
65
        }
66
67 1
        return (string) $value;
68
    }
69
}
70