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 |
||
| 12 | class LaravelExpressionProvider 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 | const TYPE_NAMESPACE = 'POData\\Providers\\Metadata\\Type\\'; |
||
| 34 | |||
| 35 | private $functionDescriptionParsers; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The name of iterator. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $iteratorName; |
||
| 43 | /** |
||
| 44 | * The type of the resource pointed by the resource path segment. |
||
| 45 | * |
||
| 46 | * @var ResourceType |
||
| 47 | */ |
||
| 48 | private $resourceType; |
||
| 49 | 52 | ||
| 50 | public function __construct() |
||
| 51 | 52 | { |
|
| 52 | $this->functionDescriptionParsers[ODataConstants::STRFUN_COMPARE] = function ($params) { |
||
| 53 | return 'strcmp(' . $params[0] . ', ' . $params[1] . ')'; |
||
| 54 | }; |
||
| 55 | $this->functionDescriptionParsers[ODataConstants::STRFUN_ENDSWITH] = function ($params) { |
||
| 56 | return '(strcmp(substr(' . $params[0] . ', strlen(' . $params[0] . ') - strlen(' . $params[1] . ')), ' |
||
| 57 | 1 | .$params[1] . ') === 0)'; |
|
| 58 | }; |
||
| 59 | 1 | $this->functionDescriptionParsers[ODataConstants::STRFUN_INDEXOF] = function ($params) { |
|
| 60 | return 'strpos(' . $params[0] . ', ' . $params[1] . ')'; |
||
| 61 | }; |
||
| 62 | View Code Duplication | $this->functionDescriptionParsers[ODataConstants::STRFUN_REPLACE] = function ($params) { |
|
| 63 | return 'str_replace(' . $params[1] . ', ' . $params[2] . ', ' . $params[0] . ')'; |
||
| 64 | }; |
||
| 65 | $this->functionDescriptionParsers[ODataConstants::STRFUN_STARTSWITH] = function ($params) { |
||
| 66 | return '(strpos(' . $params[0] . ', ' . $params[1] . ') === 0)'; |
||
| 67 | 1 | }; |
|
| 68 | $this->functionDescriptionParsers[ODataConstants::STRFUN_TOLOWER] = function ($params) { |
||
| 69 | 1 | return 'strtolower(' . $params[0] . ')'; |
|
| 70 | 1 | }; |
|
| 71 | 1 | $this->functionDescriptionParsers[ODataConstants::STRFUN_TOUPPER] = function ($params) { |
|
| 72 | return 'strtoupper(' . $params[0] . ')'; |
||
| 73 | }; |
||
| 74 | $this->functionDescriptionParsers[ODataConstants::STRFUN_TRIM] = function ($params) { |
||
| 75 | return 'trim(' . $params[0] . ')'; |
||
| 76 | }; |
||
| 77 | $this->functionDescriptionParsers[ODataConstants::STRFUN_SUBSTRING] = function ($params) { |
||
| 78 | return count($params) == 3 ? |
||
| 79 | 'substr(' . $params[0] . ', ' . $params[1] . ', ' . $params[2] . ')' : 'substr(' . $params[0] . ', ' . $params[1] . ')'; |
||
|
|
|||
| 80 | }; |
||
| 81 | 3 | $this->functionDescriptionParsers[ODataConstants::STRFUN_SUBSTRINGOF] = function ($params) { |
|
| 82 | return '(strpos(' . $params[1] . ', ' . $params[0] . ') !== false)'; |
||
| 83 | }; |
||
| 84 | 3 | $this->functionDescriptionParsers[ODataConstants::STRFUN_CONCAT] = function ($params) { |
|
| 85 | 1 | return $params[0] . ' . ' . $params[1]; |
|
| 86 | 2 | }; |
|
| 87 | 1 | $this->functionDescriptionParsers[ODataConstants::STRFUN_LENGTH] = function ($params) { |
|
| 88 | 1 | return 'strlen(' . $params[0] . ')'; |
|
| 89 | 1 | }; |
|
| 90 | 1 | View Code Duplication | $this->functionDescriptionParsers[ODataConstants::GUIDFUN_EQUAL] = function ($params) { |
| 91 | return self::TYPE_NAMESPACE . 'Guid::guidEqual(' . $params[0] . ', ' . $params[1] . ')'; |
||
| 92 | }; |
||
| 93 | View Code Duplication | $this->functionDescriptionParsers[ODataConstants::DATETIME_COMPARE] = function ($params) { |
|
| 94 | return self::TYPE_NAMESPACE . 'DateTime::dateTimeCmp(' . $params[0] . ', ' . $params[1] . ')'; |
||
| 95 | }; |
||
| 96 | $this->functionDescriptionParsers[ODataConstants::DATETIME_YEAR] = function ($params) { |
||
| 97 | return self::TYPE_NAMESPACE . 'DateTime::year(' . $params[0] . ')'; |
||
| 98 | }; |
||
| 99 | $this->functionDescriptionParsers[ODataConstants::DATETIME_MONTH] = function ($params) { |
||
| 100 | return self::TYPE_NAMESPACE . 'DateTime::month(' . $params[0] . ')'; |
||
| 101 | 6 | }; |
|
| 102 | $this->functionDescriptionParsers[ODataConstants::DATETIME_DAY] = function ($params) { |
||
| 103 | return self::TYPE_NAMESPACE . 'DateTime::day(' . $params[0] . ')'; |
||
| 104 | 6 | }; |
|
| 105 | 1 | $this->functionDescriptionParsers[ODataConstants::DATETIME_HOUR] = function ($params) { |
|
| 106 | 5 | return self::TYPE_NAMESPACE . 'DateTime::hour(' . $params[0] . ')'; |
|
| 107 | 1 | }; |
|
| 108 | 4 | $this->functionDescriptionParsers[ODataConstants::DATETIME_MINUTE] = function ($params) { |
|
| 109 | 1 | return self::TYPE_NAMESPACE . 'DateTime::minute(' . $params[0] . ')'; |
|
| 110 | 3 | }; |
|
| 111 | 1 | $this->functionDescriptionParsers[ODataConstants::DATETIME_SECOND] = function ($params) { |
|
| 112 | 2 | return self::TYPE_NAMESPACE . 'DateTime::second(' . $params[0] . ')'; |
|
| 113 | 1 | }; |
|
| 114 | 1 | $this->functionDescriptionParsers[ODataConstants::MATHFUN_ROUND] = function ($params) { |
|
| 115 | 1 | return 'round(' . $params[0] . ')'; |
|
| 116 | 1 | }; |
|
| 117 | $this->functionDescriptionParsers[ODataConstants::MATHFUN_CEILING] = function ($params) { |
||
| 118 | return 'ceil(' . $params[0] . ')'; |
||
| 119 | }; |
||
| 120 | $this->functionDescriptionParsers[ODataConstants::MATHFUN_FLOOR] = function ($params) { |
||
| 121 | return 'floor(' . $params[0] . ')'; |
||
| 122 | }; |
||
| 123 | View Code Duplication | $this->functionDescriptionParsers[ODataConstants::BINFUL_EQUAL] = function ($params) { |
|
| 124 | return self::TYPE_NAMESPACE . 'Binary::binaryEqual(' . $params[0] . ', ' . $params[1] . ')'; |
||
| 125 | }; |
||
| 126 | $this->functionDescriptionParsers['is_null'] = function ($params) { |
||
| 127 | 7 | return 'is_null(' . $params[0] . ')'; |
|
| 128 | }; |
||
| 129 | } |
||
| 130 | 7 | /** |
|
| 131 | 1 | * Get the name of the iterator. |
|
| 132 | 6 | * |
|
| 133 | 1 | * @return string |
|
| 134 | 5 | */ |
|
| 135 | 1 | public function getIteratorName() |
|
| 139 | 1 | ||
| 140 | 2 | /** |
|
| 141 | 1 | * Get the resource type. |
|
| 142 | 1 | * |
|
| 143 | 1 | * @return object|null |
|
| 144 | 1 | */ |
|
| 145 | public function getResourceType() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * call-back for setting the resource type. |
||
| 152 | * |
||
| 153 | * @param ResourceType $resourceType The resource type on which the filter |
||
| 154 | 3 | * is going to be applied |
|
| 155 | */ |
||
| 156 | public function setResourceType(ResourceType $resourceType) |
||
| 161 | 1 | /** |
|
| 162 | 1 | * Call-back for logical expression. |
|
| 163 | 1 | * |
|
| 164 | * @param ExpressionType $expressionType The type of logical expression |
||
| 165 | * @param string $left The left expression |
||
| 166 | * @param string $right The left expression |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | View Code Duplication | public function onLogicalExpression($expressionType, $left, $right) |
|
| 182 | /** |
||
| 183 | * Call-back for arithmetic expression. |
||
| 184 | * |
||
| 185 | * @param ExpressionType $expressionType The type of arithmetic expression |
||
| 186 | * @param string $left The left expression |
||
| 187 | * @param string $right The left expression |
||
| 188 | * |
||
| 189 | 1 | * @return string |
|
| 190 | */ |
||
| 191 | 1 | public function onArithmeticExpression($expressionType, $left, $right) |
|
| 209 | 28 | /** |
|
| 210 | * Call-back for relational expression. |
||
| 211 | 28 | * |
|
| 212 | 1 | * @param ExpressionType $expressionType The type of relation expression |
|
| 213 | * @param string $left The left expression |
||
| 214 | 27 | * @param string $right The left expression |
|
| 215 | 27 | * |
|
| 216 | 1 | * @return string |
|
| 217 | 26 | */ |
|
| 218 | 1 | public function onRelationalExpression($expressionType, $left, $right) |
|
| 238 | 15 | /** |
|
| 239 | 1 | * Call-back for unary expression. |
|
| 240 | 14 | * |
|
| 241 | 1 | * @param ExpressionType $expressionType The type of unary expression |
|
| 242 | 13 | * @param string $child The child expression |
|
| 243 | 1 | * |
|
| 244 | 12 | * @return string |
|
| 245 | 1 | */ |
|
| 246 | 11 | View Code Duplication | public function onUnaryExpression($expressionType, $child) |
| 258 | 5 | /** |
|
| 259 | 1 | * Call-back for constant expression. |
|
| 260 | 4 | * |
|
| 261 | 1 | * @param IType $type The type of constant |
|
| 262 | 3 | * @param mixed $value The value of the constant |
|
| 263 | 1 | * |
|
| 264 | 2 | * @return string |
|
| 265 | 1 | */ |
|
| 266 | 1 | public function onConstantExpression(IType $type, $value) |
|
| 275 | /** |
||
| 276 | * Call-back for property access expression. |
||
| 277 | * |
||
| 278 | * @param PropertyAccessExpression $expression The property access expression |
||
| 279 | 13 | * |
|
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | 13 | public function onPropertyAccessExpression($expression) |
|
| 294 | 2 | /** |
|
| 295 | * Call-back for function call expression. |
||
| 296 | * |
||
| 297 | * @param \POData\UriProcessor\QueryProcessor\FunctionDescription $functionDescription Description of the function |
||
| 298 | * @param array<string> $params Parameters to the function |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | public function onFunctionCallExpression($functionDescription, $params) |
||
| 312 | /** |
||
| 313 | * To format binary expression. |
||
| 314 | * |
||
| 315 | * @param string $operator The binary operator |
||
| 316 | * @param string $left The left operand |
||
| 317 | * @param string $right The right operand |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | private function prepareBinaryExpression($operator, $left, $right) |
||
| 325 | /** |
||
| 326 | * To format unary expression. |
||
| 327 | * |
||
| 328 | * @param string $operator The unary operator |
||
| 329 | * @param string $child The operand |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | private function prepareUnaryExpression($operator, $child) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param $expressionType |
||
| 340 | * @return mixed |
||
| 341 | */ |
||
| 342 | private function unpackExpressionType($expressionType) |
||
| 351 | } |
||
| 352 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.