Passed
Pull Request — 2.x (#135)
by Aleksei
16:08
created

WhereJsonTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
eloc 53
dl 0
loc 176
rs 10
c 5
b 2
f 0
wmc 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A whereJson() 0 10 1
A orWhereJsonDoesntContain() 0 7 1
A orWhereJsonLength() 0 10 1
A orWhereJsonContains() 0 7 1
A whereJsonDoesntContainKey() 0 10 1
A whereJsonDoesntContain() 0 7 1
A whereJsonContainsKey() 0 10 1
A orWhereJson() 0 10 1
A whereJsonContains() 0 3 1
A whereJsonLength() 0 10 1
A orWhereJsonContainsKey() 0 10 1
A orWhereJsonDoesntContainKey() 0 10 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\SQLite\Query\Traits;
13
14
use Cycle\Database\Driver\SQLite\Injection\CompileJson;
15
use Cycle\Database\Driver\SQLite\Injection\CompileJsonContainsKey;
16
use Cycle\Database\Driver\SQLite\Injection\CompileJsonDoesntContainKey;
17
use Cycle\Database\Driver\SQLite\Injection\CompileJsonLength;
18
use Cycle\Database\Exception\DriverException;
19
20
/**
21
 * @internal
22
 *
23
 * @psalm-internal Cycle\Database\Driver\SQLite
24
 */
25
trait WhereJsonTrait
26
{
27
    /**
28
     * @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...
29
     */
30
    public function whereJson(string $column, mixed $value): static
31
    {
32
        $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

32
        $this->/** @scrutinizer ignore-call */ 
33
               registerToken(
Loading history...
33
            'AND',
34
            [new CompileJson($column), $value],
35
            $this->whereTokens,
36
            $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

36
            $this->/** @scrutinizer ignore-call */ 
37
                   whereWrapper()
Loading history...
37
        );
38
39
        return $this;
40
    }
41
42
    /**
43
     * @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...
44
     */
45
    public function orWhereJson(string $column, mixed $value): static
46
    {
47
        $this->registerToken(
48
            'OR',
49
            [new CompileJson($column), $value],
50
            $this->whereTokens,
51
            $this->whereWrapper()
52
        );
53
54
        return $this;
55
    }
56
57
    /**
58
     * @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...
59
     * @param bool $encode Encode the value into JSON.
60
     * @param bool $validate Check that $value is a valid JSON string if the $encode parameter is false.
61
     */
62
    public function whereJsonContains(string $column, mixed $value, bool $encode = true, bool $validate = true): static
0 ignored issues
show
Unused Code introduced by
The parameter $validate is not used and could be removed. ( Ignorable by Annotation )

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

62
    public function whereJsonContains(string $column, mixed $value, bool $encode = true, /** @scrutinizer ignore-unused */ bool $validate = true): static

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

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

62
    public function whereJsonContains(/** @scrutinizer ignore-unused */ string $column, mixed $value, bool $encode = true, bool $validate = true): static

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

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

62
    public function whereJsonContains(string $column, /** @scrutinizer ignore-unused */ mixed $value, bool $encode = true, bool $validate = true): static

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $encode is not used and could be removed. ( Ignorable by Annotation )

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

62
    public function whereJsonContains(string $column, mixed $value, /** @scrutinizer ignore-unused */ bool $encode = true, bool $validate = true): static

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
        throw new DriverException('This database engine does not support JSON contains operations.');
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 Check that $value is a valid JSON string if the $encode parameter is false.
71
     */
72
    public function orWhereJsonContains(
73
        string $column,
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

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

73
        /** @scrutinizer ignore-unused */ string $column,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
        mixed $value,
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

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

74
        /** @scrutinizer ignore-unused */ mixed $value,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
        bool $encode = true,
0 ignored issues
show
Unused Code introduced by
The parameter $encode is not used and could be removed. ( Ignorable by Annotation )

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

75
        /** @scrutinizer ignore-unused */ bool $encode = true,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
        bool $validate = true
0 ignored issues
show
Unused Code introduced by
The parameter $validate is not used and could be removed. ( Ignorable by Annotation )

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

76
        /** @scrutinizer ignore-unused */ bool $validate = true

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
    ): static {
78
        throw new DriverException('This database engine does not support JSON contains operations.');
79
    }
80
81
    /**
82
     * @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...
83
     * @param bool $encode Encode the value into JSON.
84
     * @param bool $validate Check that $value is a valid JSON string if the $encode parameter is false.
85
     */
86
    public function whereJsonDoesntContain(
87
        string $column,
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

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

87
        /** @scrutinizer ignore-unused */ string $column,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
        mixed $value,
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

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

88
        /** @scrutinizer ignore-unused */ mixed $value,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
        bool $encode = true,
0 ignored issues
show
Unused Code introduced by
The parameter $encode is not used and could be removed. ( Ignorable by Annotation )

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

89
        /** @scrutinizer ignore-unused */ bool $encode = true,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
        bool $validate = true
0 ignored issues
show
Unused Code introduced by
The parameter $validate is not used and could be removed. ( Ignorable by Annotation )

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

90
        /** @scrutinizer ignore-unused */ bool $validate = true

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
    ): static {
92
        throw new DriverException('This database engine does not support JSON contains operations.');
93
    }
94
95
    /**
96
     * @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...
97
     * @param bool $encode Encode the value into JSON.
98
     * @param bool $validate Check that $value is a valid JSON string if the $encode parameter is false.
99
     */
100
    public function orWhereJsonDoesntContain(
101
        string $column,
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

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

101
        /** @scrutinizer ignore-unused */ string $column,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
        mixed $value,
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

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

102
        /** @scrutinizer ignore-unused */ mixed $value,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
        bool $encode = true,
0 ignored issues
show
Unused Code introduced by
The parameter $encode is not used and could be removed. ( Ignorable by Annotation )

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

103
        /** @scrutinizer ignore-unused */ bool $encode = true,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
        bool $validate = true
0 ignored issues
show
Unused Code introduced by
The parameter $validate is not used and could be removed. ( Ignorable by Annotation )

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

104
        /** @scrutinizer ignore-unused */ bool $validate = true

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
    ): static {
106
        throw new DriverException('This database engine does not support JSON contains operations.');
107
    }
108
109
    /**
110
     * @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...
111
     */
112
    public function whereJsonContainsKey(string $column): static
113
    {
114
        $this->registerToken(
115
            'AND',
116
            [new CompileJsonContainsKey($column)],
117
            $this->whereTokens,
118
            $this->whereWrapper()
119
        );
120
121
        return $this;
122
    }
123
124
    /**
125
     * @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...
126
     */
127
    public function orWhereJsonContainsKey(string $column): static
128
    {
129
        $this->registerToken(
130
            'OR',
131
            [new CompileJsonContainsKey($column)],
132
            $this->whereTokens,
133
            $this->whereWrapper()
134
        );
135
136
        return $this;
137
    }
138
139
    /**
140
     * @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...
141
     */
142
    public function whereJsonDoesntContainKey(string $column): static
143
    {
144
        $this->registerToken(
145
            'AND',
146
            [new CompileJsonDoesntContainKey($column)],
147
            $this->whereTokens,
148
            $this->whereWrapper()
149
        );
150
151
        return $this;
152
    }
153
154
    /**
155
     * @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...
156
     */
157
    public function orWhereJsonDoesntContainKey(string $column): static
158
    {
159
        $this->registerToken(
160
            'OR',
161
            [new CompileJsonDoesntContainKey($column)],
162
            $this->whereTokens,
163
            $this->whereWrapper()
164
        );
165
166
        return $this;
167
    }
168
169
    /**
170
     * @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...
171
     * @param int<0, max> $length
172
     * @param non-empty-string $operator
173
     */
174
    public function whereJsonLength(string $column, int $length, string $operator = '='): static
175
    {
176
        $this->registerToken(
177
            'AND',
178
            [new CompileJsonLength($column, $length, $operator)],
179
            $this->whereTokens,
180
            $this->whereWrapper()
181
        );
182
183
        return $this;
184
    }
185
186
    /**
187
     * @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...
188
     * @param int<0, max> $length
189
     * @param non-empty-string $operator
190
     */
191
    public function orWhereJsonLength(string $column, int $length, string $operator = '='): static
192
    {
193
        $this->registerToken(
194
            'OR',
195
            [new CompileJsonLength($column, $length, $operator)],
196
            $this->whereTokens,
197
            $this->whereWrapper()
198
        );
199
200
        return $this;
201
    }
202
}
203