Passed
Pull Request — master (#303)
by
unknown
03:47
created

CompositeExpressionBuilder::buildPlaceholders()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 38
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.2694

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 38
ccs 14
cts 17
cp 0.8235
crap 7.2694
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql\Builder;
6
7
use Yiisoft\Db\Exception\Exception;
8
use Yiisoft\Db\Exception\InvalidArgumentException;
9
use Yiisoft\Db\Exception\InvalidConfigException;
10
use Yiisoft\Db\Exception\NotSupportedException;
11
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
12
use Yiisoft\Db\Expression\ExpressionInterface;
13
use Yiisoft\Db\Pgsql\CompositeExpression;
14
use Yiisoft\Db\Query\QueryInterface;
15
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
16
17
use function implode;
18
19
/**
20
 * Builds expressions for {@see CompositeExpression} for PostgreSQL Server.
21
 */
22
final class CompositeExpressionBuilder implements ExpressionBuilderInterface
23
{
24 1
    public function __construct(private QueryBuilderInterface $queryBuilder)
25
    {
26 1
    }
27
28
    /**
29
     * The Method builds the raw SQL from the expression that won't be additionally escaped or quoted.
30
     *
31
     * @param ExpressionInterface $expression The expression build.
32
     * @param array $params The binding parameters.
33
     *
34
     * @throws Exception
35
     * @throws InvalidArgumentException
36
     * @throws InvalidConfigException
37
     * @throws NotSupportedException
38
     *
39
     * @return string The raw SQL that won't be additionally escaped or quoted.
40
     *
41
     * @psalm-param CompositeExpression $expression
42
     */
43 1
    public function build(ExpressionInterface $expression, array &$params = []): string
44
    {
45
        /** @psalm-var mixed $value */
46 1
        $value = $expression->getValue();
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on Yiisoft\Db\Expression\ExpressionInterface. It seems like you code against a sub-type of Yiisoft\Db\Expression\ExpressionInterface such as Yiisoft\Db\Pgsql\CompositeExpression or Yiisoft\Db\Expression\ArrayExpression or Yiisoft\Db\Expression\JsonExpression or Yiisoft\Db\Command\Param or Yiisoft\Db\QueryBuilder\...lumnsConditionInterface or Yiisoft\Db\QueryBuilder\...impleConditionInterface or Yiisoft\Db\QueryBuilder\...\LikeConditionInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        /** @scrutinizer ignore-call */ 
47
        $value = $expression->getValue();
Loading history...
47
48 1
        if (empty($value)) {
49
            return 'NULL';
50
        }
51
52 1
        if ($value instanceof QueryInterface) {
53
            [$sql, $params] = $this->queryBuilder->build($value, $params);
54
            return "($sql)" . $this->getTypeHint($expression);
55
        }
56
57
        /** @psalm-var string[] $placeholders */
58 1
        $placeholders = $this->buildPlaceholders($expression, $params);
59
60 1
        if (empty($placeholders)) {
61
            return 'NULL';
62
        }
63
64 1
        return 'ROW(' . implode(', ', $placeholders) . ')' . $this->getTypeHint($expression);
65
    }
66
67
    /**
68
     * Builds a placeholder array out of $expression values.
69
     *
70
     * @param array $params The binding parameters.
71
     *
72
     * @throws Exception
73
     * @throws InvalidArgumentException
74
     * @throws InvalidConfigException
75
     * @throws NotSupportedException
76
     *
77
     * @psalm-param CompositeExpression $expression
78
     */
79 1
    private function buildPlaceholders(ExpressionInterface $expression, array &$params): array
80
    {
81 1
        $placeholders = [];
82
83
        /** @psalm-var mixed $value */
84 1
        $value = $expression->getNormalizedValue();
0 ignored issues
show
Bug introduced by
The method getNormalizedValue() does not exist on Yiisoft\Db\Expression\ExpressionInterface. It seems like you code against a sub-type of Yiisoft\Db\Expression\ExpressionInterface such as Yiisoft\Db\Pgsql\CompositeExpression. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        /** @scrutinizer ignore-call */ 
85
        $value = $expression->getNormalizedValue();
Loading history...
85
86 1
        if (!is_iterable($value)) {
87
            return $placeholders;
88
        }
89
90 1
        $columns = (array) $expression->getColumns();
0 ignored issues
show
Bug introduced by
The method getColumns() does not exist on Yiisoft\Db\Expression\ExpressionInterface. It seems like you code against a sub-type of Yiisoft\Db\Expression\ExpressionInterface such as Yiisoft\Db\Pgsql\CompositeExpression. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
        $columns = (array) $expression->/** @scrutinizer ignore-call */ getColumns();
Loading history...
91 1
        $columnNames = array_keys($columns);
92
93
        /**
94
         * @psalm-var int|string $columnName
95
         * @psalm-var mixed $item
96
         */
97 1
        foreach ($value as $columnName => $item) {
98 1
            if (is_int($columnName)) {
99
                $columnName = $columnNames[$columnName] ?? null;
100
            }
101
102 1
            if ($columnName === null || !isset($columns[$columnName])) {
103
                continue;
104
            }
105
106
            /** @psalm-var mixed $item */
107 1
            $item = $columns[$columnName]->dbTypecast($item);
108
109 1
            if ($item instanceof ExpressionInterface) {
110 1
                $placeholders[] = $this->queryBuilder->buildExpression($item, $params);
111
            } else {
112 1
                $placeholders[] = $this->queryBuilder->bindParam($item, $params);
113
            }
114
        }
115
116 1
        return $placeholders;
117
    }
118
119
    /**
120
     * @return string The typecast expression based on {@see type}.
121
     *
122
     * @psalm-param CompositeExpression $expression
123
     */
124 1
    private function getTypeHint(ExpressionInterface $expression): string
125
    {
126 1
        $type = $expression->getType();
0 ignored issues
show
Bug introduced by
The method getType() does not exist on Yiisoft\Db\Expression\ExpressionInterface. It seems like you code against a sub-type of Yiisoft\Db\Expression\ExpressionInterface such as Yiisoft\Db\Pgsql\CompositeExpression or Yiisoft\Db\Expression\ArrayExpression or Yiisoft\Db\Expression\JsonExpression or Yiisoft\Db\Command\Param. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
        /** @scrutinizer ignore-call */ 
127
        $type = $expression->getType();
Loading history...
127
128 1
        if ($type === null) {
129
            return '';
130
        }
131
132 1
        return '::' . $type;
133
    }
134
}
135