Passed
Push — master ( 9b125d...f5b0c0 )
by Alexander
11:25 queued 07:35
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\Builder;
6
7
use Iterator;
8
use Traversable;
9
use Yiisoft\Db\Exception\Exception;
10
use Yiisoft\Db\Exception\InvalidArgumentException;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\Exception\NotSupportedException;
13
use Yiisoft\Db\Expression\ExpressionInterface;
14
use Yiisoft\Db\QueryBuilder\Conditions\Builder\InConditionBuilder as AbstractInConditionBuilder;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\QueryBuilder\...lder\InConditionBuilder 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
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
16
17
use function implode;
18
use function is_array;
19
use function str_contains;
20
21
final class InConditionBuilder extends AbstractInConditionBuilder
22
{
23 30
    public function __construct(private QueryBuilderInterface $queryBuilder)
24
    {
25 30
        parent::__construct($queryBuilder);
26
    }
27
28
    /**
29
     * Builds SQL for IN condition.
30
     *
31
     * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException
32
     */
33 2
    protected function buildSubqueryInCondition(
34
        string $operator,
35
        iterable|string|Iterator $columns,
36
        ExpressionInterface $values,
37
        array &$params = []
38
    ): string {
39 2
        if (is_array($columns)) {
40
            throw new NotSupportedException(__METHOD__ . ' is not supported by MSSQL.');
41
        }
42
43 2
        return parent::buildSubqueryInCondition($operator, $columns, $values, $params);
44
    }
45
46
    /**
47
     * Builds SQL for IN condition.
48
     */
49 4
    protected function buildCompositeInCondition(
50
        ?string $operator,
51
        array|Traversable $columns,
52
        iterable|Iterator $values,
53
        array &$params = []
54
    ): string {
55 4
        $quotedColumns = [];
56
57
        /** @psalm-var string[] $columns */
58 4
        foreach ($columns as $i => $column) {
59 4
            $quotedColumns[$i] = !str_contains($column, '(')
60 4
                ? $this->queryBuilder->quoter()->quoteColumnName($column) : $column;
61
        }
62
63 4
        $vss = [];
64
65
        /** @psalm-var string[][] $values */
66 4
        foreach ($values as $value) {
67 4
            $vs = [];
68 4
            foreach ($columns as $i => $column) {
69 4
                if (isset($value[$column])) {
70 4
                    $phName = $this->queryBuilder->bindParam($value[$column], $params);
71 4
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' = ' : ' != ') . $phName;
72
                } else {
73
                    $vs[] = $quotedColumns[$i] . ($operator === 'IN' ? ' IS' : ' IS NOT') . ' NULL';
74
                }
75
            }
76 4
            $vss[] = '(' . implode($operator === 'IN' ? ' AND ' : ' OR ', $vs) . ')';
77
        }
78
79 4
        return '(' . implode($operator === 'IN' ? ' OR ' : ' AND ', $vss) . ')';
80
    }
81
}
82