Complex classes like CompoundConditionBuilder 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 CompoundConditionBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class CompoundConditionBuilder { |
||
38 | |||
39 | /** |
||
40 | * @var EngineOptions |
||
41 | */ |
||
42 | private $engineOptions; |
||
43 | |||
44 | /** |
||
45 | * @var DispatchingDescriptionInterpreter |
||
46 | */ |
||
47 | private $dispatchingDescriptionInterpreter; |
||
48 | |||
49 | /** |
||
50 | * @var CircularReferenceGuard |
||
51 | */ |
||
52 | private $circularReferenceGuard; |
||
53 | |||
54 | /** |
||
55 | * @var PropertyHierarchyLookup |
||
56 | */ |
||
57 | private $propertyHierarchyLookup; |
||
58 | |||
59 | /** |
||
60 | * @var DescriptionFactory |
||
61 | */ |
||
62 | private $descriptionFactory; |
||
63 | |||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | private $errors = array(); |
||
68 | |||
69 | /** |
||
70 | * Counter used to generate globally fresh variables. |
||
71 | * @var integer |
||
72 | */ |
||
73 | private $variableCounter = 0; |
||
74 | |||
75 | /** |
||
76 | * sortKeys that are being used while building the query conditions |
||
77 | * @var array |
||
78 | */ |
||
79 | private $sortKeys = array(); |
||
80 | |||
81 | /** |
||
82 | * The name of the SPARQL variable that represents the query result |
||
83 | * @var string |
||
84 | */ |
||
85 | private $resultVariable = 'result'; |
||
86 | |||
87 | /** |
||
88 | * @var string |
||
89 | */ |
||
90 | private $joinVariable; |
||
91 | |||
92 | /** |
||
93 | * @var DIProperty|null |
||
94 | */ |
||
95 | private $orderByProperty; |
||
96 | |||
97 | /** |
||
98 | * @var array |
||
99 | */ |
||
100 | private $redirectByVariableReplacementMap = array(); |
||
101 | |||
102 | /** |
||
103 | * @since 2.2 |
||
104 | * |
||
105 | * @param DescriptionInterpreterFactory $descriptionInterpreterFactory |
||
106 | * @param EngineOptions|null $engineOptions |
||
107 | */ |
||
108 | 33 | public function __construct( DescriptionInterpreterFactory $descriptionInterpreterFactory, EngineOptions $engineOptions = null ) { |
|
118 | |||
119 | /** |
||
120 | * @since 2.0 |
||
121 | * |
||
122 | * @param string $resultVariable |
||
123 | */ |
||
124 | 5 | public function setResultVariable( $resultVariable ) { |
|
128 | |||
129 | /** |
||
130 | * Get a fresh unused variable name for building SPARQL conditions. |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | 24 | public function getNextVariable( $prefix = 'v' ) { |
|
137 | |||
138 | /** |
||
139 | * @since 2.0 |
||
140 | * |
||
141 | * @param array $sortKeys |
||
142 | */ |
||
143 | 5 | public function setSortKeys( $sortKeys ) { |
|
147 | |||
148 | /** |
||
149 | * @since 2.1 |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | 19 | public function getSortKeys() { |
|
156 | |||
157 | /** |
||
158 | * @since 2.2 |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | 1 | public function getErrors() { |
|
165 | |||
166 | /** |
||
167 | * @since 2.2 |
||
168 | * |
||
169 | * @param string $error |
||
170 | */ |
||
171 | public function addError( $error, $type = Message::TEXT ) { |
||
174 | |||
175 | /** |
||
176 | * @since 2.2 |
||
177 | * |
||
178 | * @param CircularReferenceGuard $circularReferenceGuard |
||
179 | */ |
||
180 | 5 | public function setCircularReferenceGuard( CircularReferenceGuard $circularReferenceGuard ) { |
|
183 | |||
184 | /** |
||
185 | * @since 2.2 |
||
186 | * |
||
187 | * @return CircularReferenceGuard |
||
188 | */ |
||
189 | public function getCircularReferenceGuard() { |
||
192 | |||
193 | /** |
||
194 | * @since 2.3 |
||
195 | * |
||
196 | * @param PropertyHierarchyLookup $propertyHierarchyLookup |
||
197 | */ |
||
198 | 5 | public function setPropertyHierarchyLookup( PropertyHierarchyLookup $propertyHierarchyLookup ) { |
|
201 | |||
202 | /** |
||
203 | * @since 2.3 |
||
204 | * |
||
205 | * @return PropertyHierarchyLookup |
||
206 | */ |
||
207 | 18 | public function getPropertyHierarchyLookup() { |
|
210 | |||
211 | /** |
||
212 | * @since 2.2 |
||
213 | * |
||
214 | * @param string $joinVariable name of the variable that conditions |
||
215 | * will refer to |
||
216 | */ |
||
217 | 24 | public function setJoinVariable( $joinVariable ) { |
|
220 | |||
221 | /** |
||
222 | * @since 2.2 |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | 24 | public function getJoinVariable() { |
|
229 | |||
230 | /** |
||
231 | * @since 2.2 |
||
232 | * |
||
233 | * @param DIProperty|null $orderByProperty if given then |
||
234 | * this is the property the values of which this condition will refer |
||
235 | * to, and the condition should also enable ordering by this value |
||
236 | */ |
||
237 | 24 | public function setOrderByProperty( $orderByProperty ) { |
|
240 | |||
241 | /** |
||
242 | * @since 2.2 |
||
243 | * |
||
244 | * @return DIProperty|null |
||
245 | */ |
||
246 | 24 | public function getOrderByProperty() { |
|
249 | |||
250 | /** |
||
251 | * Get a Condition object for a Description. |
||
252 | * |
||
253 | * This conversion is implemented by a number of recursive functions, |
||
254 | * and this is the main entry point for this recursion. In particular, |
||
255 | * it resets global variables that are used for the construction. |
||
256 | * |
||
257 | * If property value variables should be recorded for ordering results |
||
258 | * later on, the keys of the respective properties need to be given in |
||
259 | * sortKeys earlier. |
||
260 | * |
||
261 | * @param Description $description |
||
262 | * |
||
263 | * @return Condition |
||
264 | */ |
||
265 | 24 | public function getConditionFrom( Description $description ) { |
|
287 | |||
288 | /** |
||
289 | * Recursively create a Condition from a Description |
||
290 | * |
||
291 | * @param Description $description |
||
292 | * |
||
293 | * @return Condition |
||
294 | */ |
||
295 | 24 | public function mapDescriptionToCondition( Description $description ) { |
|
298 | |||
299 | /** |
||
300 | * Build the condition (WHERE) string for a given Condition. |
||
301 | * The function also expresses the single value of |
||
302 | * SingletonCondition objects in the condition, which may |
||
303 | * lead to additional namespaces for serializing its URI. |
||
304 | * |
||
305 | * @param Condition $condition |
||
306 | * |
||
307 | * @return string |
||
308 | */ |
||
309 | 23 | public function convertConditionToString( Condition &$condition ) { |
|
340 | |||
341 | /** |
||
342 | * Create an Condition from an empty (true) description. |
||
343 | * May still require helper conditions for ordering. |
||
344 | * |
||
345 | * @param $joinVariable string name, see mapDescriptionToCondition() |
||
346 | * @param $orderByProperty mixed DIProperty or null, see mapDescriptionToCondition() |
||
347 | * |
||
348 | * @return Condition |
||
349 | */ |
||
350 | 9 | public function newTrueCondition( $joinVariable, $orderByProperty ) { |
|
355 | |||
356 | /** |
||
357 | * @since 2.3 |
||
358 | * |
||
359 | * @param DataItem|null $dataItem |
||
360 | * |
||
361 | * @return string|null |
||
362 | */ |
||
363 | 23 | public function tryToFindRedirectVariableForDataItem( DataItem $dataItem = null ) { |
|
402 | |||
403 | /** |
||
404 | * @since 2.3 |
||
405 | * |
||
406 | * @param integer $queryFeatureFlag |
||
407 | * |
||
408 | * @return boolean |
||
409 | */ |
||
410 | 21 | public function canUseQFeature( $queryFeatureFlag ) { |
|
425 | |||
426 | /** |
||
427 | * Extend the given SPARQL condition by a suitable order by variable, |
||
428 | * if an order by property is set. |
||
429 | * |
||
430 | * @param Condition $sparqlCondition condition to modify |
||
431 | * @param string $mainVariable the variable that represents the value to be ordered |
||
432 | * @param mixed $orderByProperty DIProperty or null |
||
433 | * @param integer $diType DataItem type id if known, or DataItem::TYPE_NOTYPE to determine it from the property |
||
434 | */ |
||
435 | 24 | public function addOrderByDataForProperty( Condition &$sparqlCondition, $mainVariable, $orderByProperty, $diType = DataItem::TYPE_NOTYPE ) { |
|
446 | |||
447 | /** |
||
448 | * Extend the given SPARQL condition by a suitable order by variable, |
||
449 | * possibly adding conditions if required for the type of data. |
||
450 | * |
||
451 | * @param Condition $sparqlCondition condition to modify |
||
452 | * @param string $mainVariable the variable that represents the value to be ordered |
||
453 | * @param integer $diType DataItem type id |
||
454 | */ |
||
455 | 10 | public function addOrderByData( Condition &$condition, $mainVariable, $diType ) { |
|
470 | |||
471 | /** |
||
472 | * Extend the given Condition with additional conditions to |
||
473 | * ensure that it can be ordered by all requested properties. After |
||
474 | * this operation, every key in sortKeys is assigned to a query |
||
475 | * variable by $sparqlCondition->orderVariables. |
||
476 | * |
||
477 | * @param Condition $condition condition to modify |
||
478 | */ |
||
479 | 24 | protected function addMissingOrderByConditions( Condition &$condition ) { |
|
495 | |||
496 | 3 | private function addOrderForUnknownPropertyKey( Condition &$condition, $propertyKey, $order ) { |
|
556 | |||
557 | /** |
||
558 | * @see http://www.w3.org/TR/sparql11-query/#propertypaths |
||
559 | * |
||
560 | * Query of: |
||
561 | * |
||
562 | * SELECT DISTINCT ?result WHERE { |
||
563 | * ?result swivt:wikiPageSortKey ?resultsk . |
||
564 | * { |
||
565 | * ?result property:FOO ?v1 . |
||
566 | * FILTER( ?v1sk >= "=BAR" ) |
||
567 | * ?v1 swivt:wikiPageSortKey ?v1sk . |
||
568 | * } UNION { |
||
569 | * ?result property:FOO ?v2 . |
||
570 | * } |
||
571 | * } |
||
572 | * |
||
573 | * results in: |
||
574 | * |
||
575 | * SELECT DISTINCT ?result WHERE { |
||
576 | * ?result swivt:wikiPageSortKey ?resultsk . |
||
577 | * ?r2 ^swivt:redirectsTo property:FOO . |
||
578 | * { |
||
579 | * ?result ?r2 ?v1 . |
||
580 | * FILTER( ?v1sk >= "=BAR" ) |
||
581 | * ?v1 swivt:wikiPageSortKey ?v1sk . |
||
582 | * } UNION { |
||
583 | * ?result ?r2 ?v3 . |
||
584 | * } |
||
585 | * } |
||
586 | */ |
||
587 | 23 | private function addPropertyPathToMatchRedirectTargets( Condition &$condition ) { |
|
608 | |||
609 | /** |
||
610 | * @see https://www.w3.org/TR/rdf-sparql-query/#func-bound |
||
611 | * |
||
612 | * Remove entities that contain a "swivt:redirectsTo" predicate |
||
613 | */ |
||
614 | 23 | private function addFilterToRemoveEntitiesThatContainRedirectPredicate( Condition &$condition ) { |
|
625 | |||
626 | } |
||
627 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..