TTranslate::translateKey()   C
last analyzed

Complexity

Conditions 14
Paths 14

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 14

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 21
ccs 18
cts 18
cp 1
rs 6.2666
cc 14
nc 14
nop 2
crap 14

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace kalanis\kw_mapper\Storage\Database\Dialects;
4
5
6
use kalanis\kw_mapper\Interfaces\IQueryBuilder;
7
use kalanis\kw_mapper\MapperException;
8
9
10
/**
11
 * Trait TTranslate
12
 * @package kalanis\kw_mapper\Storage\Database\Dialects
13
 * Translate actions in query builder into db operands
14
 */
15
trait TTranslate
16
{
17
    /**
18
     * @param string $operation
19
     * @throws MapperException
20
     * @return string
21
     */
22 54
    public function translateOperation(string $operation): string
23
    {
24
        switch ($operation) {
25 54
            case IQueryBuilder::OPERATION_NULL:
26 3
                return 'IS NULL';
27 53
            case IQueryBuilder::OPERATION_NNULL:
28 3
                return 'IS NOT NULL';
29 52
            case IQueryBuilder::OPERATION_EQ:
30 41
                return '=';
31 33
            case IQueryBuilder::OPERATION_NEQ:
32 21
                return '!=';
33 13
            case IQueryBuilder::OPERATION_GT:
34 1
                return '>';
35 12
            case IQueryBuilder::OPERATION_GTE:
36 2
                return '>=';
37 10
            case IQueryBuilder::OPERATION_LT:
38 1
                return '<';
39 9
            case IQueryBuilder::OPERATION_LTE:
40 1
                return '<=';
41 8
            case IQueryBuilder::OPERATION_LIKE:
42 3
                return 'LIKE';
43 5
            case IQueryBuilder::OPERATION_NLIKE:
44 1
                return 'NOT LIKE';
45 4
            case IQueryBuilder::OPERATION_REXP:
46 1
                return 'REGEX';
47 3
            case IQueryBuilder::OPERATION_IN:
48 1
                return 'IN';
49 2
            case IQueryBuilder::OPERATION_NIN:
50 1
                return 'NOT IN';
51
            default:
52 1
                throw new MapperException(sprintf('Unknown operation *%s*', $operation));
53
        }
54
    }
55
56
    /**
57
     * @param string $operation
58
     * @param string|string[] $columnKey
59
     * @throws MapperException
60
     * @return string
61
     */
62 60
    public function translateKey(string $operation, $columnKey): string
63
    {
64
        switch ($operation) {
65 60
            case IQueryBuilder::OPERATION_NULL:
66 59
            case IQueryBuilder::OPERATION_NNULL:
67 7
                return '';
68 58
            case IQueryBuilder::OPERATION_EQ:
69 37
            case IQueryBuilder::OPERATION_NEQ:
70 14
            case IQueryBuilder::OPERATION_GT:
71 13
            case IQueryBuilder::OPERATION_GTE:
72 11
            case IQueryBuilder::OPERATION_LT:
73 10
            case IQueryBuilder::OPERATION_LTE:
74 9
            case IQueryBuilder::OPERATION_LIKE:
75 5
            case IQueryBuilder::OPERATION_NLIKE:
76 4
            case IQueryBuilder::OPERATION_REXP:
77 55
                return strval($columnKey);
78 3
            case IQueryBuilder::OPERATION_IN:
79 2
            case IQueryBuilder::OPERATION_NIN:
80 2
                return sprintf('(%s)', implode(',', $this->notEmptyArray($columnKey)));
81
            default:
82 1
                throw new MapperException(sprintf('Unknown operation *%s*', $operation));
83
        }
84
    }
85
86
    /**
87
     * @param array<string|int|float>|string|int|float $array
88
     * @return array<string|int|float>
89
     */
90 2
    protected function notEmptyArray($array): array
91
    {
92 2
        if (empty($array)) {
93 1
            return [0];
94
        }
95 1
        return (array) $array;
96
    }
97
}
98