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

CompositeExpression   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 96.55%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 13
eloc 25
c 4
b 0
f 0
dl 0
loc 130
rs 10
ccs 28
cts 29
cp 0.9655

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 3 1
A __toString() 0 7 2
A addMultiple() 0 7 2
A __construct() 0 5 1
A add() 0 13 4
A count() 0 3 1
A with() 0 11 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 1586
    public function __construct($type, array $parts = [])
43
    {
44 1586
        $this->type = $type;
45
46 1586
        $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 1586
    }
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 1586
    public function addMultiple(array $parts = [])
59
    {
60 1586
        foreach ($parts as $part) {
61 1538
            $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 1586
        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 1586
    public function add($part)
77
    {
78 1586
        if (empty($part)) {
79 1341
            return $this;
80
        }
81
82 1586
        if ($part instanceof self && count($part) === 0) {
83 1329
            return $this;
84
        }
85
86 1586
        $this->parts[] = $part;
87
88 1586
        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 1394
    public function with($part, ...$parts) : self
98
    {
99 1394
        $that = clone $this;
100
101 1394
        $that->parts[] = $part;
102
103 1394
        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 1394
        return $that;
108
    }
109
110
    /**
111
     * Retrieves the amount of expressions on composite expression.
112
     *
113
     * @return int
114
     */
115 1574
    public function count()
116
    {
117 1574
        return count($this->parts);
118
    }
119
120
    /**
121
     * Retrieves the string representation of this composite expression.
122
     *
123
     * @return string
124
     */
125 1487
    public function __toString()
126
    {
127 1487
        if ($this->count() === 1) {
128 1375
            return (string) $this->parts[0];
129
        }
130
131 1374
        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 673
    public function getType()
140
    {
141 673
        return $this->type;
142
    }
143
}
144