Completed
Pull Request — master (#51)
by Guillaume
02:18
created

BooleanValidatorTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 44.44 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 20
loc 45
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B mustBeBoolean() 20 33 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure;
4
5
use Assert\InvalidArgumentException;
6
use Imedia\Ammit\Domain\BooleanValidation;
7
use Imedia\Ammit\UI\Resolver\UIValidationEngine;
8
use Imedia\Ammit\UI\Resolver\Validator\UIValidatorInterface;
9
10
/**
11
 * @author Guillaume MOREL <[email protected]>
12
 */
13
trait BooleanValidatorTrait
14
{
15
    /** @var UIValidationEngine */
16
    protected $validationEngine;
17
18
    /**
19
     * Exceptions are caught in order to be processed later
20
     * @param mixed $value Boolean ?
21
     *
22
     * @return boolean Value casted into boolean or false
23
     */
24
    public function mustBeBoolean($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null): bool
25
    {
26 1
        $this->validationEngine->validateFieldValue(
27 1
            $parentValidator ?: $this,
28 1 View Code Duplication
            function() use ($value, $propertyPath, $exceptionMessage) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
29 1
                $booleanValidation = new BooleanValidation();
30 1
                if ($booleanValidation->isBooleanValid($value)) {
31 1
                    return;
32
                }
33
34 1
                if (null === $exceptionMessage) {
35
                    $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...
36
                        'Value "%s" is not a boolean.',
37
                        $value
38
                    );
39
                }
40
41 1
                throw new InvalidArgumentException(
42
                    $exceptionMessage,
43 1
                    0,
44
                    $propertyPath,
45
                    $value
46
                );
47 1
            }
48
        );
49
50
        // Otherwise "false" would return true
51 1
        if (in_array($value, [true, 'true', '1', 1], true)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return in_array($value, ...'true', '1', 1), true);.
Loading history...
52 1
            return true;
53
        }
54
55 1
        return false;
56
    }
57
}
58