Passed
Push — 2.2 ( c9917f...37714b )
by Alexander
02:01 queued 17s
created

JsonExpressionBuilder   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 24
ccs 6
cts 8
cp 0.75
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 13 2
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\db\mysql;
9
10
use yii\db\ExpressionBuilderInterface;
11
use yii\db\ExpressionBuilderTrait;
12
use yii\db\ExpressionInterface;
13
use yii\db\JsonExpression;
14
use yii\db\Query;
15
use yii\helpers\Json;
16
17
/**
18
 * Class JsonExpressionBuilder builds [[JsonExpression]] for MySQL DBMS.
19
 *
20
 * @author Dmytro Naumenko <[email protected]>
21
 * @since 2.0.14
22
 */
23
class JsonExpressionBuilder implements ExpressionBuilderInterface
24
{
25
    use ExpressionBuilderTrait;
26
27
    const PARAM_PREFIX = ':qp';
28
29
30
    /**
31
     * {@inheritdoc}
32
     * @param JsonExpression|ExpressionInterface $expression the expression to be built
33
     */
34 5
    public function build(ExpressionInterface $expression, array &$params = [])
35
    {
36 5
        $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 5
        if ($value instanceof Query) {
39
            list ($sql, $params) = $this->queryBuilder->build($value, $params);
40
            return "($sql)";
41
        }
42
43 5
        $placeholder = static::PARAM_PREFIX . count($params);
44 5
        $params[$placeholder] = Json::encode($value);
45
46 5
        return "CAST($placeholder AS JSON)";
47
    }
48
}
49