Passed
Push — master ( 9b125d...f5b0c0 )
by Alexander
11:25 queued 07:35
created

InConditionBuilder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 59
ccs 19
cts 21
cp 0.9048
rs 10
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B buildCompositeInCondition() 0 31 10
A buildSubqueryInCondition() 0 11 2
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