Completed
Push — master ( b6d955...929034 )
by Marcus
02:03
created

Boolean::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Mbright\Validation\Rule\Sanitize;
4
5
use Mbright\Validation\Rule\AbstractBooleanCase;
6
7
class Boolean extends AbstractBooleanCase implements SanitizeRuleInterface
8
{
9
    /** @var bool|mixed */
10
    protected $trueValue;
11
12
    /** @var bool|mixed */
13
    protected $falseValue;
14
15
    /**
16
     * @param bool $true Use this value for `true`.
17
     * @param bool $false Use this value for `false`.
18
     */
19 114
    public function __construct($true = true, $false = false)
20
    {
21 114
        $this->trueValue = $true;
22 114
        $this->falseValue = $false;
23 114
    }
24
25
    /**
26
     * Sanitize the value to a boolean, or a pseudo-boolean.
27
     *
28
     * @param object $subject The subject to be filtered.
29
     * @param string $field The subject field name.
30
     * @param mixed $true Use this value for `true`.
0 ignored issues
show
Bug introduced by
There is no parameter named $true. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
31
     * @param mixed $false Use this value for `false`.
0 ignored issues
show
Bug introduced by
There is no parameter named $false. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
32
     *
33
     * @return bool Always true.
34
     */
35 108
    public function __invoke($subject, string $field): bool
36
    {
37 108
        $value = $subject->$field;
38
39 108
        if ($this->isTrue($value)) {
40 54
            $subject->$field = $this->trueValue;
41
42 54
            return true;
43
        }
44
45 54
        if ($this->isFalse($value)) {
46 48
            $subject->$field = $this->falseValue;
47
48 48
            return true;
49
        }
50
51 6
        $subject->$field = $value ? $this->trueValue : $this->falseValue;
52
53 6
        return true;
54
    }
55
}
56