Passed
Pull Request — master (#274)
by
unknown
03:13 queued 22s
created

Count::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
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 54
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.
32
         *
33
         * @see $lessThanMinMessage for the customized message for a value with too few items.
34
         */
35
        ?int $min = null,
36
        /**
37
         * @var int|null maximum number of items. null means no maximum number limit.
38
         *
39
         * @see $greaterThanMaxMessage for the customized message for a value wuth too many items.
40
         */
41
        ?int $max = null,
42
        /**
43
         * @var int|null exact number of items. null means no strict comparison. Mutually exclusive with {@see $min} and
44
         * {@see $max}.
45
         */
46
        ?int $exactly = null,
47
        /**
48
         * @var string user-defined error message used when the value is neither an array nor implementing
49
         * {@see \Countable} interface.
50
         *
51
         * @see Countable
52
         */
53
        private string $message = 'This value must be an array or implement \Countable interface.',
54
        /**
55
         * @var string user-defined error message used when the number of items is smaller than {@see $min}.
56
         */
57
        string $lessThanMinMessage = 'This value must contain at least {min, number} {min, plural, one{item} ' .
58
        'other{items}}.',
59
        /**
60
         * @var string user-defined error message used when the number of items is greater than {@see $max}.
61
         */
62
        string $greaterThanMaxMessage = 'This value must contain at most {max, number} {max, plural, one{item} ' .
63
        'other{items}}.',
64
        /**
65
         * @var string user-defined error message used when the number of items does not equal {@see $exactly}.
66
         */
67
        string $notExactlyMessage = 'This value must contain exactly {exactly, number} {exactly, plural, one{item} ' .
68
        'other{items}}.',
69
        private bool $skipOnEmpty = false,
70
        private bool $skipOnError = false,
71
        /**
72
         * @var Closure(mixed, ValidationContext):bool|null
73
         */
74
        private ?Closure $when = null,
75
    ) {
76 7
        $this->initLimitProperties(
77
            $min,
78
            $max,
79
            $exactly,
80
            $lessThanMinMessage,
81
            $greaterThanMaxMessage,
82
            $notExactlyMessage
83
        );
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getMessage(): string
90
    {
91
        return $this->message;
92
    }
93
94 1
    #[ArrayShape([
95
        'min' => 'int|null',
96
        'max' => 'int|null',
97
        'exactly' => 'int|null',
98
        'message' => 'string[]',
99
        'lessThanMinMessage' => 'array',
100
        'greaterThanMaxMessage' => 'array',
101
        'notExactlyMessage' => 'array',
102
        'skipOnEmpty' => 'bool',
103
        'skipOnError' => 'bool',
104
    ])]
105
    public function getOptions(): array
106
    {
107 1
        return array_merge($this->getLimitOptions(), [
108
            'message' => [
109 1
                'message' => $this->message,
110
            ],
111 1
            'skipOnEmpty' => $this->skipOnEmpty,
112 1
            'skipOnError' => $this->skipOnError,
113
        ]);
114
    }
115
116 1
    public function getHandlerClassName(): string
117
    {
118 1
        return CountHandler::class;
119
    }
120
}
121