Passed
Pull Request — master (#191)
by Def
11:32 queued 07:36
created

InConditionBuilder::buildCompositeInCondition()   C

Complexity

Conditions 12
Paths 40

Size

Total Lines 40
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 40
ccs 20
cts 20
cp 1
rs 6.9666
c 0
b 0
f 0
cc 12
nc 40
nop 4
crap 12

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\Builder;
6
7
use Iterator;
8
use Traversable;
9
use Yiisoft\Db\Exception\Exception;
10
use Yiisoft\Db\Exception\InvalidArgumentException;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\Exception\NotSupportedException;
13
use Yiisoft\Db\Expression\ExpressionInterface;
14
use Yiisoft\Db\QueryBuilder\Condition\Builder\InConditionBuilder as BaseInConditionBuilder;
15
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
16
17
use function implode;
18
use function is_array;
19
use function str_contains;
20
21
final class InConditionBuilder extends BaseInConditionBuilder
22
{
23 38
    public function __construct(private QueryBuilderInterface $queryBuilder)
24
    {
25 38
        parent::__construct($queryBuilder);
26
    }
27
28
    /**
29
     * Builds SQL for IN condition.
30
     *
31
     * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException
32
     *
33
     * @return string SQL.
34
     */
35 3
    protected function buildSubqueryInCondition(
36
        string $operator,
37
        iterable|string|Iterator $columns,
38
        ExpressionInterface $values,
39
        array &$params = []
40
    ): string {
41 3
        if (is_array($columns)) {
42 1
            throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
43
        }
44
45 2
        return parent::buildSubqueryInCondition($operator, $columns, $values, $params);
46
    }
47
48
    /**
49
     * Builds SQL for IN condition.
50
     *
51
     * @param iterable|Iterator $values
52
     *
53
     * @return string SQL.
54
     */
55 9
    protected function buildCompositeInCondition(
56
        string|null $operator,
57
        Traversable|array $columns,
58
        iterable|Iterator $values,
59
        array &$params = []
60
    ): string {
61 9
        $quotedColumns = [];
62
63
        /** @psalm-var string[]|Traversable $columns */
64 9
        foreach ($columns as $i => $column) {
65 9
            if ($column instanceof ExpressionInterface) {
66
                /** @psalm-suppress InvalidCast */
67 1
                $quotedColumns[$i] = (string) $column;
68 1
                continue;
69
            }
70 8
            $quotedColumns[$i] = !str_contains($column, '(')
71 8
                ? $this->queryBuilder->quoter()->quoteColumnName($column) : $column;
72
        }
73
74 9
        $vss = [];
75
76
        /** @psalm-var string[][] $values */
77 9
        foreach ($values as $value) {
78 9
            $vs = [];
79 9
            foreach ($columns as $i => $column) {
80 9
                if ($column instanceof ExpressionInterface) {
81
                    /** @psalm-suppress InvalidCast */
82 1
                    $column = (string)$column;
83
                }
84 9
                if (isset($value[$column])) {
85 9
                    $phName = $this->queryBuilder->bindParam($value[$column], $params);
86 9
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' = ' : ' != ') . $phName;
87
                } else {
88 3
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' IS' : ' IS NOT') . ' NULL';
89
                }
90
            }
91 9
            $vss[] = '(' . implode($operator === 'IN' ? ' AND ' : ' OR ', $vs) . ')';
92
        }
93
94 9
        return '(' . implode($operator === 'IN' ? ' OR ' : ' AND ', $vss) . ')';
95
    }
96
}
97