Passed
Push — master ( c4f127...730a00 )
by Sergei
24:33 queued 20:27
created

CompositeExpressionBuilder::getTypeHint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
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\Composite\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 11
    public function __construct(private QueryBuilderInterface $queryBuilder)
25
    {
26 11
    }
27
28
    /**
29
     * The method builds the raw SQL from the expression that won't be additionally escaped or quoted.
30
     *
31
     * @param CompositeExpression $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 11
    public function build(ExpressionInterface $expression, array &$params = []): string
42
    {
43 11
        $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\Expression\ArrayExpression or Yiisoft\Db\Expression\JsonExpression or Yiisoft\Db\Command\Param or Yiisoft\Db\Pgsql\Composite\CompositeExpression 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

43
        /** @scrutinizer ignore-call */ 
44
        $value = $expression->getValue();
Loading history...
44
45 11
        if (empty($value)) {
46 1
            return 'NULL';
47
        }
48
49 10
        if ($value instanceof QueryInterface) {
50 2
            [$sql, $params] = $this->queryBuilder->build($value, $params);
51 2
            return "($sql)" . $this->getTypeHint($expression);
52
        }
53
54
        /** @psalm-var string[] $placeholders */
55 8
        $placeholders = $this->buildPlaceholders($expression, $params);
56
57 8
        if (empty($placeholders)) {
58 1
            return 'NULL';
59
        }
60
61 7
        return 'ROW(' . implode(', ', $placeholders) . ')' . $this->getTypeHint($expression);
62
    }
63
64
    /**
65
     * Builds a placeholder array out of $expression values.
66
     *
67
     * @param array $params The binding parameters.
68
     *
69
     * @throws Exception
70
     * @throws InvalidArgumentException
71
     * @throws InvalidConfigException
72
     * @throws NotSupportedException
73
     */
74 8
    private function buildPlaceholders(CompositeExpression $expression, array &$params): array
75
    {
76 8
        $value = $expression->getNormalizedValue();
77
78 8
        if (!is_iterable($value)) {
79 1
            return [];
80
        }
81
82 7
        $placeholders = [];
83 7
        $columns = $expression->getColumns();
84
85
        /** @psalm-var int|string $columnName */
86 7
        foreach ($value as $columnName => $item) {
87 7
            if (isset($columns[$columnName])) {
88 2
                $item = $columns[$columnName]->dbTypecast($item);
89
            }
90
91 7
            if ($item instanceof ExpressionInterface) {
92 1
                $placeholders[] = $this->queryBuilder->buildExpression($item, $params);
93
            } else {
94 7
                $placeholders[] = $this->queryBuilder->bindParam($item, $params);
95
            }
96
        }
97
98 7
        return $placeholders;
99
    }
100
101
    /**
102
     * @return string The typecast expression based on {@see type}.
103
     */
104 9
    private function getTypeHint(CompositeExpression $expression): string
105
    {
106 9
        $type = $expression->getType();
107
108 9
        if ($type === null) {
109 5
            return '';
110
        }
111
112 4
        return '::' . $type;
113
    }
114
}
115