Completed
Branch feature/pre-split (b5c37f)
by Anton
03:43
created

MySQLCompiler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 77
Duplicated Lines 23.38 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 18
loc 77
rs 10
c 0
b 0
f 0
wmc 14
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A orderParameters() 0 20 2
B compileLimit() 18 18 7
B prepareOperator() 0 18 5

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
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Database\Drivers\MySQL;
10
11
use Spiral\Database\Entities\QueryCompiler as AbstractCompiler;
12
use Spiral\Database\Injections\ParameterInterface;
13
14
/**
15
 * MySQL syntax specific compiler.
16
 */
17
class MySQLCompiler extends AbstractCompiler
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function orderParameters(
23
        string $queryType,
24
        array $whereParameters = [],
25
        array $onParameters = [],
26
        array $havingParameters = [],
27
        array $columnIdentifiers = []
28
    ): array {
29
        if ($queryType == self::UPDATE_QUERY) {
30
            //Where statement has pretty specific order
31
            return array_merge($onParameters, $columnIdentifiers, $whereParameters);
32
        }
33
34
        return parent::orderParameters(
35
            $queryType,
36
            $whereParameters,
37
            $onParameters,
38
            $havingParameters,
39
            $columnIdentifiers
40
        );
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     *
46
     * @link http://dev.mysql.com/doc/refman/5.0/en/select.html#id4651990
47
     */
48 View Code Duplication
    protected function compileLimit(int $limit, int $offset): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
49
    {
50
        if (empty($limit) && empty($offset)) {
51
            return '';
52
        }
53
54
        $statement = '';
55
        if (!empty($limit) || !empty($offset)) {
56
            //When limit is not provided but offset does we can replace limit value with PHP_INT_MAX
57
            $statement = 'LIMIT ' . ($limit ?: '18446744073709551615') . ' ';
58
        }
59
60
        if (!empty($offset)) {
61
            $statement .= "OFFSET {$offset}";
62
        }
63
64
        return trim($statement);
65
    }
66
67
    /**
68
     * Resolve operator value based on value value. ;).
69
     *
70
     * @param mixed  $parameter
71
     * @param string $operator
72
     *
73
     * @return string
74
     */
75
    protected function prepareOperator($parameter, string $operator): string
76
    {
77
        if (!$parameter instanceof ParameterInterface) {
78
            //Probably fragment
79
            return $operator;
80
        }
81
82
        if ($parameter->getType() == \PDO::PARAM_NULL) {
83
            switch ($operator) {
84
                case '=':
85
                    return 'IS';
86
                case '!=':
87
                    return 'IS NOT';
88
            }
89
        }
90
91
        return parent::prepareOperator($parameter, $operator);
92
    }
93
}
94