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 = array(); |
||
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 | * Creates new instance of OrderByParser. |
||
72 | * |
||
73 | * @param ProvidersWrapper $providerWrapper Reference to metadata |
||
74 | * and query provider |
||
75 | * wrapper |
||
76 | */ |
||
77 | private function __construct(ProvidersWrapper $providerWrapper) |
||
78 | { |
||
79 | $this->_providerWrapper = $providerWrapper; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * This function perform the following tasks with the help of internal helper |
||
84 | * functions |
||
85 | * (1) Read the orderby clause and perform basic syntax errors |
||
86 | * (2) Build 'Order By Tree', creates anonymous sorter function for each leaf |
||
87 | * node and check for error |
||
88 | * (3) Build 'OrderInfo' structure, holds information about the navigation |
||
89 | * properties used in the orderby clause (if any) and orderby path if |
||
90 | * IDSQP implementor want to perform sorting |
||
91 | * (4) Build top level anonymous sorter function |
||
92 | * (4) Release resources hold by the 'Order By Tree' |
||
93 | * (5) Create 'InternalOrderInfo' structure, which wraps 'OrderInfo' and top |
||
94 | * level sorter function. |
||
95 | * |
||
96 | * @param ResourceSetWrapper $resourceSetWrapper ResourceSetWrapper for the resource targeted by resource path |
||
97 | * @param ResourceType $resourceType ResourceType for the resource targeted by resource path |
||
98 | * @param string $orderBy The orderby clause |
||
99 | * @param ProvidersWrapper $providerWrapper Reference to the wrapper for IDSQP and IDSMP impl |
||
100 | * |
||
101 | * @return InternalOrderByInfo |
||
102 | * |
||
103 | * @throws ODataException If any error occur while parsing orderby clause |
||
104 | */ |
||
105 | public static function parseOrderByClause( |
||
106 | ResourceSetWrapper $resourceSetWrapper, |
||
107 | ResourceType $resourceType, |
||
108 | $orderBy, |
||
109 | ProvidersWrapper $providerWrapper |
||
110 | ) { |
||
111 | $orderByParser = new self($providerWrapper); |
||
112 | try { |
||
113 | $orderByParser->_dummyObject = $resourceType->getInstanceType()->newInstance(); |
||
|
|||
114 | } catch (\ReflectionException $reflectionException) { |
||
115 | throw ODataException::createInternalServerError(Messages::orderByParserFailedToCreateDummyObject()); |
||
116 | } |
||
117 | $orderByParser->_rootOrderByNode = new OrderByRootNode($resourceSetWrapper, $resourceType); |
||
118 | $orderByPathSegments = $orderByParser->_readOrderBy($orderBy); |
||
119 | |||
120 | $orderByParser->_buildOrderByTree($orderByPathSegments); |
||
121 | $orderByParser->_createOrderInfo($orderByPathSegments); |
||
122 | $orderByParser->_generateTopLevelComparisonFunction(); |
||
123 | //Recursively release the resources |
||
124 | $orderByParser->_rootOrderByNode->free(); |
||
125 | //creates internal order info wrapper |
||
126 | $internalOrderInfo = new InternalOrderByInfo( |
||
127 | $orderByParser->_orderByInfo, |
||
128 | $orderByParser->_comparisonFunctions, |
||
129 | $orderByParser->_topLevelComparisonFunction, |
||
130 | $orderByParser->_dummyObject, |
||
131 | $resourceType |
||
132 | ); |
||
133 | unset($orderByParser->_orderByInfo); |
||
134 | unset($orderByParser->_topLevelComparisonFunction); |
||
135 | |||
136 | return $internalOrderInfo; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Build 'OrderBy Tree' from the given orderby path segments, also build |
||
141 | * comparsion function for each path segment. |
||
142 | * |
||
143 | * @param array(array) &$orderByPathSegments Collection of orderby path segments, |
||
144 | * this is passed by reference |
||
145 | * since we need this function to |
||
146 | * modify this array in two cases: |
||
147 | * 1. if asc or desc present, then the |
||
148 | * corresponding sub path segment |
||
149 | * should be removed |
||
150 | * 2. remove duplicate orderby path |
||
151 | * segment |
||
152 | * |
||
153 | * @throws ODataException If any error occurs while processing the orderby path |
||
154 | * segments |
||
155 | */ |
||
156 | private function _buildOrderByTree(&$orderByPathSegments) |
||
351 | |||
352 | /** |
||
353 | * Traverse 'Order By Tree' and create 'OrderInfo' structure. |
||
354 | * |
||
355 | * @param array(array) $orderByPaths The orderby paths |
||
356 | * |
||
357 | * @return OrderByInfo |
||
358 | * |
||
359 | * @throws ODataException In case parser found any tree inconsisitent |
||
360 | * state, throws unexpected state error |
||
361 | */ |
||
362 | private function _createOrderInfo($orderByPaths) |
||
392 | |||
393 | /** |
||
394 | * Generates top level comparison function from sub comparison functions. |
||
395 | */ |
||
396 | private function _generateTopLevelComparisonFunction() |
||
417 | |||
418 | /** |
||
419 | * Read orderby clause. |
||
420 | * |
||
421 | * @param string $value orderby clause to read |
||
422 | * |
||
423 | * @return array(array) An array of 'OrderByPathSegment's, each of which |
||
424 | * is array of 'OrderBySubPathSegment's |
||
425 | * |
||
426 | * @throws ODataException If any syntax error found while reading the clause |
||
427 | */ |
||
428 | private function _readOrderBy($value) |
||
470 | |||
471 | /** |
||
472 | * Assert that the given condition is true, if false throw |
||
473 | * ODataException for unexpected state. |
||
474 | * |
||
475 | * @param bool $condition The condition to assert |
||
476 | * |
||
477 | * @throws ODataException |
||
478 | */ |
||
479 | private function _assertion($condition) |
||
485 | } |
||
486 |
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: