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:
Complex classes like WordPressDSExpressionProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WordPressDSExpressionProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class WordPressDSExpressionProvider implements IExpressionProvider |
||
| 12 | { |
||
| 13 | const ADD = '+'; |
||
| 14 | const CLOSE_BRACKET = ')'; |
||
| 15 | const COMMA = ','; |
||
| 16 | const DIVIDE = '/'; |
||
| 17 | const SUBTRACT = '-'; |
||
| 18 | const EQUAL = '='; |
||
| 19 | const GREATERTHAN = '>'; |
||
| 20 | const GREATERTHAN_OR_EQUAL = '>='; |
||
| 21 | const LESSTHAN = '<'; |
||
| 22 | const LESSTHAN_OR_EQUAL = '<='; |
||
| 23 | const LOGICAL_AND = '&&'; |
||
| 24 | const LOGICAL_NOT = '!'; |
||
| 25 | const LOGICAL_OR = '||'; |
||
| 26 | const MEMBERACCESS = ''; |
||
| 27 | const MODULO = '%'; |
||
| 28 | const MULTIPLY = '*'; |
||
| 29 | const NEGATE = '-'; |
||
| 30 | const NOTEQUAL = '!='; |
||
| 31 | const OPEN_BRAKET = '('; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The type of the resource pointed by the resource path segement |
||
| 35 | * |
||
| 36 | * @var ResourceType |
||
| 37 | */ |
||
| 38 | private $_resourceType; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * |
||
| 42 | * @var array(string, array(string, string)) |
||
| 43 | */ |
||
| 44 | private $_entityMapping; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Constructs new instance of WordPressDSExpressionProvider |
||
| 48 | * |
||
| 49 | */ |
||
| 50 | public function __construct() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get the name of the iterator |
||
| 57 | * |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | public function getIteratorName() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * call-back for setting the resource type. |
||
| 67 | * |
||
| 68 | * @param ResourceType $resourceType The resource type on which the filter |
||
| 69 | * is going to be applied. |
||
| 70 | */ |
||
| 71 | public function setResourceType(ResourceType $resourceType) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Call-back for logical expression |
||
| 78 | * |
||
| 79 | * @param ExpressionType $expressionType The type of logical expression. |
||
| 80 | * @param string $left The left expression. |
||
| 81 | * @param string $right The left expression. |
||
| 82 | * |
||
| 83 | * @return string |
||
| 84 | */ |
||
| 85 | View Code Duplication | public function onLogicalExpression($expressionType, $left, $right) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Call-back for arithmetic expression |
||
| 101 | * |
||
| 102 | * @param ExpressionType $expressionType The type of arithmetic expression. |
||
| 103 | * @param string $left The left expression. |
||
| 104 | * @param string $right The left expression. |
||
| 105 | * |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | View Code Duplication | public function onArithmeticExpression($expressionType, $left, $right) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Call-back for relational expression |
||
| 133 | * |
||
| 134 | * @param ExpressionType $expressionType The type of relation expression |
||
| 135 | * @param string $left The left expression |
||
| 136 | * @param string $right The left expression |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function onRelationalExpression($expressionType, $left, $right) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Call-back for unary expression |
||
| 172 | * |
||
| 173 | * @param ExpressionType $expressionType The type of unary expression |
||
| 174 | * @param string $child The child expression |
||
| 175 | * |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | View Code Duplication | public function onUnaryExpression($expressionType, $child) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Call-back for constant expression |
||
| 194 | * |
||
| 195 | * @param IType $type The type of constant |
||
| 196 | * @param objetc $value The value of the constant |
||
| 197 | * |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | View Code Duplication | public function onConstantExpression(IType $type, $value) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Call-back for property access expression |
||
| 213 | * |
||
| 214 | * @param PropertyAccessExpression $expression The property access expression |
||
| 215 | * |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | |||
| 219 | View Code Duplication | public function onPropertyAccessExpression($expression) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Call-back for function call expression |
||
| 238 | * |
||
| 239 | * @param FunctionDescription $functionDescription Description of the function. |
||
| 240 | * @param array<string> $params Paameters to the function. |
||
| 241 | * |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | public function onFunctionCallExpression($functionDescription, $params) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * To format binary expression |
||
| 332 | * |
||
| 333 | * @param string $operator The binary operator. |
||
| 334 | * @param string $left The left operand. |
||
| 335 | * @param string $right The right operand. |
||
| 336 | * |
||
| 337 | * @return string |
||
| 338 | */ |
||
| 339 | View Code Duplication | private function _prepareBinaryExpression($operator, $left, $right) |
|
| 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) |
||
| 368 | } |
||
| 369 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.