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