Passed
Push — master ( fd9a54...1b375d )
by Alexander
06:02 queued 01:51
created

JsonExpressionBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite\Builder;
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\ExpressionInterface;
14
use Yiisoft\Db\Expression\JsonExpression;
15
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
16
use Yiisoft\Db\Query\QueryInterface;
17
use Yiisoft\Json\Json;
18
19
/**
20
 * Builds expressions for {@see `Yiisoft\Db\Expression\JsonExpression`} for SQLite.
21
 */
22
final class JsonExpressionBuilder implements ExpressionBuilderInterface
23
{
24 13
    public function __construct(private QueryBuilderInterface $queryBuilder)
25
    {
26 13
    }
27
28
    /**
29
     * The method builds the raw SQL from the `$expression` that won't be additionally escaped or quoted.
30
     *
31
     * @param JsonExpression $expression The expression to build.
32
     * @param array $params The binding parameters.
33
     *
34
     * @throws Exception
35
     * @throws InvalidArgumentException
36
     * @throws InvalidConfigException
37
     * @throws JsonException
38
     * @throws NotSupportedException
39
     *
40
     * @return string The raw SQL that won't be additionally escaped or quoted.
41
     */
42 13
    public function build(ExpressionInterface $expression, array &$params = []): string
43
    {
44
        /** @psalm-var mixed $value */
45 13
        $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\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

45
        /** @scrutinizer ignore-call */ 
46
        $value = $expression->getValue();
Loading history...
46
47 13
        if ($value instanceof QueryInterface) {
48 2
            [$sql, $params] = $this->queryBuilder->build($value, $params);
49
50 2
            return "($sql)";
51
        }
52
53 11
        return $this->queryBuilder->bindParam(Json::encode($value), $params);
54
    }
55
}
56