Passed
Pull Request — master (#28)
by
unknown
06:43
created

InConditionBuilder::buildCompositeInCondition()   B

Complexity

Conditions 10
Paths 18

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 10.0244

Importance

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

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\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\Mssql\Connection;
13
use Yiisoft\Db\Query\Conditions\InConditionBuilder as AbstractInConditionBuilder;
14
use Yiisoft\Db\Query\Query;
15
16
use function implode;
17
use function is_array;
18
use function strpos;
19
20
final class InConditionBuilder extends AbstractInConditionBuilder
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 MSSQL.');
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 array|iterable $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 4
        foreach ($columns as $i => $column) {
60 4
            $quotedColumns[$i] = strpos($column, '(') === false
61 4
                ? $db->quoteColumnName($column) : $column;
62
        }
63
64 4
        $vss = [];
65 4
        foreach ($values as $value) {
66 4
            $vs = [];
67 4
            foreach ($columns as $i => $column) {
68 4
                if (isset($value[$column])) {
69 4
                    $phName = $this->queryBuilder->bindParam($value[$column], $params);
70 4
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' = ' : ' != ') . $phName;
71
                } else {
72
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' IS' : ' IS NOT') . ' NULL';
73
                }
74
            }
75 4
            $vss[] = '(' . implode($operator === 'IN' ? ' AND ' : ' OR ', $vs) . ')';
76
        }
77
78 4
        return '(' . implode($operator === 'IN' ? ' OR ' : ' AND ', $vss) . ')';
79
    }
80
}
81