Passed
Push — master ( ea33ba...e9743d )
by Alexander
01:53
created

InConditionBuilder::buildCompositeInCondition()   B

Complexity

Conditions 10
Paths 18

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 10.0244

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 28
ccs 15
cts 16
cp 0.9375
rs 7.6666
cc 10
nc 18
nop 4
crap 10.0244

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite\Condition;
6
7
use Traversable;
8
use Yiisoft\Db\Exception\Exception;
9
use Yiisoft\Db\Exception\InvalidArgumentException;
10
use Yiisoft\Db\Exception\InvalidConfigException;
11
use Yiisoft\Db\Exception\NotSupportedException;
12
use Yiisoft\Db\Query\Query;
13
use Yiisoft\Db\Query\Conditions\InConditionBuilder as BaseInConditionBuilder;
14
use Yiisoft\Db\Sqlite\Connection;
15
16
use function implode;
17
use function is_array;
18
use function strpos;
19
20
final class InConditionBuilder extends BaseInConditionBuilder
21
{
22
    /**
23
     * Builds SQL for IN condition.
24
     *
25
     * @param string $operator
26
     * @param array|string $columns
27
     * @param Query $values
28
     * @param array $params
29
     *
30
     * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException
31
     *
32
     * @return string SQL.
33
     */
34 2
    protected function buildSubqueryInCondition(string $operator, $columns, Query $values, array &$params = []): string
35
    {
36 2
        if (is_array($columns)) {
37
            throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
38
        }
39
40 2
        return parent::buildSubqueryInCondition($operator, $columns, $values, $params);
41
    }
42
43
    /**
44
     * Builds SQL for IN condition.
45
     *
46
     * @param string|null $operator
47
     * @param array|Traversable $columns
48
     * @param iterable|array $values
49
     * @param array $params
50
     *
51
     * @return string SQL.
52
     */
53 4
    protected function buildCompositeInCondition(?string $operator, $columns, $values, array &$params = []): string
54
    {
55
        /** @var Connection $db */
56 4
        $db = $this->queryBuilder->getDb();
57
58 4
        $quotedColumns = [];
59
60 4
        foreach ($columns as $i => $column) {
61 4
            $quotedColumns[$i] = strpos($column, '(') === false
62 4
                ? $db->quoteColumnName($column) : $column;
63
        }
64
65 4
        $vss = [];
66
67 4
        foreach ($values as $value) {
68 4
            $vs = [];
69 4
            foreach ($columns as $i => $column) {
70 4
                if (isset($value[$column])) {
71 4
                    $phName = $this->queryBuilder->bindParam($value[$column], $params);
72 4
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' = ' : ' != ') . $phName;
73
                } else {
74
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' IS' : ' IS NOT') . ' NULL';
75
                }
76
            }
77 4
            $vss[] = '(' . implode($operator === 'IN' ? ' AND ' : ' OR ', $vs) . ')';
78
        }
79
80 4
        return '(' . implode($operator === 'IN' ? ' OR ' : ' AND ', $vss) . ')';
81
    }
82
}
83