Completed
Push — master ( 73f405...9e0eff )
by Guillaume
02:38
created

BooleanValidatorTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 30
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A mustBeBoolean() 0 21 3
1
<?php
2
3
namespace Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure;
4
5
use Assert\Assertion;
6
use Imedia\Ammit\UI\Resolver\Validator\UIValidatorInterface;
7
8
/**
9
 * @author Guillaume MOREL <[email protected]>
10
 */
11
trait BooleanValidatorTrait
12
{
13
    /**
14
     * Exceptions are caught in order to be processed later
15
     * @param mixed $value Boolean ?
16
     *
17
     * @return boolean Value casted into boolean or false
18
     */
19
    public function mustBeBoolean($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null): bool
20
    {
21 1
        $this->validationEngine->validateFieldValue(
0 ignored issues
show
Bug introduced by
The property validationEngine does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22 1
            $parentValidator ?: $this,
23 1
            function() use ($value, $propertyPath, $exceptionMessage) {
24 1
                Assertion::inArray(
25
                    $value,
26 1
                    [true, false, 1, 0, '1', '0', 'true', 'false'],
27
                    $exceptionMessage,
28
                    $propertyPath
29
                );
30 1
            }
31
        );
32
33
        // Otherwise "false" would return true
34 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...
35 1
            return true;
36
        }
37
38 1
        return false;
39
    }
40
}
41