Complex classes like OrderByParser 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 OrderByParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class OrderByParser |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Collection of anonymous sorter function corresponding to |
||
| 32 | * each orderby path segment. |
||
| 33 | * |
||
| 34 | * @var callable[] |
||
| 35 | */ |
||
| 36 | private $comparisonFunctions = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The top level sorter function generated from orderby path |
||
| 40 | * segments. |
||
| 41 | * |
||
| 42 | * @var callable |
||
| 43 | */ |
||
| 44 | private $topLevelComparisonFunction; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The structure holds information about the navigation properties |
||
| 48 | * used in the orderby clause (if any) and orderby path if IDSQP |
||
| 49 | * implementor want to perform sorting. |
||
| 50 | * |
||
| 51 | * @var OrderByInfo |
||
| 52 | */ |
||
| 53 | private $orderByInfo; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Reference to metadata and query provider wrapper. |
||
| 57 | * |
||
| 58 | * @var ProvidersWrapper |
||
| 59 | */ |
||
| 60 | private $providerWrapper; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * This object will be of type of the resource set identified by the |
||
| 64 | * request uri. |
||
| 65 | * |
||
| 66 | * @var mixed |
||
| 67 | */ |
||
| 68 | private $dummyObject; |
||
| 69 | |||
| 70 | /* |
||
| 71 | * Root node for tree ordering |
||
| 72 | * |
||
| 73 | * @var mixed |
||
| 74 | */ |
||
| 75 | private $rootOrderByNode; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Creates new instance of OrderByParser. |
||
| 79 | * |
||
| 80 | * @param ProvidersWrapper $providerWrapper Reference to metadata |
||
| 81 | * and query provider |
||
| 82 | * wrapper |
||
| 83 | */ |
||
| 84 | private function __construct(ProvidersWrapper $providerWrapper) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * This function perform the following tasks with the help of internal helper |
||
| 91 | * functions |
||
| 92 | * (1) Read the orderby clause and perform basic syntax errors |
||
| 93 | * (2) Build 'Order By Tree', creates anonymous sorter function for each leaf |
||
| 94 | * node and check for error |
||
| 95 | * (3) Build 'OrderInfo' structure, holds information about the navigation |
||
| 96 | * properties used in the orderby clause (if any) and orderby path if |
||
| 97 | * IDSQP implementor want to perform sorting |
||
| 98 | * (4) Build top level anonymous sorter function |
||
| 99 | * (4) Release resources hold by the 'Order By Tree' |
||
| 100 | * (5) Create 'InternalOrderInfo' structure, which wraps 'OrderInfo' and top |
||
| 101 | * level sorter function. |
||
| 102 | * |
||
| 103 | * @param ResourceSetWrapper $resourceSetWrapper ResourceSetWrapper for the resource targeted by resource path |
||
| 104 | * @param ResourceType $resourceType ResourceType for the resource targeted by resource path |
||
| 105 | * @param string $orderBy The orderby clause |
||
| 106 | * @param ProvidersWrapper $providerWrapper Reference to the wrapper for IDSQP and IDSMP impl |
||
| 107 | * |
||
| 108 | * @throws ODataException If any error occur while parsing orderby clause |
||
| 109 | * |
||
| 110 | * @return InternalOrderByInfo |
||
| 111 | */ |
||
| 112 | public static function parseOrderByClause( |
||
| 113 | ResourceSetWrapper $resourceSetWrapper, |
||
| 114 | ResourceType $resourceType, |
||
| 115 | $orderBy, |
||
| 116 | ProvidersWrapper $providerWrapper |
||
| 117 | ) { |
||
| 118 | assert(is_string($orderBy), "OrderBy clause must be a string"); |
||
| 119 | $orderByParser = new self($providerWrapper); |
||
| 120 | try { |
||
| 121 | $orderByParser->dummyObject = $resourceType->getInstanceType()->newInstance(); |
||
|
|
|||
| 122 | } catch (\ReflectionException $reflectionException) { |
||
| 123 | throw ODataException::createInternalServerError(Messages::orderByParserFailedToCreateDummyObject()); |
||
| 124 | } |
||
| 125 | $orderByParser->rootOrderByNode = new OrderByRootNode($resourceSetWrapper, $resourceType); |
||
| 126 | $orderByPathSegments = $orderByParser->readOrderBy($orderBy); |
||
| 127 | |||
| 128 | $orderByParser->buildOrderByTree($orderByPathSegments); |
||
| 129 | $orderByParser->createOrderInfo($orderByPathSegments); |
||
| 130 | $orderByParser->generateTopLevelComparisonFunction(); |
||
| 131 | //Recursively release the resources |
||
| 132 | $orderByParser->rootOrderByNode->free(); |
||
| 133 | //creates internal order info wrapper |
||
| 134 | $internalOrderInfo = new InternalOrderByInfo( |
||
| 135 | $orderByParser->orderByInfo, |
||
| 136 | $orderByParser->comparisonFunctions, |
||
| 137 | $orderByParser->topLevelComparisonFunction, |
||
| 138 | $orderByParser->dummyObject, |
||
| 139 | $resourceType |
||
| 140 | ); |
||
| 141 | unset($orderByParser->orderByInfo); |
||
| 142 | unset($orderByParser->topLevelComparisonFunction); |
||
| 143 | |||
| 144 | return $internalOrderInfo; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Build 'OrderBy Tree' from the given orderby path segments, also build |
||
| 149 | * comparsion function for each path segment. |
||
| 150 | * |
||
| 151 | * @param array(array) &$orderByPathSegments Collection of orderby path segments, |
||
| 152 | * this is passed by reference |
||
| 153 | * since we need this function to |
||
| 154 | * modify this array in two cases: |
||
| 155 | * 1. if asc or desc present, then the |
||
| 156 | * corresponding sub path segment |
||
| 157 | * should be removed |
||
| 158 | * 2. remove duplicate orderby path |
||
| 159 | * segment |
||
| 160 | * |
||
| 161 | * @throws ODataException If any error occurs while processing the orderby path |
||
| 162 | * segments |
||
| 163 | */ |
||
| 164 | private function buildOrderByTree(&$orderByPathSegments) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Traverse 'Order By Tree' and create 'OrderInfo' structure. |
||
| 359 | * |
||
| 360 | * @param array(array) $orderByPaths The orderby paths |
||
| 361 | * |
||
| 362 | * @throws ODataException In case parser found any tree inconsisitent |
||
| 363 | * state, throws unexpected state error |
||
| 364 | * |
||
| 365 | * @return OrderByInfo |
||
| 366 | */ |
||
| 367 | private function createOrderInfo($orderByPaths) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Generates top level comparison function from sub comparison functions. |
||
| 403 | */ |
||
| 404 | private function generateTopLevelComparisonFunction() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Read orderby clause. |
||
| 427 | * |
||
| 428 | * @param string $value orderby clause to read |
||
| 429 | * |
||
| 430 | * @throws ODataException If any syntax error found while reading the clause |
||
| 431 | * |
||
| 432 | * @return array(array) An array of 'OrderByPathSegment's, each of which |
||
| 433 | * is array of 'OrderBySubPathSegment's |
||
| 434 | */ |
||
| 435 | private function readOrderBy($value) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Assert that the given condition is true, if false throw |
||
| 480 | * ODataException for unexpected state. |
||
| 481 | * |
||
| 482 | * @param bool $condition The condition to assert |
||
| 483 | * |
||
| 484 | * @throws ODataException |
||
| 485 | */ |
||
| 486 | private function assertion($condition) |
||
| 492 | } |
||
| 493 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: