Passed
Push — master ( 730f0e...9fbdc9 )
by Alexander
19:56 queued 02:18
created

InConditionBuilder::buildCompositeInCondition()   C

Complexity

Conditions 12
Paths 50

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 18.174

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 12
eloc 18
c 1
b 1
f 0
nc 50
nop 4
dl 0
loc 27
ccs 13
cts 20
cp 0.65
crap 18.174
rs 6.9666

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