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

CallExpression   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 8.06 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 2
dl 5
loc 62
ccs 0
cts 30
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C buildUsing() 5 30 7
A buildCallString() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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