Issues (33)

src/Expression/Expression.php (2 issues)

1
<?php
2
3
namespace Tsukasa\QueryBuilder\Expression;
4
5
use Tsukasa\QueryBuilder\Exception\QBException;
6
7
class Expression extends AbstractExpression
8
{
9
    protected $expression;
10
    protected $params = [];
11
12
    public function __construct($expression, array $params = [])
13
    {
14
        $this->params = $params;
15
        $this->expression = $expression;
16
    }
17
18
    public function toSQL()
19
    {
20
        if ($this->expression) {
21
            return $this->generateSql() ?: '';
22
        }
23
24
        return '';
25
    }
26
27
    protected function generateSql()
28
    {
29
        $sql = $this->expression;
30
31
        if (strpos($sql, '{') !== false)
32
        {
33
            $tableAlias = $this->qb->getAlias();
34
35
            $sql = preg_replace_callback('~\{([^\}]+)\}~', function($matches) use ($tableAlias) {
36
                $rawColumn = $matches[1];
37
38
                list(, $column) = $this->qb->getLookupBuilder()->parseLookup($this->qb, $rawColumn, null);
39
                $column = $this->qb->getLookupBuilder()->fetchColumnName($column);
0 ignored issues
show
The method fetchColumnName() does not exist on Tsukasa\QueryBuilder\Interfaces\ILookupBuilder. Since it exists in all sub-types, consider adding an abstract or default implementation to Tsukasa\QueryBuilder\Interfaces\ILookupBuilder. ( Ignorable by Annotation )

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

39
                $column = $this->qb->getLookupBuilder()->/** @scrutinizer ignore-call */ fetchColumnName($column);
Loading history...
40
41
                if ($tableAlias !== null && strpos($column, '.') === false) {
42
                    $column = $tableAlias . '.' . $column;
43
                }
44
45
                return $this->qb->getAdapter()->buildColumns($column);
46
47
            }, $sql);
48
        }
49
50
        if (strpos($sql, '?'))
51
        {
52
            if (mb_substr_count($sql, '?') !== count($this->params)) {
53
                throw new QBException('Incorrect parameters count in Expression: "' . addslashes($this->expression) . '"');
54
            }
55
56
            $sql = preg_replace_callback('~\?~', function($matches) {
0 ignored issues
show
The parameter $matches is not used and could be removed. ( Ignorable by Annotation )

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

56
            $sql = preg_replace_callback('~\?~', function(/** @scrutinizer ignore-unused */ $matches) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
                return $this->qb->getAdapter()->quoteValue(
58
                    $this->getNextParam()
59
                );
60
            }, $sql);
61
62
        }
63
64
        return $sql;
65
    }
66
67
    protected function getNextParam()
68
    {
69
        static $params;
70
71
        if ($params === null) {
72
            $params = $this->params;
73
            reset($params);
74
        }
75
76
        $value = current($params);
77
78
        if (next($params) === null) {
79
            reset($params);
80
        }
81
82
        return $value;
83
    }
84
}