Passed
Pull Request — master (#219)
by
unknown
02:29
created

Boolean::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 8
dl 0
loc 22
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Yiisoft\Validator\FormatterInterface;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\Rule;
11
use Yiisoft\Validator\ValidationContext;
12
13
/**
14
 * Checks if the value is a boolean value or a value corresponding to it.
15
 */
16
#[Attribute(Attribute::TARGET_PROPERTY)]
17
final class Boolean extends Rule
18
{
19 5
    public function __construct(
20
        /**
21
         * @var mixed the value representing true status.
22
         */
23
        private mixed $trueValue = '1',
24
        /**
25
         * @var mixed the value representing false status.
26
         */
27
        private mixed $falseValue = '0',
28
        /**
29
         * @var bool whether the comparison to {@see $trueValue} and {@see $falseValue} is strict.
30
         * When this is `true`, the value and type must both match those of {@see $trueValue} or
31
         * {@see $falseValue}. Defaults to `false`, meaning only the value needs to be matched.
32
         */
33
        private bool $strict = false,
34
        private string $message = 'The value must be either "{true}" or "{false}".',
35
        ?FormatterInterface $formatter = null,
36
        bool $skipOnEmpty = false,
37
        bool $skipOnError = false,
38
        $when = null
39
    ) {
40 5
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
41
    }
42
43
    /**
44
     * @see $trueValue
45
     */
46 1
    public function trueValue(mixed $value): self
47
    {
48 1
        $new = clone $this;
49 1
        $new->trueValue = $value;
50
51 1
        return $new;
52
    }
53
54
    /**
55
     * @see $falseValue
56
     */
57 1
    public function falseValue(mixed $value): self
58
    {
59 1
        $new = clone $this;
60 1
        $new->falseValue = $value;
61
62 1
        return $new;
63
    }
64
65
    /**
66
     * @see $strict
67
     */
68 1
    public function strict(bool $value): self
69
    {
70 1
        $new = clone $this;
71 1
        $new->strict = $value;
72
73 1
        return $new;
74
    }
75
76 20
    protected function validateValue($value, ?ValidationContext $context = null): Result
77
    {
78 20
        if ($this->strict) {
79 8
            $valid = $value === $this->trueValue || $value === $this->falseValue;
80
        } else {
81 12
            $valid = $value == $this->trueValue || $value == $this->falseValue;
82
        }
83
84 20
        $result = new Result();
85
86 20
        if ($valid) {
87 13
            return $result;
88
        }
89
90 7
        $message = $this->getFormattedMessage();
91 7
        $result->addError($message);
92
93 7
        return $result;
94
    }
95
96 17
    private function getFormattedMessage(): string
97
    {
98 17
        return $this->formatMessage($this->message, [
99 17
            'true' => $this->trueValue === true ? 'true' : $this->trueValue,
100 17
            'false' => $this->falseValue === false ? 'false' : $this->falseValue,
101
        ]);
102
    }
103
104 10
    public function getOptions(): array
105
    {
106 10
        return array_merge(parent::getOptions(), [
107 10
            'trueValue' => $this->trueValue,
108 10
            'falseValue' => $this->falseValue,
109 10
            'strict' => $this->strict,
110 10
            'message' => $this->getFormattedMessage(),
111
        ]);
112
    }
113
}
114