Passed
Pull Request — master (#248)
by Rustam
12:42
created

Equal::__construct()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 42
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 42
ccs 11
cts 12
cp 0.9167
rs 9.9
cc 4
nc 2
nop 8
crap 4.0092

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 checked 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 extends Compare
30
{
31 1
    public function __construct(
32
        /**
33
         * @var mixed the constant value to be equal to. When both this property
34
         * and {@see $targetAttribute} are set, this property takes precedence.
35
         */
36
        private $targetValue = null,
37
        /**
38
         * @var mixed the attribute to be equal to. When both this property
39
         * and {@see $targetValue} are set, the {@see $targetValue} takes precedence.
40
         */
41
        private ?string $targetAttribute = null,
42
        /**
43
         * @var string|null user-defined error message
44
         */
45
        private ?string $message = null,
46
        /**
47
         * @var string the type of the values being compared.
48
         */
49
        private string $type = self::TYPE_STRING,
50
        /**
51
         * @var bool Whether this validator strictly check.
52
         */
53
        private bool $strict = false,
54
        private bool $skipOnEmpty = false,
55
        private bool $skipOnError = false,
56
        /**
57
         * @var Closure(mixed, ValidationContext):bool|null
58
         */
59
        private ?Closure $when = null,
60
    ) {
61 1
        if ($this->targetValue === null && $this->targetAttribute === null) {
62
            throw new RuntimeException('Either "targetValue" or "targetAttribute" must be specified.');
63
        }
64 1
        parent::__construct(
65 1
            targetValue: $this->targetValue,
66 1
            targetAttribute: $this->targetAttribute,
67 1
            message: $this->message,
68 1
            type: $this->type,
69 1
            operator: $this->strict ? '===' : '==',
70 1
            skipOnEmpty: $this->skipOnEmpty,
71 1
            skipOnError: $this->skipOnError,
72 1
            when: $this->when
73
        );
74
    }
75
76
    public function getHandlerClassName(): string
77
    {
78
        return CompareHandler::class;
79
    }
80
}
81