Passed
Push — master ( cd2db1...fdabae )
by Alexander
02:29
created

Subset::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

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 Yiisoft\Validator\BeforeValidationInterface;
10
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
11
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
12
use Yiisoft\Validator\SerializableRuleInterface;
13
use Yiisoft\Validator\ValidationContext;
14
15
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
16
final class Subset implements SerializableRuleInterface, BeforeValidationInterface
17
{
18
    use BeforeValidationTrait;
19
    use RuleNameTrait;
20
21 3
    public function __construct(
22
        private iterable $values,
23
        /**
24
         * @var bool whether the comparison is strict (both type and value must be the same)
25
         */
26
        private bool $strict = false,
27
        private string $iterableMessage = 'Value must be iterable.',
28
        private string $subsetMessage = 'Values must be ones of {values}.',
29
        private bool $skipOnEmpty = false,
30
        /**
31
         * @var callable
32
         */
33
        private $skipOnEmptyCallback = null,
34
        private bool $skipOnError = false,
35
        /**
36
         * @var Closure(mixed, ValidationContext):bool|null
37
         */
38
        private ?Closure $when = null,
39
    ) {
40 3
        $this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback);
41
    }
42
43
    /**
44
     * @return iterable
45
     */
46 9
    public function getValues(): iterable
47
    {
48 9
        return $this->values;
49
    }
50
51
    /**
52
     * @return bool
53
     */
54 9
    public function isStrict(): bool
55
    {
56 9
        return $this->strict;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getIterableMessage(): string
63
    {
64
        return $this->iterableMessage;
65
    }
66
67
    /**
68
     * @return string
69
     */
70 3
    public function getSubsetMessage(): string
71
    {
72 3
        return $this->subsetMessage;
73
    }
74
75 1
    public function getOptions(): array
76
    {
77
        return [
78 1
            'values' => $this->values,
79 1
            'strict' => $this->strict,
80
            'iterableMessage' => [
81 1
                'message' => $this->iterableMessage,
82
            ],
83
            'subsetMessage' => [
84 1
                'message' => $this->subsetMessage,
85
            ],
86 1
            'skipOnEmpty' => $this->skipOnEmpty,
87 1
            'skipOnError' => $this->skipOnError,
88
        ];
89
    }
90
91 1
    public function getHandlerClassName(): string
92
    {
93 1
        return SubsetHandler::class;
94
    }
95
}
96