Passed
Push — master ( c644e1...919d8c )
by Alexander
04:16 queued 01:42
created

Count::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 56
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 JetBrains\PhpStorm\ArrayShape;
11
use Yiisoft\Validator\Rule\Trait\LimitTrait;
12
use Yiisoft\Validator\SerializableRuleInterface;
13
use Yiisoft\Validator\BeforeValidationInterface;
14
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
15
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
16
use Yiisoft\Validator\ValidationContext;
17
18
/**
19
 * Validates that the value contains certain number of items. Can be applied to arrays or classes implementing
20
 * {@see Countable} interface.
21
 */
22
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
23
final class Count implements SerializableRuleInterface, BeforeValidationInterface
24
{
25
    use BeforeValidationTrait;
26
    use LimitTrait;
27
    use RuleNameTrait;
28
29 7
    public function __construct(
30
        /**
31
         * @var int|null minimum number of items. null means no minimum number limit. Can't be combined with
32
         * {@see $exactly}.
33
         *
34
         * @see $lessThanMinMessage for the customized message for a value with too few items.
35
         */
36
        ?int $min = null,
37
        /**
38
         * @var int|null maximum number of items. null means no maximum number limit. Can't be combined with
39
         * {@see $exactly}.
40
         *
41
         * @see $greaterThanMaxMessage for the customized message for a value wuth too many items.
42
         */
43
        ?int $max = null,
44
        /**
45
         * @var int|null exact number of items. `null` means no strict comparison. Mutually exclusive with {@see $min}
46
         * and {@see $max}.
47
         */
48
        ?int $exactly = null,
49
        /**
50
         * @var string user-defined error message used when the value is neither an array nor implementing
51
         * {@see \Countable} interface.
52
         *
53
         * @see Countable
54
         */
55
        private string $message = 'This value must be an array or implement \Countable interface.',
56
        /**
57
         * @var string user-defined error message used when the number of items is smaller than {@see $min}.
58
         */
59
        string $lessThanMinMessage = 'This value must contain at least {min, number} {min, plural, one{item} ' .
60
        'other{items}}.',
61
        /**
62
         * @var string user-defined error message used when the number of items is greater than {@see $max}.
63
         */
64
        string $greaterThanMaxMessage = 'This value must contain at most {max, number} {max, plural, one{item} ' .
65
        'other{items}}.',
66
        /**
67
         * @var string user-defined error message used when the number of items does not equal {@see $exactly}.
68
         */
69
        string $notExactlyMessage = 'This value must contain exactly {exactly, number} {exactly, plural, one{item} ' .
70
        'other{items}}.',
71
        private bool $skipOnEmpty = false,
72
        private bool $skipOnError = false,
73
        /**
74
         * @var Closure(mixed, ValidationContext):bool|null
75
         */
76
        private ?Closure $when = null,
77
    ) {
78 7
        $this->initLimitProperties(
79
            $min,
80
            $max,
81
            $exactly,
82
            $lessThanMinMessage,
83
            $greaterThanMaxMessage,
84
            $notExactlyMessage
85
        );
86
    }
87
88
    /**
89
     * @return string
90
     */
91 2
    public function getMessage(): string
92
    {
93 2
        return $this->message;
94
    }
95
96 1
    #[ArrayShape([
97
        'min' => 'int|null',
98
        'max' => 'int|null',
99
        'exactly' => 'int|null',
100
        'message' => 'string[]',
101
        'lessThanMinMessage' => 'array',
102
        'greaterThanMaxMessage' => 'array',
103
        'notExactlyMessage' => 'array',
104
        'skipOnEmpty' => 'bool',
105
        'skipOnError' => 'bool',
106
    ])]
107
    public function getOptions(): array
108
    {
109 1
        return array_merge($this->getLimitOptions(), [
110
            'message' => [
111 1
                'message' => $this->getMessage(),
112
            ],
113 1
            'skipOnEmpty' => $this->skipOnEmpty,
114 1
            'skipOnError' => $this->skipOnError,
115
        ]);
116
    }
117
118 1
    public function getHandlerClassName(): string
119
    {
120 1
        return CountHandler::class;
121
    }
122
}
123