Passed
Branch dev (ea35c5)
by Wilmer
17:29 queued 12:43
created

InConditionBuilder::buildSubqueryInCondition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 11
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 4
crap 2.0625
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite\Builder;
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\Builder\InConditionBuilder as BaseInConditionBuilder;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\Conditi...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...
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 str_contains;
19
20
final class InConditionBuilder extends BaseInConditionBuilder
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 SQLite.');
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|Traversable $values
58
     * @param array $params
59
     *
60
     * @return string SQL.
61
     */
62 4
    protected function buildCompositeInCondition(
63
        ?string $operator,
64
        Traversable|array $columns,
65
        Traversable|array $values,
66
        array &$params = []
67
    ): string {
68 4
        $quotedColumns = [];
69
70
        /** @psalm-var string[]|Traversable $columns */
71 4
        foreach ($columns as $i => $column) {
72 4
            $quotedColumns[$i] = !str_contains($column, '(')
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