Passed
Push — dev ( cc6591...c9f34d )
by Wilmer
04:23 queued 23s
created

InConditionBuilder::buildCompositeInCondition()   B

Complexity

Conditions 10
Paths 18

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 10.0296

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 15
c 1
b 0
f 0
nc 18
nop 4
dl 0
loc 31
ccs 14
cts 15
cp 0.9333
crap 10.0296
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\Expression\ExpressionInterface;
13
use Yiisoft\Db\Query\Conditions\InConditionBuilder as AbstractInConditionBuilder;
14
use Yiisoft\Db\Query\QueryBuilderInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\QueryBuilderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
use function implode;
17
use function is_array;
18
use function strpos;
19
20
final class InConditionBuilder extends AbstractInConditionBuilder
21
{
22 30
    public function __construct(private QueryBuilderInterface $queryBuilder)
23
    {
24 30
        parent::__construct($queryBuilder);
25
    }
26
27
    /**
28
     * Builds SQL for IN condition.
29
     *
30
     * @param string $operator
31
     * @param array|string $columns
32
     * @param ExpressionInterface $values
33
     * @param array $params
34
     *
35
     * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException
36
     *
37
     * @return string SQL
38
     */
39 2
    protected function buildSubqueryInCondition(
40
        string $operator,
41
        array|string $columns,
42
        ExpressionInterface $values,
43
        array &$params = []
44
    ): string {
45 2
        if (is_array($columns)) {
0 ignored issues
show
introduced by
The condition is_array($columns) is always true.
Loading history...
46
            throw new NotSupportedException(__METHOD__ . ' is not supported by MSSQL.');
47
        }
48
49 2
        return parent::buildSubqueryInCondition($operator, $columns, $values, $params);
50
    }
51
52
    /**
53
     * Builds SQL for IN condition.
54
     *
55
     * @param string|null $operator
56
     * @param array|Traversable $columns
57
     * @param array $values
58
     * @param array $params
59
     *
60
     * @return string SQL
61
     */
62 4
    protected function buildCompositeInCondition(
63
        ?string $operator,
64
        array|Traversable $columns,
65
        array|Traversable $values,
66
        array &$params = []
67
    ): string {
68 4
        $quotedColumns = [];
69
70
        /** @psalm-var string[] $columns */
71 4
        foreach ($columns as $i => $column) {
72 4
            $quotedColumns[$i] = strpos($column, '(') === false
73 4
                ? $this->queryBuilder->quoter()->quoteColumnName($column) : $column;
74
        }
75
76 4
        $vss = [];
77
78
        /** @psalm-var string[][] $values */
79 4
        foreach ($values as $value) {
80 4
            $vs = [];
81 4
            foreach ($columns as $i => $column) {
82 4
                if (isset($value[$column])) {
83 4
                    $phName = $this->queryBuilder->bindParam($value[$column], $params);
84 4
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' = ' : ' != ') . $phName;
85
                } else {
86
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' IS' : ' IS NOT') . ' NULL';
87
                }
88
            }
89 4
            $vss[] = '(' . implode($operator === 'IN' ? ' AND ' : ' OR ', $vs) . ')';
90
        }
91
92 4
        return '(' . implode($operator === 'IN' ? ' OR ' : ' AND ', $vss) . ')';
93
    }
94
}
95