Passed
Push — master ( 1f0d9b...6a4b0f )
by Wilmer
12:11 queued 10:38
created

JsonExpressionBuilder::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql\Expression;
6
7
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
8
use Yiisoft\Db\Expression\ExpressionBuilderTrait;
9
use Yiisoft\Db\Expression\ExpressionInterface;
10
use Yiisoft\Db\Expression\JsonExpression;
11
use Yiisoft\Db\Query\Query;
12
use Yiisoft\Json\Json;
13
14
/**
15
 * Class JsonExpressionBuilder builds {@see JsonExpression} for MySQL DBMS.
16
 */
17
class JsonExpressionBuilder implements ExpressionBuilderInterface
18
{
19
    use ExpressionBuilderTrait;
20
21
    public const PARAM_PREFIX = ':qp';
22
23
    /**
24
     * {@inheritdoc}
25
     *
26
     * @param JsonExpression|ExpressionInterface $expression the expression to be built
27
     */
28 11
    public function build(ExpressionInterface $expression, array &$params = []): string
29
    {
30 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\Pdo\PdoValue or Yiisoft\Db\Expression\ArrayExpression or Yiisoft\Db\Expression\JsonExpression or Yiisoft\Db\Query\Conditions\SimpleCondition or Yiisoft\Db\Query\Conditi...BetweenColumnsCondition. ( Ignorable by Annotation )

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

30
        /** @scrutinizer ignore-call */ 
31
        $value = $expression->getValue();
Loading history...
31
32 11
        if ($value instanceof Query) {
33 2
            list($sql, $params) = $this->queryBuilder->build($value, $params);
34
35 2
            return "($sql)";
36
        }
37
38 9
        $placeholder = static::PARAM_PREFIX . count($params);
39 9
        $params[$placeholder] = Json::encode($value);
40
41 9
        return "CAST($placeholder AS JSON)";
42
    }
43
}
44