Passed
Pull Request — 2.2 (#20357)
by Wilmer
13:33 queued 05:55
created

JsonExpressionBuilder::getTypecast()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @link https://www.yiiframework.com/
5
 * @copyright Copyright (c) 2008 Yii Software LLC
6
 * @license https://www.yiiframework.com/license/
7
 */
8
9
namespace yii\db\pgsql;
10
11
use yii\db\ArrayExpression;
12
use yii\db\ExpressionBuilderInterface;
13
use yii\db\ExpressionBuilderTrait;
14
use yii\db\ExpressionInterface;
15
use yii\db\JsonExpression;
16
use yii\db\Query;
17
use yii\helpers\Json;
18
19
/**
20
 * Class JsonExpressionBuilder builds [[JsonExpression]] for PostgreSQL DBMS.
21
 *
22
 * @author Dmytro Naumenko <[email protected]>
23
 * @since 2.0.14
24
 */
25
class JsonExpressionBuilder implements ExpressionBuilderInterface
26
{
27
    use ExpressionBuilderTrait;
28
29
30
    /**
31
     * {@inheritdoc}
32
     * @param JsonExpression|ExpressionInterface $expression the expression to be built
33
     */
34
    public function build(ExpressionInterface $expression, array &$params = [])
35
    {
36
        $value = $expression->getValue();
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on yii\db\ExpressionInterface. It seems like you code against a sub-type of yii\db\ExpressionInterface such as yii\db\JsonExpression or yii\db\Query or yii\db\ArrayExpression or yii\db\PdoValue or yii\db\Expression or yii\db\conditions\BetweenColumnsCondition or yii\db\conditions\SimpleCondition. ( Ignorable by Annotation )

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

36
        /** @scrutinizer ignore-call */ 
37
        $value = $expression->getValue();
Loading history...
37
38
        if ($value instanceof Query) {
39
            list ($sql, $params) = $this->queryBuilder->build($value, $params);
40
            return "($sql)" . $this->getTypecast($expression);
41
        }
42
        if ($value instanceof ArrayExpression) {
43
            $placeholder = 'array_to_json(' . $this->queryBuilder->buildExpression($value, $params) . ')';
44
        } else {
45
            $placeholder = $this->queryBuilder->bindParam(Json::encode($value), $params);
46
        }
47
48
        return $placeholder . $this->getTypecast($expression);
49
    }
50
51
    /**
52
     * @param JsonExpression $expression
53
     * @return string the typecast expression based on [[type]].
54
     */
55
    protected function getTypecast(JsonExpression $expression)
56
    {
57
        if ($expression->getType() === null) {
58
            return '';
59
        }
60
61
        return '::' . $expression->getType();
62
    }
63
}
64