Completed
Branch feature/pre-split (80980a)
by Anton
04:00
created

MySQLCompiler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 53
Duplicated Lines 33.96 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
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
     * @link http://dev.mysql.com/doc/refman/5.0/en/select.html#id4651990
23
     */
24 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...
25
    {
26
        if (empty($limit) && empty($offset)) {
27
            return '';
28
        }
29
30
        $statement = '';
31
        if (!empty($limit) || !empty($offset)) {
32
            //When limit is not provided but offset does we can replace limit value with PHP_INT_MAX
33
            $statement = 'LIMIT ' . ($limit ?: '18446744073709551615') . ' ';
34
        }
35
36
        if (!empty($offset)) {
37
            $statement .= "OFFSET {$offset}";
38
        }
39
40
        return trim($statement);
41
    }
42
43
    /**
44
     * Resolve operator value based on value value. ;).
45
     *
46
     * @param mixed  $parameter
47
     * @param string $operator
48
     *
49
     * @return string
50
     */
51
    protected function prepareOperator($parameter, string $operator): string
52
    {
53
        if (!$parameter instanceof ParameterInterface) {
54
            //Probably fragment
55
            return $operator;
56
        }
57
58
        if ($parameter->getType() == \PDO::PARAM_NULL) {
59
            switch ($operator) {
60
                case '=':
61
                    return 'IS';
62
                case '!=':
63
                    return 'IS NOT';
64
            }
65
        }
66
67
        return parent::prepareOperator($parameter, $operator);
68
    }
69
}
70