Passed
Pull Request — master (#222)
by Dmitriy
02:26
created

Count::__construct()   B

Complexity

Conditions 10
Paths 4

Size

Total Lines 56
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 10

Importance

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

How to fix   Long Method    Complexity    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\Count;
6
7
use Attribute;
8
use Closure;
9
use Countable;
10
use InvalidArgumentException;
11
use Yiisoft\Validator\Rule\RuleNameTrait;
12
use Yiisoft\Validator\Rule\HandlerClassNameTrait;
13
use Yiisoft\Validator\RuleInterface;
14
15
/**
16
 * Validates that the value contains certain number of items. Can be applied to arrays or classes implementing
17
 * {@see Countable} interface.
18
 */
19
#[Attribute(Attribute::TARGET_PROPERTY)]
20
final class Count implements RuleInterface
21
{
22
    use HandlerClassNameTrait;
23
    use RuleNameTrait;
24
25 6
    public function __construct(
26
        /**
27
         * @var int|null minimum number of items. null means no minimum number limit.
28
         *
29
         * @see $tooFewItemsMessage for the customized message for a value with too few items.
30
         */
31
        public ?int $min = null,
32
        /**
33
         * @var int|null maximum number of items. null means no maximum number limit.
34
         *
35
         * @see $tooManyItemsMessage for the customized message for a value wuth too many items.
36
         */
37
        public ?int $max = null,
38
        /**
39
         * @var int|null exact number of items. null means no strict comparison. Mutually exclusive with {@see $min} and
40
         * {@see $max}.
41
         */
42
        public ?int $exactly = null,
43
        /**
44
         * @var string user-defined error message used when the value is neither an array nor implementing
45
         * {@see \Countable} interface.
46
         *
47
         * @see Countable
48
         */
49
        public string $message = 'This value must be an array or implement \Countable interface.',
50
        /**
51
         * @var string user-defined error message used when the number of items is smaller than {@see $min}.
52
         */
53
        public string $tooFewItemsMessage = 'This value must contain at least {min, number} ' .
54
        '{min, plural, one{item} other{items}}.',
55
        /**
56
         * @var string user-defined error message used when the number of items is greater than {@see $max}.
57
         */
58
        public string $tooManyItemsMessage = 'This value must contain at most {max, number} ' .
59
        '{max, plural, one{item} other{items}}.',
60
        /**
61
         * @var string user-defined error message used when the number of items does not equal {@see $exactly}.
62
         */
63
        public string $notExactlyMessage = 'This value must contain exactly {max, number} ' .
64
        '{max, plural, one{item} other{items}}.',
65
        public bool $skipOnEmpty = false,
66
        public bool $skipOnError = false,
67
        public ?Closure $when = null,
68
    ) {
69 6
        if (!$this->min && !$this->max && !$this->exactly) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->exactly of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $this->min of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $this->max of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
70 1
            throw new InvalidArgumentException(
71
                'At least one of these attributes must be specified: $min, $max, $exactly.'
72
            );
73
        }
74
75 5
        if ($this->exactly && ($this->min || $this->max)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->max of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $this->exactly of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $this->min of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
76 3
            throw new InvalidArgumentException('$exactly is mutually exclusive with $min and $max.');
77
        }
78
79 2
        if ($this->min && $this->max && $this->min === $this->max) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->min of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $this->max of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
80 1
            throw new InvalidArgumentException('Use $exactly instead.');
81
        }
82
    }
83
84 1
    public function getOptions(): array
85
    {
86
        return [
87 1
            'min' => $this->min,
88 1
            'max' => $this->max,
89 1
            'exactly' => $this->exactly,
90
            'message' => [
91 1
                'message' => $this->message,
92
            ],
93
            'tooFewItemsMessage' => [
94 1
                'message' => $this->tooFewItemsMessage,
95 1
                'parameters' => ['min' => $this->min],
96
            ],
97
            'tooManyItemsMessage' => [
98 1
                'message' => $this->tooManyItemsMessage,
99 1
                'parameters' => ['max' => $this->max],
100
            ],
101
            'notExactlyMessage' => [
102 1
                'message' => $this->notExactlyMessage,
103 1
                'parameters' => ['exactly' => $this->exactly],
104
            ],
105 1
            'skipOnEmpty' => $this->skipOnEmpty,
106 1
            'skipOnError' => $this->skipOnError,
107
        ];
108
    }
109
}
110