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 ExpressionParser2 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 ExpressionParser2, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class ExpressionParser2 extends ExpressionParser |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | private $mapTable; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Collection of navigation properties used in the expression. |
||
| 37 | * |
||
| 38 | * @var array(array(ResourceProperty)) |
||
| 39 | */ |
||
| 40 | private $navigationPropertiesUsedInTheExpression; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Indicates whether the end user has implemented IExpressionProvider or not. |
||
| 44 | * |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | private $isPHPExpressionProvider; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Create new instance of ExpressionParser2. |
||
| 51 | * |
||
| 52 | * @param string $text The text expression to parse |
||
| 53 | * @param ResourceType $resourceType The resource type in which |
||
| 54 | * expression will be applied |
||
| 55 | * @param bool $isPHPExpressionProvider True if IExpressionProvider provider is |
||
| 56 | * implemented by user, False otherwise |
||
| 57 | */ |
||
| 58 | public function __construct($text, ResourceType $resourceType, $isPHPExpressionProvider) |
||
| 59 | { |
||
| 60 | parent::__construct($text, $resourceType, $isPHPExpressionProvider); |
||
| 61 | $this->navigationPropertiesUsedInTheExpression = []; |
||
| 62 | $this->isPHPExpressionProvider = $isPHPExpressionProvider; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Parse and generate expression from the the given odata expression. |
||
| 67 | * |
||
| 68 | * |
||
| 69 | * @param string $text The text expression to parse |
||
| 70 | * @param ResourceType $resourceType The resource type in which |
||
| 71 | * @param IExpressionProvider $expressionProvider Implementation of IExpressionProvider |
||
| 72 | * |
||
| 73 | * @throws ODataException If any error occurs while parsing the odata expression or |
||
| 74 | * building the php/custom expression |
||
| 75 | * |
||
| 76 | * @return FilterInfo |
||
| 77 | */ |
||
| 78 | public static function parseExpression2($text, ResourceType $resourceType, IExpressionProvider $expressionProvider) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Parse the expression. |
||
| 102 | * |
||
| 103 | * @see library/POData/QueryProcessor/ExpressionParser::parseFilter() |
||
| 104 | * |
||
| 105 | * @throws ODataException |
||
| 106 | * |
||
| 107 | * @return AbstractExpression |
||
| 108 | */ |
||
| 109 | public function parseFilter() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Process the expression node for nullability. |
||
| 129 | * |
||
| 130 | * @param AbstractExpression $expression The expression node to process |
||
| 131 | * @param AbstractExpression $parentExpression The parent expression of expression node to process |
||
| 132 | * @param bool $checkNullForMostChild whether to include null check for current property |
||
| 133 | * |
||
| 134 | * @throws ODataException |
||
| 135 | * |
||
| 136 | * @return AbstractExpression New expression tree with nullability check |
||
| 137 | */ |
||
| 138 | private function processNodeForNullability( |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Process an arithmetic expression node for nullability. |
||
| 170 | * |
||
| 171 | * @param ArithmeticExpression $expression The arithmetic expression node |
||
| 172 | * to process |
||
| 173 | * |
||
| 174 | * @return AbstractExpression|null |
||
| 175 | */ |
||
| 176 | private function processArithmeticNode(ArithmeticExpression $expression) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Process an arithmetic expression node for nullability. |
||
| 202 | * |
||
| 203 | * @param FunctionCallExpression $expression The function call expression |
||
| 204 | * node to process |
||
| 205 | * @param AbstractExpression $parentExpression The parent expression of |
||
| 206 | * expression node to process |
||
| 207 | * |
||
| 208 | * @return null|AbstractExpression |
||
| 209 | */ |
||
| 210 | private function processFunctionCallNode( |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Process an logical expression node for nullability. |
||
| 254 | * |
||
| 255 | * @param LogicalExpression $expression The logical expression node |
||
| 256 | * to process |
||
| 257 | * @param AbstractExpression $parentExpression The parent expression of |
||
| 258 | * expression node to process |
||
| 259 | * |
||
| 260 | * @return null|AbstractExpression |
||
| 261 | */ |
||
| 262 | private function processLogicalNode( |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Process an property access expression node for nullability. |
||
| 324 | * |
||
| 325 | * @param PropertyAccessExpression $expression The property access |
||
| 326 | * expression node to process |
||
| 327 | * @param AbstractExpression $parentExpression The parent expression of |
||
| 328 | * expression node to process |
||
| 329 | * @param bool $checkNullForMostChild Wheter to check null for |
||
| 330 | * most child node or not |
||
| 331 | * |
||
| 332 | * @return LogicalExpression|RelationalExpression|null |
||
| 333 | */ |
||
| 334 | private function processPropertyAccessNode( |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Process a releational expression node for nullability. |
||
| 359 | * |
||
| 360 | * @param RelationalExpression $expression The relational expression node |
||
| 361 | * to process |
||
| 362 | * @param AbstractExpression $parentExpression The parent expression of |
||
| 363 | * expression node to process |
||
| 364 | * |
||
| 365 | * @return null|AbstractExpression |
||
| 366 | */ |
||
| 367 | private function processRelationalNode( |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Process an unary expression node for nullability. |
||
| 407 | * |
||
| 408 | * @param UnaryExpression $expression The unary expression node |
||
| 409 | * to process |
||
| 410 | * @param AbstractExpression $parentExpression The parent expression of |
||
| 411 | * expression node to process |
||
| 412 | * |
||
| 413 | * @return AbstractExpression|null |
||
| 414 | */ |
||
| 415 | private function processUnaryNode( |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Merge two null check expression trees by removing duplicate nodes. |
||
| 453 | * |
||
| 454 | * @param AbstractExpression $nullCheckExpTree1 First expression |
||
| 455 | * @param AbstractExpression $nullCheckExpTree2 Second expression |
||
| 456 | * |
||
| 457 | * @return AbstractExpression |
||
| 458 | */ |
||
| 459 | private function mergeNullableExpressionTrees( |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Populate map table. |
||
| 501 | * |
||
| 502 | * @param AbstractExpression $nullCheckExpTree The expression to verfiy |
||
| 503 | * |
||
| 504 | * @throws ODataException |
||
| 505 | */ |
||
| 506 | private function map($nullCheckExpTree) |
||
| 531 | } |
||
| 532 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: