Passed
Pull Request — master (#248)
by Rustam
03:00
created

Equal::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 32
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3.3332

Importance

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

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 JetBrains\PhpStorm\ArrayShape;
10
use RuntimeException;
11
use Yiisoft\Validator\ParametrizedRuleInterface;
12
use Yiisoft\Validator\BeforeValidationInterface;
13
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
14
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
15
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
16
use Yiisoft\Validator\ValidationContext;
17
18
/**
19
 * Validates if the specified value is equal to another value or attribute.
20
 *
21
 * The value being validated with {@see Equal::$targetValue} or {@see Equal::$targetAttribute}, which
22
 * is set in the constructor.
23
 *
24
 * The default validation function is based on string values, which means the values
25
 * are equals byte by byte. When validating numbers, make sure to change {@see Equal::$type} to
26
 * {@see Equal::TYPE_NUMBER} to enable numeric validation.
27
 */
28
#[Attribute(Attribute::TARGET_PROPERTY)]
29
final class Equal implements ParametrizedRuleInterface, BeforeValidationInterface
30
{
31
    use BeforeValidationTrait;
32
    use HandlerClassNameTrait;
33
    use RuleNameTrait;
34
35
    /**
36
     * Constant for specifying validation as string values.
37
     * No conversion will be done before validation.
38
     *
39
     * @see $type
40
     */
41
    public const TYPE_STRING = 'string';
42
    /**
43
     * Constant for specifying validation as numeric values.
44
     * String values will be converted into numbers before validation.
45
     *
46
     * @see $type
47
     */
48
    public const TYPE_NUMBER = 'number';
49
50 1
    public function __construct(
51
        /**
52
         * @var mixed the constant value to be equal with. When both this property
53
         * and {@see $targetAttribute} are set, this property takes precedence.
54
         */
55
        private $targetValue = null,
56
        /**
57
         * @var mixed the attribute to be equal with. When both this property
58
         * and {@see $targetValue} are set, the {@see $targetValue} takes precedence.
59
         */
60
        private ?string $targetAttribute = null,
61
        /**
62
         * @var string|null user-defined error message
63
         */
64
        private ?string $message = null,
65
        /**
66
         * @var string the type of the values being compared.
67
         */
68
        private string $type = self::TYPE_STRING,
69
        /**
70
         * @var bool Whether this validator strictly check.
71
         */
72
        private bool $strict = false,
73
        private bool $skipOnEmpty = false,
74
        private bool $skipOnError = false,
75
        /**
76
         * @var Closure(mixed, ValidationContext):bool|null
77
         */
78
        private ?Closure $when = null,
79
    ) {
80 1
        if ($this->targetValue === null && $this->targetAttribute === null) {
81
            throw new RuntimeException('Either "targetValue" or "targetAttribute" must be specified.');
82
        }
83
    }
84
85
    /**
86
     * @return mixed
87
     */
88 7
    public function getTargetValue(): mixed
89
    {
90 7
        return $this->targetValue;
91
    }
92
93
    /**
94
     * @return string|null
95
     */
96 5
    public function getTargetAttribute(): ?string
97
    {
98 5
        return $this->targetAttribute;
99
    }
100
101
    /**
102
     * @return string
103
     */
104 7
    public function getType(): string
105
    {
106 7
        return $this->type;
107
    }
108
109 7
    public function shouldCheckStrictly(): bool
110
    {
111 7
        return $this->strict;
112
    }
113
114 12
    public function getMessage(): string
115
    {
116 12
        return $this->message ?? 'Value must be equal to "{targetValueOrAttribute}".';
117
    }
118
119 8
    #[ArrayShape([
120
        'targetValue' => 'mixed',
121
        'targetAttribute' => 'string|null',
122
        'message' => 'array',
123
        'type' => 'string',
124
        'strict' => 'bool',
125
        'skipOnEmpty' => 'bool',
126
        'skipOnError' => 'bool',
127
    ])]
128
    public function getOptions(): array
129
    {
130
        return [
131 8
            'targetValue' => $this->targetValue,
132 8
            'targetAttribute' => $this->targetAttribute,
133
            'message' => [
134 8
                'message' => $this->getMessage(),
135
                'parameters' => [
136 8
                    'targetValue' => $this->targetValue,
137 8
                    'targetAttribute' => $this->targetAttribute,
138 8
                    'targetValueOrAttribute' => $this->targetValue ?? $this->targetAttribute,
139
                ],
140
            ],
141 8
            'type' => $this->type,
142 8
            'strict' => $this->strict,
143 8
            'skipOnEmpty' => $this->skipOnEmpty,
144 8
            'skipOnError' => $this->skipOnError,
145
        ];
146
    }
147
}
148