Failed Conditions
Push — 2.11.x ( ad4f12...ce9b49 )
by Sergei
49:32 queued 46:10
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 1468
    public function __construct($type, array $parts = [])
43
    {
44 1468
        $this->type = $type;
45
46 1468
        $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 1468
    }
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 1468
    public function addMultiple(array $parts = [])
59
    {
60 1468
        foreach ($parts as $part) {
61 1444
            $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 1468
        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 1468
    public function add($part)
77
    {
78 1468
        if (empty($part)) {
79 1333
            return $this;
80
        }
81
82 1468
        if ($part instanceof self && count($part) === 0) {
83 1327
            return $this;
84
        }
85
86 1468
        $this->parts[] = $part;
87
88 1468
        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 1372
    public function with($part, ...$parts) : self
98
    {
99 1372
        $that = clone $this;
100
101 1372
        $that->parts[] = $part;
102
103 1372
        foreach ($parts as $part) {
0 ignored issues
show
introduced by
$part is overwriting one of the parameters of this function.
Loading history...
104
            $that->parts[] = $part;
105
        }
106
107 1372
        return $that;
108
    }
109
110
    /**
111
     * Retrieves the amount of expressions on composite expression.
112
     *
113
     * @return int
114
     */
115 1462
    public function count()
116
    {
117 1462
        return count($this->parts);
118
    }
119
120
    /**
121
     * Retrieves the string representation of this composite expression.
122
     *
123
     * @return string
124
     */
125 1381
    public function __toString()
126
    {
127 1381
        if ($this->count() === 1) {
128 1325
            return (string) $this->parts[0];
129
        }
130
131 1312
        return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')';
132
    }
133
134
    /**
135
     * Returns the type of this composite expression (AND/OR).
136
     *
137
     * @return string
138
     */
139 649
    public function getType()
140
    {
141 649
        return $this->type;
142
    }
143
}
144