Passed
Pull Request — master (#806)
by Sergei
02:15
created

ExpressionBuilder::getPattern()   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
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Expression;
6
7
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
8
9
use function array_intersect_key;
10
use function array_merge;
11
use function preg_quote;
12
use function preg_replace;
13
use function str_starts_with;
14
15
/**
16
 * It's used to build expressions for use in database queries.
17
 *
18
 * It provides a methods {@see build()} for creating various types of expressions, such as conditions, joins, and
19 333
 * ordering clauses.
20
 *
21 333
 * These expressions can be used with the query builder to build complex and customizable database queries
22
 * {@see Expression} class.
23 333
 */
24
class ExpressionBuilder implements ExpressionBuilderInterface
25
{
26
    public function __construct(private QueryBuilderInterface|null $queryBuilder = null)
27
    {
28
    }
29
30
    public function build(Expression $expression, array &$params = []): string
31
    {
32
        $sql = $expression->__toString();
33
        $expressionParams = $expression->getParams();
34
35
        if (empty($expressionParams)) {
36
            return $sql;
37
        }
38
39
        if ($this->queryBuilder === null || isset($params[0]) || isset($expressionParams[0])) {
40
            $params = array_merge($params, $expressionParams);
41
            return $sql;
42
        }
43
44
        $sql = $this->appendParams($sql, $expressionParams, $params);
45
46
        return $this->replaceParamExpressions($sql, $expressionParams, $params);
47
    }
48
49
    private function appendParams(string $sql, array &$expressionParams, array &$params): string
50
    {
51
        $nonUniqueParams = array_intersect_key($expressionParams, $params);
52
        $params += $expressionParams;
53
54
        /** @var string $name */
55
        foreach ($nonUniqueParams as $name => $value) {
56
            $pattern = $this->getPattern($name);
57
            $uniqueName = $this->getUniqueName($name, $params);
58
59
            $replacement = !str_starts_with($uniqueName, ':') ? ":$uniqueName" : $uniqueName;
60
61
            $sql = preg_replace($pattern, $replacement, $sql, 1);
62
63
            $params[$uniqueName] = $value;
64
            $expressionParams[$uniqueName] = $value;
65
            unset($expressionParams[$name]);
66
        }
67
68
        return $sql;
69
    }
70
71
    private function replaceParamExpressions(string $sql, array $expressionParams, array &$params): string
72
    {
73
        /** @var string $name */
74
        foreach ($expressionParams as $name => $value) {
75
            if (!$value instanceof ExpressionInterface) {
76
                continue;
77
            }
78
79
            $pattern = $this->getPattern($name);
80
            /** @psalm-suppress PossiblyNullReference */
81
            $replacement = $this->queryBuilder->buildExpression($value, $params);
0 ignored issues
show
Bug introduced by
The method buildExpression() does not exist on null. ( Ignorable by Annotation )

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

81
            /** @scrutinizer ignore-call */ 
82
            $replacement = $this->queryBuilder->buildExpression($value, $params);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
83
            $sql = preg_replace($pattern, $replacement, $sql, 1);
84
85
            unset($params[$name]);
86
        }
87
88
        return $sql;
89
    }
90
91
    /** @psalm-return non-empty-string */
92
    private function getPattern(string $name): string
93
    {
94
        if (!str_starts_with($name, ':')) {
95
            $name = ":$name";
96
        }
97
98
        return '/' . preg_quote($name, '/') . '\b/';
99
    }
100
101
    private function getUniqueName(string $name, array $params): string
102
    {
103
        $uniqueName = $name . '_0';
104
105
        for ($i = 1; isset($params[$uniqueName]); ++$i) {
106
            $uniqueName = $name . '_' . $i;
107
        }
108
109
        return $uniqueName;
110
    }
111
}
112