Passed
Push — master ( 5646b8...782b53 )
by compolom
02:04
created

Fields::setFunction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Compolomus\LSQLQueryBuilder\System;
4
5
use Compolomus\LSQLQueryBuilder\BuilderException;
6
7
class Fields
8
{
9
    use Traits\Helper;
10
11
    private $name;
12
13
    private $allias;
14
15
    private $result;
16
17
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
18
    'COMPRESS',
19
    'MD5',
20
    'PASSWORD',
21
    'SHA',
22
    'SHA1',
23
    'UNCOMPRESS',
24
    'UNCOMPRESSED_LENGTH',
25
    'VALIDATE_PASSWORD_STRENGTH',
26
27
    'AVG',
28
    'BIT_AND',
29
    'BIT_OR',
30
    'BIT_XOR',
31
    'BIT_COUNT',
32
    'COUNT',
33
    'GROUP_CONCAT',
34
    'JSON_ARRAYAGG',
35
    'MAX',
36
    'MIN',
37
    'STD',
38
    'STDDEV',
39
    'STDDEV_POP',
40
    'STDDEV_SAMP',
41
    'SUM',
42
    'VAR_POP',
43
    'VAR_SAMP',
44
    'VARIANCE',
45
46
    'ASCII',
47
    'BIN',
48
    'ORD',
49
    'CHAR_LENGTH', // multibyte len
50
    'HEX',
51
    'OCT',
52
    'LCASE',
53
    'LOWER',
54
    'UCASE',
55
    'UPPER',
56
    'REVERSE',
57
    #'LOAD_FILE', // security
58
    'TRIM', // rtrim
59
    'LTRIM',
60
    'RTRIM',
61
62
    'ABS',
63
    'ACOS',
64
    'ASIN',
65
    'ATAN',
66
    'CEIL',
67
    'CEILING',
68
    'COS',
69
    'COT',
70
    'CRC32',
71
    'DEGREES',
72
    'EXP',
73
    'FLOOR',
74
    'LN',
75
    'LOG',
76
    'LOG2',
77
    'LOG10',
78
    'RADIANS',
79
    'ROUND',
80
    'SIGN',
81
    'SIN',
82
    'SQRT',
83
    'TAN',
84
85
    'DATE',
86
    'DAY',
87
    'DAYNAME',
88
    'DAYOFMONTH',
89
    'DAYOFWEEK',
90
    'DAYOFYEAR',
91
    'FROM_DAYS',
92
    'FROM_UNIXTIME',
93
    'HOUR',
94
    'LAST_DAY',
95
    'MICROSECOND',
96
    'MINUTE',
97
    'MONTH',
98
    'MONTHNAME',
99
    'QUARTER',
100
    'SECOND',
101
    'SEC_TO_TIME',
102
    'TIME',
103
    'TIME_TO_SEC',
104
    'TO_DAYS',
105
    'TO_SECONDS',
106
    'UNIX_TIMESTAMP',
107
    'WEEK',
108
    'WEEKDAY',
109
    'YEAR',
110
    'YEARWEEK',
111
    'WEEKOFYEAR',
112
     */
113
    private $functionsList = [
114
        'MD5',
115
        'SHA1',
116
        'AVG',
117
        'COUNT',
118
        'MAX',
119
        'MIN',
120
        'SUM',
121
        'ABS',
122
        'UNIX_TIMESTAMP'
123
    ];
124
125 4
    public function __construct(string $name)
126
    {
127 4
        $this->result = $this->name = $this->escapeField($name);
128 4
    }
129
130 2
    public function setAllias(string $alliasName): void
131
    {
132 2
        $this->allias = $this->escapeField($alliasName);
133 2
    }
134
135 3
    public function setFunction(string $functionName): void
136
    {
137 3
        if (!\in_array(strtoupper($functionName), $this->functionsList, true)) {
138 1
            throw new BuilderException('MYSQL Функция ' . $functionName . ' не найдена |SELECT setFunction|');
139
        }
140 2
        $this->result = strtoupper($functionName) . '(' . $this->name . ')';
141 2
    }
142
143 2
    public function result(): string
144
    {
145 2
        return $this->result . (null !== $this->allias ? ' AS ' . $this->allias : '');
146
    }
147
}
148