Passed
Pull Request — 2.x (#135)
by Maxim
17:48
created

WhereJsonTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 258
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 79
c 1
b 0
f 0
dl 0
loc 258
rs 10
wmc 18

18 Methods

Rating   Name   Duplication   Size   Complexity  
A andWhereJson() 0 3 1
A whereJsonContains() 0 10 1
A orWhereJsonContains() 0 10 1
A orWhereJsonDoesntContainKey() 0 10 1
A whereJsonDoesntContainKey() 0 10 1
A whereJson() 0 10 1
A orWhereJson() 0 10 1
A andWhereJsonLength() 0 3 1
A andWhereJsonDoesntContainKey() 0 3 1
A orWhereJsonDoesntContain() 0 14 1
A orWhereJsonContainsKey() 0 10 1
A andWhereJsonContainsKey() 0 3 1
A andWhereJsonContains() 0 3 1
A whereJsonContainsKey() 0 10 1
A orWhereJsonLength() 0 10 1
A whereJsonLength() 0 10 1
A andWhereJsonDoesntContain() 0 7 1
A whereJsonDoesntContain() 0 14 1
1
<?php
2
3
/**
4
 * This file is part of Cycle ORM package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Database\Driver\MySQL\Query\Traits;
13
14
use Cycle\Database\Driver\Jsoner;
15
use Cycle\Database\Driver\MySQL\Injection\CompileJson;
16
use Cycle\Database\Driver\MySQL\Injection\CompileJsonContains;
17
use Cycle\Database\Driver\MySQL\Injection\CompileJsonContainsKey;
18
use Cycle\Database\Driver\MySQL\Injection\CompileJsonDoesntContain;
19
use Cycle\Database\Driver\MySQL\Injection\CompileJsonDoesntContainKey;
20
use Cycle\Database\Driver\MySQL\Injection\CompileJsonLength;
21
22
/**
23
 * @internal
24
 *
25
 * @psalm-internal Cycle\Database\Driver\MySQL
26
 */
27
trait WhereJsonTrait
28
{
29
    /**
30
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
31
     */
32
    public function whereJson(string $column, mixed $value): self
33
    {
34
        $this->registerToken(
0 ignored issues
show
Bug introduced by
It seems like registerToken() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

34
        $this->/** @scrutinizer ignore-call */ 
35
               registerToken(
Loading history...
35
            'AND',
36
            [new CompileJson($column), $value],
37
            $this->whereTokens,
38
            $this->whereWrapper()
0 ignored issues
show
Bug introduced by
It seems like whereWrapper() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

38
            $this->/** @scrutinizer ignore-call */ 
39
                   whereWrapper()
Loading history...
39
        );
40
41
        return $this;
42
    }
43
44
    /**
45
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
46
     */
47
    public function andWhereJson(string $column, mixed $value): self
48
    {
49
        return $this->whereJson($column, $value);
50
    }
51
52
    /**
53
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
54
     */
55
    public function orWhereJson(string $column, mixed $value): self
56
    {
57
        $this->registerToken(
58
            'OR',
59
            [new CompileJson($column), $value],
60
            $this->whereTokens,
61
            $this->whereWrapper()
62
        );
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
69
     * @param bool $encode Encode the value into JSON.
70
     * @param bool $validate Checking the value that it is valid JSON.
71
     */
72
    public function whereJsonContains(string $column, mixed $value, bool $encode = true, bool $validate = true): self
73
    {
74
        $this->registerToken(
75
            'AND',
76
            [new CompileJsonContains($column, Jsoner::toJson($value, $encode, $validate))],
77
            $this->whereTokens,
78
            $this->whereWrapper()
79
        );
80
81
        return $this;
82
    }
83
84
    /**
85
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
86
     * @param bool $encode Encode the value into JSON.
87
     * @param bool $validate Checking the value that it is valid JSON.
88
     */
89
    public function andWhereJsonContains(string $column, mixed $value, bool $encode = true, bool $validate = true): self
90
    {
91
        return $this->whereJsonContains($column, $value, $encode, $validate);
92
    }
93
94
    /**
95
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
96
     * @param bool $encode Encode the value into JSON.
97
     * @param bool $validate Checking the value that it is valid JSON.
98
     */
99
    public function orWhereJsonContains(string $column, mixed $value, bool $encode = true, bool $validate = true): self
100
    {
101
        $this->registerToken(
102
            'OR',
103
            [new CompileJsonContains($column, Jsoner::toJson($value, $encode, $validate))],
104
            $this->whereTokens,
105
            $this->whereWrapper()
106
        );
107
108
        return $this;
109
    }
110
111
    /**
112
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
113
     * @param bool $encode Encode the value into JSON.
114
     * @param bool $validate Checking the value that it is valid JSON.
115
     */
116
    public function whereJsonDoesntContain(
117
        string $column,
118
        mixed $value,
119
        bool $encode = true,
120
        bool $validate = true
121
    ): self {
122
        $this->registerToken(
123
            'AND',
124
            [new CompileJsonDoesntContain($column, Jsoner::toJson($value, $encode, $validate))],
125
            $this->whereTokens,
126
            $this->whereWrapper()
127
        );
128
129
        return $this;
130
    }
131
132
    /**
133
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
134
     * @param bool $encode Encode the value into JSON.
135
     * @param bool $validate Checking the value that it is valid JSON.
136
     */
137
    public function andWhereJsonDoesntContain(
138
        string $column,
139
        mixed $value,
140
        bool $encode = true,
141
        bool $validate = true
142
    ): self {
143
        return $this->whereJsonDoesntContain($column, $value, $encode, $validate);
144
    }
145
146
    /**
147
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
148
     * @param bool $encode Encode the value into JSON.
149
     * @param bool $validate Checking the value that it is valid JSON.
150
     */
151
    public function orWhereJsonDoesntContain(
152
        string $column,
153
        mixed $value,
154
        bool $encode = true,
155
        bool $validate = true
156
    ): self {
157
        $this->registerToken(
158
            'OR',
159
            [new CompileJsonDoesntContain($column, Jsoner::toJson($value, $encode, $validate))],
160
            $this->whereTokens,
161
            $this->whereWrapper()
162
        );
163
164
        return $this;
165
    }
166
167
    /**
168
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
169
     */
170
    public function whereJsonContainsKey(string $column): self
171
    {
172
        $this->registerToken(
173
            'AND',
174
            [new CompileJsonContainsKey($column)],
175
            $this->whereTokens,
176
            $this->whereWrapper()
177
        );
178
179
        return $this;
180
    }
181
182
    /**
183
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
184
     */
185
    public function andWhereJsonContainsKey(string $column): self
186
    {
187
        return $this->whereJsonContainsKey($column);
188
    }
189
190
    /**
191
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
192
     */
193
    public function orWhereJsonContainsKey(string $column): self
194
    {
195
        $this->registerToken(
196
            'OR',
197
            [new CompileJsonContainsKey($column)],
198
            $this->whereTokens,
199
            $this->whereWrapper()
200
        );
201
202
        return $this;
203
    }
204
205
    /**
206
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
207
     */
208
    public function whereJsonDoesntContainKey(string $column): self
209
    {
210
        $this->registerToken(
211
            'AND',
212
            [new CompileJsonDoesntContainKey($column)],
213
            $this->whereTokens,
214
            $this->whereWrapper()
215
        );
216
217
        return $this;
218
    }
219
220
    /**
221
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
222
     */
223
    public function andWhereJsonDoesntContainKey(string $column): self
224
    {
225
        return $this->whereJsonDoesntContainKey($column);
226
    }
227
228
    /**
229
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
230
     */
231
    public function orWhereJsonDoesntContainKey(string $column): self
232
    {
233
        $this->registerToken(
234
            'OR',
235
            [new CompileJsonDoesntContainKey($column)],
236
            $this->whereTokens,
237
            $this->whereWrapper()
238
        );
239
240
        return $this;
241
    }
242
243
    /**
244
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
245
     * @param int<0, max> $length
246
     * @param non-empty-string $operator
247
     */
248
    public function whereJsonLength(string $column, int $length, string $operator = '='): self
249
    {
250
        $this->registerToken(
251
            'AND',
252
            [new CompileJsonLength($column, $length, $operator)],
253
            $this->whereTokens,
254
            $this->whereWrapper()
255
        );
256
257
        return $this;
258
    }
259
260
    /**
261
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
262
     * @param int<0, max> $length
263
     * @param non-empty-string $operator
264
     */
265
    public function andWhereJsonLength(string $column, int $length, string $operator = '='): self
266
    {
267
        return $this->whereJsonLength($column, $length, $operator);
268
    }
269
270
    /**
271
     * @param non-empty-string $column
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
272
     * @param int<0, max> $length
273
     * @param non-empty-string $operator
274
     */
275
    public function orWhereJsonLength(string $column, int $length, string $operator = '='): self
276
    {
277
        $this->registerToken(
278
            'OR',
279
            [new CompileJsonLength($column, $length, $operator)],
280
            $this->whereTokens,
281
            $this->whereWrapper()
282
        );
283
284
        return $this;
285
    }
286
}
287