Passed
Pull Request — master (#40)
by Wilmer
12:50
created

JsonExpressionBuilder   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 32
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 14 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql;
6
7
use JsonException;
8
use Yiisoft\Db\Exception\Exception;
9
use Yiisoft\Db\Exception\InvalidArgumentException;
10
use Yiisoft\Db\Exception\InvalidConfigException;
11
use Yiisoft\Db\Exception\NotSupportedException;
12
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
13
use Yiisoft\Db\Expression\ExpressionBuilderTrait;
14
use Yiisoft\Db\Expression\ExpressionInterface;
15
use Yiisoft\Db\Expression\JsonExpression;
16
use Yiisoft\Db\Query\Query;
17
use Yiisoft\Json\Json;
18
19
use function count;
20
21
final class JsonExpressionBuilder implements ExpressionBuilderInterface
22
{
23
    use ExpressionBuilderTrait;
24
25
    public const PARAM_PREFIX = ':qp';
26
27
    /**
28
     * @param JsonExpression|ExpressionInterface $expression the expression to be built
29
     * @param array $params
30
     *
31
     * @throws JsonException
32
     * @throws Exception
33
     * @throws InvalidArgumentException
34
     * @throws InvalidConfigException
35
     * @throws NotSupportedException
36
     *
37
     * @return string
38
     */
39
    public function build(ExpressionInterface $expression, array &$params = []): string
40
    {
41
        $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

41
        /** @scrutinizer ignore-call */ 
42
        $value = $expression->getValue();
Loading history...
42
43
        if ($value instanceof Query) {
44
            [$sql, $params] = $this->queryBuilder->build($value, $params);
45
46
            return "($sql)";
47
        }
48
49
        $placeholder = static::PARAM_PREFIX . count($params);
50
        $params[$placeholder] = Json::encode($value);
51
52
        return "CAST($placeholder AS JSON)";
53
    }
54
}
55