LessThan::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 9
dl 0
loc 22
ccs 0
cts 0
cp 0
crap 2
rs 9.9

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\WhenInterface;
10
11
/**
12
 * Defines validation options to check that the specified value is equal to "target" value provided directly
13
 * ({@see LessThan::$targetValue}) or within an attribute ({@see LessThan::$targetAttribute}).
14
 *
15
 * The default comparison is based on number values (including float values). It's also possible to compare values as
16
 * strings byte by byte and compare original values as is. See {@see LessThan::$type} for all possible options.
17
 *
18
 * `new LessThan()` is a shortcut for `new Compare(operator: '<')`.
19
 *
20
 * @see CompareHandler
21
 * @see AbstractCompare
22
 *
23
 * @psalm-import-type WhenType from WhenInterface
24
 */
25 4
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
26
final class LessThan extends AbstractCompare
27
{
28
    /**
29
     * @param mixed $targetValue The value to be less than. When both this property and {@see $targetAttribute} are set,
30
     * this property takes precedence.
31
     * @param string|null $targetAttribute The attribute to be less than. When both this property and
32
     * {@see $targetValue} are set, the {@see $targetValue} takes precedence.
33
     * @param string $incorrectInputMessage A message used when the input is incorrect.
34
     *
35
     * You may use the following placeholders in the message:
36
     *
37
     * - `{attribute}`: the translated label of the attribute being validated.
38
     * - `{type}`: the type of the value being validated.
39
     * @param string $incorrectDataSetTypeMessage A message used when the value returned from a custom
40
     * data set s not scalar.
41
     *
42
     * You may use the following placeholders in the message:
43
     *
44
     * - `{type}`: type of the value.
45
     * @param string|null $message A message used when the value is not valid.
46
     *
47
     * You may use the following placeholders in the message:
48
     *
49
     * - `{attribute}`: the translated label of the attribute being validated.
50
     * - `{targetValue}`: the value to be compared with.
51
     * - `{targetAttribute}`: the name of the attribute to be compared with.
52
     * - `{targetAttributeValue}`: the value extracted from the attribute to be compared with if this attribute was set.
53
     * - `{targetValueOrAttribute}`: the value to be compared with or, if it's absent, the name of the attribute to be
54 4
     * compared with.
55 1
     * - `{value}`: the value being validated.
56
     *
57
     * When {@see CompareType::ORIGINAL} is used with complex types (neither scalar nor `null`), `{targetValue}`,
58 3
     * `{targetAttributeValue}` and `{targetValueOrAttribute}` parameters might contain the actual type instead of the
59 3
     * value, e.g. "object" for predictable formatting.
60 3
     * @param string $type The type of the values being compared:
61 3
     *
62 3
     * - {@see CompareType::NUMBER}: default, both values will be converted to float numbers before comparison.
63 3
     * - {@see CompareType::ORIGINAL} - compare the values as is.
64 3
     * - {@see CompareType::STRING} - cast both values to strings before comparison.
65
     *
66
     * {@see CompareType::NUMBER} and {@see CompareType::STRING} allow only scalar and `null` values, also objects
67 3
     * implementing {@see Stringable} interface.
68 3
     *
69
     * {@see CompareType::ORIGINAL} allows any values. All PHP comparison rules apply here, see comparison operators -
70
     * {@see https://www.php.net/manual/en/language.operators.comparison.php} and PHP type comparison tables -
71
     * {@see https://www.php.net/manual/en/types.comparisons.php} sections in official PHP documentation.
72 1
     *
73
     * @psalm-param CompareType::ORIGINAL | CompareType::STRING | CompareType::NUMBER $type
74 1
     *
75
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty.
76
     * See {@see SkipOnEmptyInterface}.
77
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error.
78
     * See {@see SkipOnErrorInterface}.
79
     * @param Closure|null $when A callable to define a condition for applying the rule.
80
     * See {@see WhenInterface}.
81
     *
82
     * @psalm-param WhenType $when
83
     */
84
    public function __construct(
85
        mixed $targetValue = null,
86
        string|null $targetAttribute = null,
87
        string $incorrectInputMessage = self::DEFAULT_INCORRECT_INPUT_MESSAGE,
88
        string $incorrectDataSetTypeMessage = self::DEFAULT_INCORRECT_DATA_SET_TYPE_MESSAGE,
89
        string|null $message = null,
90
        string $type = CompareType::NUMBER,
91
        bool|callable|null $skipOnEmpty = false,
92
        bool $skipOnError = false,
93
        Closure|null $when = null,
94
    ) {
95
        parent::__construct(
96
            targetValue: $targetValue,
97
            targetAttribute: $targetAttribute,
98
            incorrectInputMessage: $incorrectInputMessage,
99
            incorrectDataSetTypeMessage: $incorrectDataSetTypeMessage,
100
            message: $message,
101
            type: $type,
102
            operator: '<',
103
            skipOnEmpty: $skipOnEmpty,
104
            skipOnError: $skipOnError,
105
            when: $when,
106
        );
107
    }
108
}
109