Passed
Pull Request — master (#219)
by
unknown
02:30
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 6
    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 6
        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
    /**
77
     * @see $message
78
     */
79 1
    public function message(string $value): self
80
    {
81 1
        $new = clone $this;
82 1
        $new->message = $value;
83
84 1
        return $new;
85
    }
86
87 20
    protected function validateValue($value, ?ValidationContext $context = null): Result
88
    {
89 20
        if ($this->strict) {
90 8
            $valid = $value === $this->trueValue || $value === $this->falseValue;
91
        } else {
92 12
            $valid = $value == $this->trueValue || $value == $this->falseValue;
93
        }
94
95 20
        $result = new Result();
96
97 20
        if ($valid) {
98 13
            return $result;
99
        }
100
101 7
        $message = $this->getFormattedMessage();
102 7
        $result->addError($message);
103
104 7
        return $result;
105
    }
106
107 18
    private function getFormattedMessage(): string
108
    {
109 18
        return $this->formatMessage($this->message, [
110 18
            'true' => $this->trueValue === true ? 'true' : $this->trueValue,
111 18
            'false' => $this->falseValue === false ? 'false' : $this->falseValue,
112
        ]);
113
    }
114
115 11
    public function getOptions(): array
116
    {
117 11
        return array_merge(parent::getOptions(), [
118 11
            'trueValue' => $this->trueValue,
119 11
            'falseValue' => $this->falseValue,
120 11
            'strict' => $this->strict,
121 11
            'message' => $this->getFormattedMessage(),
122
        ]);
123
    }
124
}
125