Completed
Pull Request — 2.1 (#15718)
by Alex
17:00
created

InConditionBuilder::buildCompositeInCondition()   C

Complexity

Conditions 10
Paths 18

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 21
cp 0
rs 6.1368
c 0
b 0
f 0
cc 10
eloc 15
nc 18
nop 4
crap 110

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
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\db\mssql\conditions;
9
10
use yii\base\NotSupportedException;
11
12
/**
13
 * {@inheritdoc}
14
 *
15
 * @author Dmytro Naumenko <[email protected]>
16
 * @since 2.0.14
17
 */
18
class InConditionBuilder extends \yii\db\conditions\InConditionBuilder
19
{
20
    /**
21
     * {@inheritdoc}
22
     * @throws NotSupportedException if `$columns` is an array
23
     */
24
    protected function buildSubqueryInCondition($operator, $columns, $values, &$params)
25
    {
26
        if (is_array($columns)) {
27
            throw new NotSupportedException(__METHOD__ . ' is not supported by MSSQL.');
28
        }
29
30
        return parent::buildSubqueryInCondition($operator, $columns, $values, $params);
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function buildCompositeInCondition($operator, $columns, $values, &$params)
37
    {
38
        $quotedColumns = [];
39
        foreach ($columns as $i => $column) {
40
            $quotedColumns[$i] = strpos($column, '(') === false ? $this->queryBuilder->db->quoteColumnName($column) : $column;
41
        }
42
        $vss = [];
43
        foreach ($values as $value) {
44
            $vs = [];
45
            foreach ($columns as $i => $column) {
46
                if (isset($value[$column])) {
47
                    $phName = $this->queryBuilder->bindParam($value[$column], $params);
48
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' = ' : ' != ') . $phName;
49
                } else {
50
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' IS' : ' IS NOT') . ' NULL';
51
                }
52
            }
53
            $vss[] = '(' . implode($operator === 'IN' ? ' AND ' : ' OR ', $vs) . ')';
54
        }
55
56
        return '(' . implode($operator === 'IN' ? ' OR ' : ' AND ', $vss) . ')';
57
    }
58
}
59