Passed
Push — master ( 749afb...ac7253 )
by Wilmer
02:39 queued 51s
created

InConditionBuilder::buildCompositeInCondition()   B

Complexity

Conditions 10
Paths 18

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 10.0296

Importance

Changes 0
Metric Value
cc 10
eloc 15
nc 18
nop 4
dl 0
loc 25
ccs 14
cts 15
cp 0.9333
crap 10.0296
rs 7.6666
c 0
b 0
f 0

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