Passed
Pull Request — master (#14)
by Wilmer
15:58 queued 01:00
created

InConditionBuilder::buildCompositeInCondition()   B

Complexity

Conditions 10
Paths 18

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 14
nc 18
nop 4
dl 0
loc 21
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\Mssql\Condition;
6
7
use Yiisoft\Db\Exception\NotSupportedException;
8
9
final class InConditionBuilder extends Yiisoft\Db\Query\Conditions\InConditionBuilder
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Mssql\Conditi...ions\InConditionBuilder was not found. Did you mean Yiisoft\Db\Query\Conditions\InConditionBuilder? If so, make sure to prefix the type with \.
Loading history...
10
{
11
    protected function buildSubqueryInCondition($operator, $columns, $values, &$params)
12
    {
13
        if (is_array($columns)) {
14
            throw new NotSupportedException(__METHOD__ . ' is not supported by MSSQL.');
15
        }
16
17
        return parent::buildSubqueryInCondition($operator, $columns, $values, $params);
18
    }
19
20
    protected function buildCompositeInCondition($operator, $columns, $values, &$params)
21
    {
22
        $quotedColumns = [];
23
        foreach ($columns as $i => $column) {
24
            $quotedColumns[$i] = strpos($column, '(') === false ? $this->queryBuilder->db->quoteColumnName($column) : $column;
25
        }
26
        $vss = [];
27
        foreach ($values as $value) {
28
            $vs = [];
29
            foreach ($columns as $i => $column) {
30
                if (isset($value[$column])) {
31
                    $phName = $this->queryBuilder->bindParam($value[$column], $params);
32
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' = ' : ' != ') . $phName;
33
                } else {
34
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' IS' : ' IS NOT') . ' NULL';
35
                }
36
            }
37
            $vss[] = '(' . implode($operator === 'IN' ? ' AND ' : ' OR ', $vs) . ')';
38
        }
39
40
        return '(' . implode($operator === 'IN' ? ' OR ' : ' AND ', $vss) . ')';
41
    }
42
}
43