Completed
Pull Request — 2.11.x (#3850)
by Benjamin
61:00
created

CompositeExpression::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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 1441
    public function __construct($type, array $parts = [])
43
    {
44 1441
        $this->type = $type;
45
46 1441
        $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 1441
    }
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 1441
     * @return \Doctrine\DBAL\Query\Expression\CompositeExpression
57
     */
58 1441
    public function addMultiple(array $parts = [])
59 1417
    {
60
        foreach ($parts as $part) {
61
            $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 1441
        }
63
64
        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 1441
     * @param mixed $part
73
     *
74 1441
     * @return \Doctrine\DBAL\Query\Expression\CompositeExpression
75 1308
     */
76
    public function add($part)
77
    {
78 1441
        if (empty($part)) {
79 1302
            return $this;
80
        }
81
82 1441
        if ($part instanceof self && count($part) === 0) {
83
            return $this;
84 1441
        }
85
86
        $this->parts[] = $part;
87
88
        return $this;
89
    }
90
91
    /**
92 1435
     * Returns a CompositeExpression with the given part added.
93
     *
94 1435
     * This methods returns a new instance. The current instance is unaffected by this method call.
95
     *
96
     * @param self|string ...$parts
97
     */
98
    public function with(...$parts) : self
99
    {
100
        $that = clone $this;
101
102 1381
        foreach ($parts as $part) {
103
            if ($part instanceof self && count($part) === 0) {
104 1381
                continue;
105 1325
            }
106
107
            $that->parts[] = $part;
108 1312
        }
109
110
        return $that;
111
    }
112
113
    /**
114
     * Retrieves the amount of expressions on composite expression.
115
     *
116 649
     * @return int
117
     */
118 649
    public function count()
119
    {
120
        return count($this->parts);
121
    }
122
123
    /**
124
     * Retrieves the string representation of this composite expression.
125
     *
126
     * @return string
127
     */
128
    public function __toString()
129
    {
130
        if ($this->count() === 1) {
131
            return (string) $this->parts[0];
132
        }
133
134
        return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')';
135
    }
136
137
    /**
138
     * Returns the type of this composite expression (AND/OR).
139
     *
140
     * @return string
141
     */
142
    public function getType()
143
    {
144
        return $this->type;
145
    }
146
}
147