Failed Conditions
Pull Request — 2.11.x (#3850)
by Benjamin
09:18
created

CompositeExpression::addMultiple()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Doctrine\DBAL\Query\Expression;
4
5
use Countable;
6
use function count;
7
use function implode;
8
9
/**
10
 * Composite expression is responsible to build a group of similar expression.
11
 */
12
class CompositeExpression implements Countable
13
{
14
    /**
15
     * Constant that represents an AND composite expression.
16
     */
17
    public const TYPE_AND = 'AND';
18
19
    /**
20
     * Constant that represents an OR composite expression.
21
     */
22
    public const TYPE_OR = 'OR';
23
24
    /**
25
     * The instance type of composite expression.
26
     *
27
     * @var string
28
     */
29
    private $type;
30
31
    /**
32
     * Each expression part of the composite expression.
33
     *
34
     * @var self[]|string[]
35
     */
36
    private $parts = [];
37
38
    /**
39
     * @param string          $type  Instance type of composite expression.
40
     * @param self[]|string[] $parts Composition of expressions to be joined on composite expression.
41
     */
42 1522
    public function __construct($type, array $parts = [])
43
    {
44 1522
        $this->type = $type;
45
46 1522
        $this->addMultiple($parts);
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Query\Expr...pression::addMultiple() has been deprecated: This class will be made immutable. Use with() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

46
        /** @scrutinizer ignore-deprecated */ $this->addMultiple($parts);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
47 1522
    }
48
49
    /**
50
     * Adds multiple parts to composite expression.
51
     *
52
     * @deprecated This class will be made immutable. Use with() instead.
53
     *
54
     * @param self[]|string[] $parts
55
     *
56
     * @return \Doctrine\DBAL\Query\Expression\CompositeExpression
57
     */
58 1522
    public function addMultiple(array $parts = [])
59
    {
60 1522
        foreach ($parts as $part) {
61 1498
            $this->add($part);
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Query\Expr...positeExpression::add() has been deprecated: This class will be made immutable. Use with() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

61
            /** @scrutinizer ignore-deprecated */ $this->add($part);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
62
        }
63
64 1522
        return $this;
65
    }
66
67
    /**
68
     * Adds an expression to composite expression.
69
     *
70
     * @deprecated This class will be made immutable. Use with() instead.
71
     *
72
     * @param mixed $part
73
     *
74
     * @return \Doctrine\DBAL\Query\Expression\CompositeExpression
75
     */
76 1522
    public function add($part)
77
    {
78 1522
        if (empty($part)) {
79 1386
            return $this;
80
        }
81
82 1522
        if ($this->isEmptySelf($part)) {
83 1380
            return $this;
84
        }
85
86 1522
        $this->parts[] = $part;
87
88 1522
        return $this;
89
    }
90
91
    /**
92
     * Returns a new CompositeExpression with the given parts added.
93
     *
94
     * @param self|string $part
95
     * @param self|string ...$parts
96
     */
97 1426
    public function with($part, ...$parts) : self
98
    {
99 1426
        $that = clone $this;
100
101 1426
        if (! $this->isEmptySelf($part)) {
102 1426
            $that->parts[] = $part;
103
        }
104
105 1426
        foreach ($parts as $part) {
0 ignored issues
show
introduced by
$part is overwriting one of the parameters of this function.
Loading history...
106
            if ($this->isEmptySelf($part)) {
107
                continue;
108
            }
109
110
            $that->parts[] = $part;
111
        }
112
113 1426
        return $that;
114
    }
115
116
    /**
117
     * @param self|string $part
118
     */
119 1522
    private function isEmptySelf($part) : bool
120
    {
121 1522
        return $part instanceof self && count($part) === 0;
122
    }
123
124
    /**
125
     * Retrieves the amount of expressions on composite expression.
126
     *
127
     * @return int
128
     */
129 1516
    public function count()
130
    {
131 1516
        return count($this->parts);
132
    }
133
134
    /**
135
     * Retrieves the string representation of this composite expression.
136
     *
137
     * @return string
138
     */
139 1432
    public function __toString()
140
    {
141 1432
        if ($this->count() === 1) {
142 1376
            return (string) $this->parts[0];
143
        }
144
145 1362
        return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')';
146
    }
147
148
    /**
149
     * Returns the type of this composite expression (AND/OR).
150
     *
151
     * @return string
152
     */
153 674
    public function getType()
154
    {
155 674
        return $this->type;
156
    }
157
}
158