Passed
Pull Request — master (#376)
by Alexander
03:40 queued 01:00
created

BetweenColumnsCondition::fromArrayDefinition()   B

Complexity

Conditions 11
Paths 5

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 20
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 35
rs 7.3166

How to fix   Complexity   

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:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\QueryBuilder\Conditions;
6
7
use Iterator;
8
use Yiisoft\Db\Exception\InvalidArgumentException;
9
use Yiisoft\Db\Expression\ExpressionInterface;
10
use Yiisoft\Db\QueryBuilder\Conditions\Interface\BetweenColumnsConditionInterface;
11
12
/**
13
 * Class BetweenColumnCondition represents a `BETWEEN` condition where values are between two columns.
14
 *
15
 * For example:.
16
 *
17
 * ```php
18
 * new BetweenColumnsCondition(42, 'BETWEEN', 'min_value', 'max_value')
19
 * // Will be build to:
20
 * // 42 BETWEEN min_value AND max_value
21
 * ```
22
 *
23
 * And a more complex example:
24
 *
25
 * ```php
26
 * new BetweenColumnsCondition(
27
 *    new Expression('NOW()'),
28
 *    'NOT BETWEEN',
29
 *    (new Query)->select('time')->from('log')->orderBy('id ASC')->limit(1),
30
 *    'update_time'
31
 * );
32
 *
33
 * // Will be built to:
34
 * // NOW() NOT BETWEEN (SELECT time FROM log ORDER BY id ASC LIMIT 1) AND update_time
35
 * ```
36
 */
37
final class BetweenColumnsCondition implements BetweenColumnsConditionInterface
38
{
39
    public function __construct(
40
        private array|int|string|Iterator|ExpressionInterface $value,
41
        private string $operator,
42
        private string|ExpressionInterface $intervalStartColumn,
43
        private string|ExpressionInterface $intervalEndColumn
44
    ) {
45
    }
46
47
    public function getIntervalEndColumn(): string|ExpressionInterface
48
    {
49
        return $this->intervalEndColumn;
50
    }
51
52
    public function getIntervalStartColumn(): string|ExpressionInterface
53
    {
54
        return $this->intervalStartColumn;
55
    }
56
57
    public function getOperator(): string
58
    {
59
        return $this->operator;
60
    }
61
62
    public function getValue(): array|int|string|Iterator|ExpressionInterface
63
    {
64
        return $this->value;
65
    }
66
67
    /**
68
     * @throws InvalidArgumentException
69
     */
70
    public static function fromArrayDefinition(string $operator, array $operands): self
71
    {
72
        if (!isset($operands[0], $operands[1], $operands[2])) {
73
            throw new InvalidArgumentException("Operator '$operator' requires three operands.");
74
        }
75
76
        if (
77
            !is_array($operands[0]) &&
78
            !is_int($operands[0]) &&
79
            !is_string($operands[0]) &&
80
            !($operands[0] instanceof Iterator) &&
81
            !($operands[0] instanceof ExpressionInterface)
82
        ) {
83
            throw new InvalidArgumentException("Operator '$operator' requires value as first operand.");
84
        }
85
86
        if (
87
            !is_string($operands[1]) &&
88
            !($operands[1] instanceof ExpressionInterface)
89
        ) {
90
            throw new InvalidArgumentException(
91
                "Operator '$operator' requires interval start column as second operand."
92
            );
93
        }
94
95
        if (
96
            !is_string($operands[2]) &&
97
            !($operands[2] instanceof ExpressionInterface)
98
        ) {
99
            throw new InvalidArgumentException(
100
                "Operator '$operator' requires interval end column as third operand."
101
            );
102
        }
103
104
        return new self($operands[0], $operator, $operands[1], $operands[2]);
105
    }
106
}
107