Test Failed
Pull Request — master (#216)
by Alexander
06:50 queued 02:42
created

InConditionBuilder::buildCompositeInCondition()   B

Complexity

Conditions 11
Paths 24

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 18
c 1
b 0
f 0
nc 24
nop 4
dl 0
loc 36
ccs 17
cts 17
cp 1
crap 11
rs 7.3166

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\Mssql\Builder;
6
7
use Iterator;
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\Expression\ExpressionInterface;
13
use Yiisoft\Db\QueryBuilder\Condition\Builder\InConditionBuilder as AbstractInConditionBuilder;
14
15
use function implode;
16
use function is_array;
17
use function str_contains;
18
19
/**
20
 * InConditionBuilder builds conditions for {@see `\Yiisoft\Db\QueryBuilder\Condition\InCondition`} IN operator for
21
 * MSSQL Server.
22
 */
23 37
final class InConditionBuilder extends AbstractInConditionBuilder
24
{
25 37
    /**
26
     * Builds SQL for IN condition.
27
     *
28
     * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException
29
     */
30
    protected function buildSubqueryInCondition(
31
        string $operator,
32
        iterable|string|Iterator $columns,
33 3
        ExpressionInterface $values,
34
        array &$params = []
35
    ): string {
36
        if (is_array($columns)) {
37
            throw new NotSupportedException(__METHOD__ . ' is not supported by MSSQL.');
38
        }
39 3
40 1
        return parent::buildSubqueryInCondition($operator, $columns, $values, $params);
41
    }
42
43 2
    /**
44
     * Builds SQL for IN condition.
45
     *
46
     * @throws Exception
47
     * @throws InvalidArgumentException
48
     * @throws InvalidConfigException
49 7
     * @throws NotSupportedException
50
     */
51
    protected function buildCompositeInCondition(
52
        string|null $operator,
53
        iterable $columns,
54
        iterable|Iterator $values,
55 7
        array &$params = []
56
    ): string {
57
        $quotedColumns = [];
58 7
59 7
        /** @psalm-var string[] $columns */
60 1
        foreach ($columns as $i => $column) {
61 1
            if ($column instanceof ExpressionInterface) {
62
                $quotedColumns[$i] = $columns[$i] = $this->queryBuilder->buildExpression($column);
0 ignored issues
show
Bug introduced by
The property queryBuilder is declared private in Yiisoft\Db\QueryBuilder\...lder\InConditionBuilder and cannot be accessed from this context.
Loading history...
63
                continue;
64 6
            }
65 6
66
            $quotedColumns[$i] = !str_contains($column, '(')
67
                ? $this->queryBuilder->quoter()->quoteColumnName($column) : $column;
68 7
        }
69
70
        $vss = [];
71 7
72 6
        /** @psalm-var string[][] $values */
73 6
        foreach ($values as $value) {
74 6
            $vs = [];
75 6
            foreach ($columns as $i => $column) {
76 6
                if (isset($value[$column])) {
77
                    $phName = $this->queryBuilder->bindParam($value[$column], $params);
78 2
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' = ' : ' != ') . $phName;
79
                } else {
80
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' IS' : ' IS NOT') . ' NULL';
81 6
                }
82
            }
83
            $vss[] = '(' . implode($operator === 'IN' ? ' AND ' : ' OR ', $vs) . ')';
84 7
        }
85
86
        return '(' . implode($operator === 'IN' ? ' OR ' : ' AND ', $vss) . ')';
87
    }
88
}
89