Passed
Branch master (950424)
by Christopher
11:06
created

onFunctionCallExpression()   D

Complexity

Conditions 27
Paths 27

Size

Total Lines 82
Code Lines 55

Duplication

Lines 10
Ratio 12.2 %

Importance

Changes 0
Metric Value
cc 27
eloc 55
nc 27
nop 2
dl 10
loc 82
rs 4.8775
c 0
b 0
f 0

How to fix   Long Method    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 POData\Providers\Expression;
4
5
use POData\Common\ODataConstants;
6
use POData\Providers\Metadata\ResourceType;
7
use POData\Providers\Metadata\Type\IType;
8
use POData\UriProcessor\QueryProcessor\ExpressionParser\Expressions\ExpressionType;
9
use POData\UriProcessor\QueryProcessor\ExpressionParser\Expressions\PropertyAccessExpression;
10
use POData\UriProcessor\QueryProcessor\FunctionDescription;
11
12
class MySQLExpressionProvider implements IExpressionProvider
13
{
14
    const ADD = '+';
15
    const CLOSE_BRACKET = ')';
16
    const COMMA = ',';
17
    const DIVIDE = '/';
18
    const SUBTRACT = '-';
19
    const EQUAL = '=';
20
    const GREATER_THAN = '>';
21
    const GREATER_THAN_OR_EQUAL = '>=';
22
    const LESS_THAN = '<';
23
    const LESS_THAN_OR_EQUAL = '<=';
24
    const LOGICAL_AND = '&&';
25
    const LOGICAL_NOT = '!';
26
    const LOGICAL_OR = '||';
27
    const MEMBER_ACCESS = '';
28
    const MODULO = '%';
29
    const MULTIPLY = '*';
30
    const NEGATE = '-';
31
    const NOT_EQUAL = '!=';
32
    const OPEN_BRACKET = '(';
33
34
    /**
35
     * The type of the resource pointed by the resource path segment.
36
     *
37
     * @var ResourceType
38
     */
39
    private $resourceType;
40
41
    private $entityMapping;
42
43
    /**
44
     * Constructs new instance of MySQLExpressionProvider.
45
     */
46
    public function __construct()
47
    {
48
        $this->entityMapping = [];
49
    }
50
51
    /**
52
     * Get the name of the iterator.
53
     *
54
     * @return string|null
55
     */
56
    public function getIteratorName()
57
    {
58
    }
59
60
    /**
61
     * call-back for setting the resource type.
62
     *
63
     * @param ResourceType $resourceType The resource type on which the filter is going to be applied
64
     */
65
    public function setResourceType(ResourceType $resourceType)
66
    {
67
        $this->resourceType = $resourceType;
68
    }
69
70
    /**
71
     * Call-back for logical expression.
72
     *
73
     * @param ExpressionType $expressionType The type of logical expression
74
     * @param string         $left           The left expression
75
     * @param string         $right          The left expression
76
     *
77
     * @return string
78
     */
79
    public function onLogicalExpression($expressionType, $left, $right)
80
    {
81
        switch ($expressionType) {
82
            case ExpressionType::AND_LOGICAL:
83
                return $this->prepareBinaryExpression(self::LOGICAL_AND, $left, $right);
84
85
            case ExpressionType::OR_LOGICAL:
86
                return $this->prepareBinaryExpression(self::LOGICAL_OR, $left, $right);
87
88
            default:
89
                throw new \InvalidArgumentException('onLogicalExpression');
90
        }
91
    }
92
93
    /**
94
     * Call-back for arithmetic expression.
95
     *
96
     * @param ExpressionType $expressionType The type of arithmetic expression
97
     * @param string         $left           The left expression
98
     * @param string         $right          The left expression
99
     *
100
     * @return string
101
     */
102
    public function onArithmeticExpression($expressionType, $left, $right)
103
    {
104
        switch ($expressionType) {
105
            case ExpressionType::MULTIPLY:
106
                return $this->prepareBinaryExpression(self::MULTIPLY, $left, $right);
107
108
            case ExpressionType::DIVIDE:
109
                return $this->prepareBinaryExpression(self::DIVIDE, $left, $right);
110
111
            case ExpressionType::MODULO:
112
                return $this->prepareBinaryExpression(self::MODULO, $left, $right);
113
114
            case ExpressionType::ADD:
115
                return $this->prepareBinaryExpression(self::ADD, $left, $right);
116
117
            case ExpressionType::SUBTRACT:
118
                return $this->prepareBinaryExpression(self::SUBTRACT, $left, $right);
119
120
            default:
121
                throw new \InvalidArgumentException('onArithmeticExpression');
122
        }
123
    }
124
125
    /**
126
     * Call-back for relational expression.
127
     *
128
     * @param ExpressionType $expressionType The type of relation expression
129
     * @param string         $left           The left expression
130
     * @param string         $right          The left expression
131
     *
132
     * @return string
133
     */
134
    public function onRelationalExpression($expressionType, $left, $right)
135
    {
136
        switch ($expressionType) {
137
            case ExpressionType::GREATERTHAN:
138
                return $this->prepareBinaryExpression(self::GREATER_THAN, $left, $right);
139
140
            case ExpressionType::GREATERTHAN_OR_EQUAL:
141
                return $this->prepareBinaryExpression(self::GREATER_THAN_OR_EQUAL, $left, $right);
142
143
            case ExpressionType::LESSTHAN:
144
                return $this->prepareBinaryExpression(self::LESS_THAN, $left, $right);
145
146
            case ExpressionType::LESSTHAN_OR_EQUAL:
147
                return $this->prepareBinaryExpression(self::LESS_THAN_OR_EQUAL, $left, $right);
148
149
            case ExpressionType::EQUAL:
150
                return $this->prepareBinaryExpression(self::EQUAL, $left, $right);
151
152
            case ExpressionType::NOTEQUAL:
153
                return $this->prepareBinaryExpression(self::NOT_EQUAL, $left, $right);
154
155
            default:
156
                throw new \InvalidArgumentException('onRelationalExpression');
157
        }
158
    }
159
160
    /**
161
     * Call-back for unary expression.
162
     *
163
     * @param ExpressionType $expressionType The type of unary expression
164
     * @param string         $child          The child expression
165
     *
166
     * @return string
167
     */
168
    public function onUnaryExpression($expressionType, $child)
169
    {
170
        switch ($expressionType) {
171
            case ExpressionType::NEGATE:
172
                return $this->prepareUnaryExpression(self::NEGATE, $child);
173
174
            case ExpressionType::NOT_LOGICAL:
175
                return $this->prepareUnaryExpression(self::LOGICAL_NOT, $child);
176
177
            default:
178
                throw new \InvalidArgumentException('onUnaryExpression');
179
        }
180
    }
181
182
    /**
183
     * Call-back for constant expression.
184
     *
185
     * @param IType $type  The type of constant
186
     * @param mixed $value The value of the constant
187
     *
188
     * @return string
189
     */
190 View Code Duplication
    public function onConstantExpression(IType $type, $value)
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...
191
    {
192
        if (is_bool($value)) {
193
            return var_export($value, true);
194
        } elseif (null === $value) {
195
            return var_export(null, true);
196
        }
197
198
        return strval($value);
199
    }
200
201
    /**
202
     * Call-back for property access expression.
203
     *
204
     * @param PropertyAccessExpression $expression The property access expression
205
     *
206
     * @return string
207
     */
208
    public function onPropertyAccessExpression($expression)
209
    {
210
        if (null == $expression) {
211
            throw new \InvalidArgumentException('onPropertyAccessExpression - expression null');
212
        }
213
        if (!($expression instanceof PropertyAccessExpression)) {
214
            throw new \InvalidArgumentException('onPropertyAccessExpression - expression is incorrect type');
215
        }
216
        if (null == $this->resourceType) {
217
            throw new \InvalidArgumentException('onPropertyAccessExpression - resourceType null');
218
        }
219
        if (null == $this->resourceType->getName()) {
220
            throw new \InvalidArgumentException('onPropertyAccessExpression - resourceType has no name');
221
        }
222
        if (null == $expression->getResourceProperty()) {
223
            throw new \InvalidArgumentException('onPropertyAccessExpression - expression has no resource property');
224
        }
225
        $parent = $expression;
226
        $entityTypeName = $this->resourceType->getName();
227
        $propertyName = $parent->getResourceProperty()->getName();
228
        if (is_array($this->entityMapping)) {
229
            if (array_key_exists($entityTypeName, $this->entityMapping)) {
230
                if (array_key_exists($propertyName, $this->entityMapping[$entityTypeName])) {
231
                    return $this->entityMapping[$entityTypeName][$propertyName];
232
                }
233
            }
234
        }
235
236
        return $propertyName;
237
    }
238
239
    /**
240
     * Call-back for function call expression.
241
     *
242
     * @param FunctionDescription $functionDescription Description of the function
243
     * @param array<string>       $params              Parameters to the function
244
     *
245
     * @return string
246
     */
247
    public function onFunctionCallExpression($functionDescription, $params)
248
    {
249
        switch ($functionDescription->name) {
250
            case ODataConstants::STRFUN_COMPARE:
251
                return 'STRCMP(' . $params[0] . ', ' . $params[1] . ')';
252
253 View Code Duplication
            case ODataConstants::STRFUN_ENDSWITH:
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...
254
                return '(STRCMP(' . $params[1] . ',RIGHT(' . $params[0] . ',LENGTH(' . $params[1] . '))) = 0)';
255
256
            case ODataConstants::STRFUN_INDEXOF:
257
                return 'INSTR(' . $params[0] . ', ' . $params[1] . ') - 1';
258
259 View Code Duplication
            case ODataConstants::STRFUN_REPLACE:
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...
260
                return 'REPLACE(' . $params[0] . ',' . $params[1] . ',' . $params[2] . ')';
261
262 View Code Duplication
            case ODataConstants::STRFUN_STARTSWITH:
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...
263
                return '(STRCMP(' . $params[1] . ',LEFT(' . $params[0] . ',LENGTH(' . $params[1] . '))) = 0)';
264
265
            case ODataConstants::STRFUN_TOLOWER:
266
                return 'LOWER(' . $params[0] . ')';
267
268
            case ODataConstants::STRFUN_TOUPPER:
269
                return 'UPPER(' . $params[0] . ')';
270
271
            case ODataConstants::STRFUN_TRIM:
272
                return 'TRIM(' . $params[0] . ')';
273
274 View Code Duplication
            case ODataConstants::STRFUN_SUBSTRING:
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...
275
                return count($params) == 3 ?
276
                    'SUBSTRING(' . $params[0] . ', ' . $params[1] . ' + 1, ' . $params[2] . ')'
277
                    : 'SUBSTRING(' . $params[0] . ', ' . $params[1] . ' + 1)';
278
279
            case ODataConstants::STRFUN_SUBSTRINGOF:
280
                return '(LOCATE(' . $params[0] . ', ' . $params[1] . ') > 0)';
281
282
            case ODataConstants::STRFUN_CONCAT:
283
                return 'CONCAT(' . $params[0] . ',' . $params[1] . ')';
284
285
            case ODataConstants::STRFUN_LENGTH:
286
                return 'LENGTH(' . $params[0] . ')';
287
288
            case ODataConstants::GUIDFUN_EQUAL:
289
                return 'STRCMP(' . $params[0] . ', ' . $params[1] . ')';
290
291
            case ODataConstants::DATETIME_COMPARE:
292
                return 'DATETIMECMP(' . $params[0] . '; ' . $params[1] . ')';
293
294
            case ODataConstants::DATETIME_YEAR:
295
                return 'EXTRACT(YEAR from ' . $params[0] . ')';
296
297
            case ODataConstants::DATETIME_MONTH:
298
                return 'EXTRACT(MONTH from ' . $params[0] . ')';
299
300
            case ODataConstants::DATETIME_DAY:
301
                return 'EXTRACT(DAY from ' . $params[0] . ')';
302
303
            case ODataConstants::DATETIME_HOUR:
304
                return 'EXTRACT(HOUR from ' . $params[0] . ')';
305
306
            case ODataConstants::DATETIME_MINUTE:
307
                return 'EXTRACT(MINUTE from ' . $params[0] . ')';
308
309
            case ODataConstants::DATETIME_SECOND:
310
                return 'EXTRACT(SECOND from ' . $params[0] . ')';
311
312
            case ODataConstants::MATHFUN_ROUND:
313
                return 'ROUND(' . $params[0] . ')';
314
315
            case ODataConstants::MATHFUN_CEILING:
316
                return 'CEIL(' . $params[0] . ')';
317
318
            case ODataConstants::MATHFUN_FLOOR:
319
                return 'FLOOR(' . $params[0] . ')';
320
321
            case ODataConstants::BINFUL_EQUAL:
322
                return '(' . $params[0] . ' = ' . $params[1] . ')';
323
324
            case 'is_null':
325
                return 'is_null(' . $params[0] . ')';
326
327
            default:
328
                throw new \InvalidArgumentException('onFunctionCallExpression');
329
        }
330
    }
331
332
    /**
333
     * To format binary expression.
334
     *
335
     * @param string $operator The binary operator
336
     * @param string $left     The left operand
337
     * @param string $right    The right operand
338
     *
339
     * @return string
340
     */
341
    private function prepareBinaryExpression($operator, $left, $right)
342
    {
343
        //DATETIMECMP
344
        if (0 == substr_compare($left, 'DATETIMECMP', 0, 11)) {
345
            $str = explode(';', $left, 2);
346
            $str[0] = str_replace('DATETIMECMP', '', $str[0]);
347
348
            return self::OPEN_BRACKET
349
                .$str[0] . ' ' . $operator
350
                .' ' . $str[1] . self::CLOSE_BRACKET;
351
        }
352
353
        return self::OPEN_BRACKET . $left . ' ' . $operator . ' ' . $right . self::CLOSE_BRACKET;
354
    }
355
356
    /**
357
     * To format unary expression.
358
     *
359
     * @param string $operator The unary operator
360
     * @param string $child    The operand
361
     *
362
     * @return string
363
     */
364
    private function prepareUnaryExpression($operator, $child)
365
    {
366
        return $operator . self::OPEN_BRACKET . $child . self::CLOSE_BRACKET;
367
    }
368
}
369