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

BooleanValidatorTrait::mustBeBoolean()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 9
cts 9
cp 1
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 12
nc 2
nop 4
crap 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