Passed
Pull Request — master (#464)
by Alexander
04:02 queued 01:28
created

Count::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 10
dl 0
loc 22
ccs 5
cts 5
cp 1
crap 1
rs 10

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 Closure;
9
use Countable;
10
use Yiisoft\Validator\LimitInterface;
11
use Yiisoft\Validator\Rule\Trait\LimitTrait;
12
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
13
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
14
use Yiisoft\Validator\Rule\Trait\WhenTrait;
15
use Yiisoft\Validator\RuleWithOptionsInterface;
16
use Yiisoft\Validator\SkipOnEmptyInterface;
17
use Yiisoft\Validator\SkipOnErrorInterface;
18
use Yiisoft\Validator\WhenInterface;
19
20
/**
21
 * Defines validation options to check that the value contains certain number of items.
22
 * Can be applied to arrays or classes implementing {@see Countable} interface.
23
 *
24
 * @see CountHandler
25
 *
26
 * @psalm-import-type WhenType from WhenInterface
27
 */
28
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
29
final class Count implements
30
    RuleWithOptionsInterface,
31
    SkipOnErrorInterface,
32
    WhenInterface,
33
    SkipOnEmptyInterface,
34
    LimitInterface
35
{
36
    use LimitTrait;
37
    use SkipOnEmptyTrait;
38 18
    use SkipOnErrorTrait;
39
    use WhenTrait;
40
41
    /**
42
     * @var object|null Object being validated.
43
     */
44
    private ?object $objectValidated = null;
0 ignored issues
show
introduced by
The private property $objectValidated is not used, and could be removed.
Loading history...
45
46
    /**
47
     * @param int|null $exactly Exact number of items. `null` means no strict comparison. Mutually exclusive with
48
     * {@see $min} and {@see $max}.
49
     * @param int|null $min Minimum number of items. null means no minimum number limit. Can't be combined with
50
     * {@see $exactly}. See {@see $lessThanMinMessage} for the customized message for a value with too few items.
51
     * @param int|null $max Maximum number of items. null means no maximum number limit. Can't be combined with
52
     * {@see $exactly}. See {@see $greaterThanMaxMessage} for the customized message for a value with too many items.
53
     * @param string $incorrectInputMessage Error message used when the value is neither an array nor an object
54
     * implementing {@see \Countable} interface.
55
     *
56
     * You may use the following placeholders in the message:
57
     *
58
     * - `{attribute}`: the translated label of the attribute being validated.
59
     * - `{type}`: the type of the value being validated.
60
     * @param string $lessThanMinMessage Error message used when the number of items is smaller than {@see $min}.
61
     *
62
     * You may use the following placeholders in the message:
63
     *
64
     * - `{attribute}`: the translated label of the attribute being validated.
65
     * - `{min}`: minimum number of items required.
66
     * - `{number}`: actual number of items.
67
     * @param string $greaterThanMaxMessage Error message used when the number of items is greater than {@see $max}.
68
     *
69
     * You may use the following placeholders in the message:
70
     *
71
     * - `{attribute}`: the translated label of the attribute being validated.
72
     * - `{max}`: maximum number of items required.
73
     * - `{number}`: actual number of items.
74
     * @param string $notExactlyMessage Error message used when the number of items does not equal {@see $exactly}.
75
     *
76
     * You may use the following placeholders in the message:
77
     *
78
     * - `{attribute}`: the translated label of the attribute being validated.
79
     * - `{exactly}`: exact number of items required.
80
     * - `{number}`: actual number of items.
81
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty.
82
     * See {@see SkipOnEmptyInterface}.
83
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error.
84
     * See {@see SkipOnErrorInterface}.
85
     * @param Closure|null $when A callable to define a condition for applying the rule.
86
     * See {@see WhenInterface}.
87
     * @psalm-param WhenType $when
88
     */
89
    public function __construct(
90
        int|null $exactly = null,
91 18
        int|null $min = null,
92
        int|null $max = null,
93
        private string $incorrectInputMessage = 'This value must be an array or implement \Countable interface.',
94
        string $lessThanMinMessage = 'This value must contain at least {min, number} {min, plural, one{item} ' .
95
        'other{items}}.',
96
        string $greaterThanMaxMessage = 'This value must contain at most {max, number} {max, plural, one{item} ' .
97
        'other{items}}.',
98
        string $notExactlyMessage = 'This value must contain exactly {exactly, number} {exactly, plural, one{item} ' .
99
        'other{items}}.',
100
        private mixed $skipOnEmpty = null,
101 1
        private bool $skipOnError = false,
102
        private Closure|null $when = null,
103 1
    ) {
104
        $this->initLimitProperties(
105
            $min,
106 5
            $max,
107
            $exactly,
108 5
            $lessThanMinMessage,
109
            $greaterThanMaxMessage,
110
            $notExactlyMessage
111 1
        );
112
    }
113 1
114
    public function getName(): string
115 1
    {
116
        return 'count';
117
    }
118 1
119 1
    /**
120
     * Get error message used when the value is neither an array nor an object implementing {@see \Countable} interface.
121
     *
122
     * @return string Error message.
123 34
     *
124
     * @see $incorrectInputMessage
125 34
     */
126
    public function getIncorrectInputMessage(): string
127
    {
128
        return $this->incorrectInputMessage;
129
    }
130
131
    public function getOptions(): array
132
    {
133
        return array_merge($this->getLimitOptions(), [
134
            'incorrectInputMessage' => [
135
                'template' => $this->getIncorrectInputMessage(),
136
                'parameters' => [],
137
            ],
138
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
139
            'skipOnError' => $this->skipOnError,
140
        ]);
141
    }
142
143
    public function getHandler(): string
144
    {
145
        return CountHandler::class;
146
    }
147
}
148