Completed
Push — master ( b794f5...f9eb18 )
by Andrii
03:04
created

CallExpression::buildUsing()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 30
Code Lines 19

Duplication

Lines 5
Ratio 16.67 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 5
loc 30
ccs 0
cts 24
cp 0
rs 6.7272
cc 7
eloc 19
nc 10
nop 2
crap 56
1
<?php
2
3
namespace hiapi\db;
4
5
use yii\db\Expression;
6
use yii\db\Query;
7
use yii\db\QueryBuilder;
8
use yii\db\QueryInterface;
9
10
/**
11
 * CallExpression represents a SQL function call expression.
12
 *
13
 * @author Andrii Vasyliev <[email protected]>
14
 */
15
class CallExpression implements ExpressionInterface
16
{
17
    const PARAM_PREFIX = ':cxp';
18
19
    /**
20
     * @var string function name
21
     */
22
    protected $name;
23
24
    /**
25
     * @var array array of function arguments.
26
     */
27
    protected $args;
28
29
    /**
30
     * CallExpression constructor.
31
     */
32
    public function __construct($name, $args = [])
33
    {
34
        $this->name = $name;
35
        $this->args = $args;
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function buildUsing(QueryBuilder $queryBuilder, &$params = [])
42
    {
43
        $args = $this->args;
44
45
        if (!is_array($args) && !$args instanceof \Traversable) {
46
            $args = [$args];
47
        }
48
49
        $placeholders = [];
50
        foreach ($args as $item) {
51 View Code Duplication
            if ($item instanceof Query) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
                list ($sql, $params) = $queryBuilder->build($item, $params);
53
                $placeholders[] = $sql;
54
                continue;
55
            }
56
            if ($item instanceof ExpressionInterface) {
57
                $placeholders[] = $item->buildUsing($queryBuilder, $params);
58
                continue;
59
            }
60
            if ($item === null) {
61
                $placeholders[] = 'NULL';
62
                continue;
63
            }
64
65
            $placeholders[] = $placeholder = static::PARAM_PREFIX . count($params);
66
            $params[$placeholder] = $item;
67
        }
68
69
        return $this->buildCallString($this->name, implode(', ', $placeholders));
70
    }
71
72
    protected function buildCallString($name, $args)
73
    {
74
        return "$name($args)";
75
    }
76
}
77