Complex classes like SMWQueryProcessor 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 SMWQueryProcessor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class SMWQueryProcessor implements QueryContext { |
||
|
|||
28 | |||
29 | /** |
||
30 | * Takes an array of unprocessed parameters, processes them using |
||
31 | * Validator, and returns them. |
||
32 | * |
||
33 | * Both input and output arrays are |
||
34 | * param name (string) => param value (mixed) |
||
35 | * |
||
36 | * @since 1.6.2 |
||
37 | * The return value changed in SMW 1.8 from an array with result values |
||
38 | * to an array with Param objects. |
||
39 | * |
||
40 | * @param array $params |
||
41 | * @param array $printRequests |
||
42 | * @param boolean $unknownInvalid |
||
43 | * |
||
44 | * @return Param[] |
||
45 | */ |
||
46 | 111 | public static function getProcessedParams( array $params, array $printRequests = array(), $unknownInvalid = true ) { |
|
47 | 111 | $validator = self::getValidatorForParams( $params, $printRequests, $unknownInvalid ); |
|
48 | 111 | $validator->processParameters(); |
|
49 | 111 | return $validator->getParameters(); |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * Takes an array of unprocessed parameters, |
||
54 | * and sets them on a new Validator object, |
||
55 | * which is returned and ready to process the parameters. |
||
56 | * |
||
57 | * @since 1.8 |
||
58 | * |
||
59 | * @param array $params |
||
60 | * @param array $printRequests |
||
61 | * @param boolean $unknownInvalid |
||
62 | * |
||
63 | * @return Processor |
||
64 | */ |
||
65 | 111 | public static function getValidatorForParams( array $params, array $printRequests = array(), $unknownInvalid = true ) { |
|
66 | 111 | $paramDefinitions = self::getParameters(); |
|
67 | |||
68 | 111 | $paramDefinitions['format']->setPrintRequests( $printRequests ); |
|
69 | |||
70 | 111 | $processorOptions = new Options(); |
|
71 | 111 | $processorOptions->setUnknownInvalid( $unknownInvalid ); |
|
72 | |||
73 | 111 | $validator = Processor::newFromOptions( $processorOptions ); |
|
74 | |||
75 | 111 | $validator->setParameters( $params, $paramDefinitions, false ); |
|
76 | |||
77 | 111 | return $validator; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Parse a query string given in SMW's query language to create |
||
82 | * an SMWQuery. Parameters are given as key-value-pairs in the |
||
83 | * given array. The parameter $context defines in what context the |
||
84 | * query is used, which affects ceretain general settings. |
||
85 | * An object of type SMWQuery is returned. |
||
86 | * |
||
87 | * The format string is used to specify the output format if already |
||
88 | * known. Otherwise it will be determined from the parameters when |
||
89 | * needed. This parameter is just for optimisation in a common case. |
||
90 | * |
||
91 | * @param string $queryString |
||
92 | * @param array $params These need to be the result of a list fed to getProcessedParams |
||
93 | * @param $context |
||
94 | * @param string $format |
||
95 | * @param array $extraPrintouts |
||
96 | * |
||
97 | * @return SMWQuery |
||
98 | */ |
||
99 | 111 | static public function createQuery( $queryString, array $params, $context = self::INLINE_QUERY, $format = '', array $extraPrintouts = array(), $contextPage = null ) { |
|
100 | |||
101 | 111 | if ( $format === '' || is_null( $format ) ) { |
|
102 | 109 | $format = $params['format']->getValue(); |
|
103 | } |
||
104 | |||
105 | 111 | $defaultSort = 'ASC'; |
|
106 | |||
107 | 111 | if ( $format == 'count' ) { |
|
108 | 2 | $queryMode = SMWQuery::MODE_COUNT; |
|
109 | 110 | } elseif ( $format == 'debug' ) { |
|
110 | 1 | $queryMode = SMWQuery::MODE_DEBUG; |
|
111 | } else { |
||
112 | 109 | $printer = self::getResultPrinter( $format, $context ); |
|
113 | 109 | $queryMode = $printer->getQueryMode( $context ); |
|
114 | 109 | $defaultSort = $printer->getDefaultSort(); |
|
115 | } |
||
116 | |||
117 | // set mode, limit, and offset: |
||
118 | 111 | $offset = 0; |
|
119 | 111 | $limit = $GLOBALS['smwgQDefaultLimit']; |
|
120 | |||
121 | 111 | if ( ( array_key_exists( 'offset', $params ) ) && ( is_int( $params['offset']->getValue() + 0 ) ) ) { |
|
122 | 111 | $offset = $params['offset']->getValue(); |
|
123 | } |
||
124 | |||
125 | 111 | if ( ( array_key_exists( 'limit', $params ) ) && ( is_int( trim( $params['limit']->getValue() ) + 0 ) ) ) { |
|
126 | 111 | $limit = $params['limit']->getValue(); |
|
127 | |||
128 | // limit < 0: always show further results link only |
||
129 | 111 | if ( ( trim( $params['limit']->getValue() ) + 0 ) < 0 ) { |
|
130 | $queryMode = SMWQuery::MODE_NONE; |
||
131 | } |
||
132 | } |
||
133 | |||
134 | // largest possible limit for "count", even inline |
||
135 | 111 | if ( $queryMode == SMWQuery::MODE_COUNT ) { |
|
136 | 2 | $offset = 0; |
|
137 | 2 | $limit = $GLOBALS['smwgQMaxLimit']; |
|
138 | } |
||
139 | |||
140 | 111 | $queryCreator = ApplicationFactory::getInstance()->getQueryFactory()->newQueryCreator(); |
|
141 | |||
142 | 111 | $queryCreator->setConfiguration( array( |
|
143 | 111 | 'extraPrintouts' => $extraPrintouts, |
|
144 | 111 | 'queryMode' => $queryMode, |
|
145 | 111 | 'context' => $context, |
|
146 | 111 | 'contextPage' => $contextPage, |
|
147 | 111 | 'offset' => $offset, |
|
148 | 111 | 'limit' => $limit, |
|
149 | 111 | 'querySource' => $params['source']->getValue(), |
|
150 | 111 | 'mainLabel' => $params['mainlabel']->getValue(), |
|
151 | 111 | 'sort' => $params['sort']->getValue(), |
|
152 | 111 | 'order' => $params['order']->getValue(), |
|
153 | 111 | 'defaultSort' => $defaultSort |
|
154 | ) ); |
||
155 | |||
156 | 111 | return $queryCreator->create( $queryString ); |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * @deprecated since 2.5, This method should no longer be used but since it |
||
161 | * was made protected (and therefore can be derived from) it will remain until |
||
162 | * 3.0 to avoid a breaking BC. |
||
163 | * |
||
164 | * Takes the sort and order parameters and returns a list of sort keys and a list of errors. |
||
165 | * |
||
166 | * @since 1.7 |
||
167 | * |
||
168 | * @param array $sortParam |
||
169 | * @param array $orders |
||
170 | * @param string $defaultSort |
||
171 | * |
||
172 | * @return array ( keys => array(), errors => array() ) |
||
173 | */ |
||
174 | protected static function getSortKeys( array $sortParam, array $orderParam, $defaultSort ) { |
||
175 | return ApplicationFactory::getInstance()->getQueryFactory()->newConfigurableQueryCreator()->getSortKeys( $sortParam, $orderParam, $defaultSort ); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Add the subject print request, unless mainlabel is set to "-". |
||
180 | * |
||
181 | * @since 1.7 |
||
182 | * |
||
183 | * @param array $printRequests |
||
184 | * @param array $rawParams |
||
185 | */ |
||
186 | 104 | public static function addThisPrintout( array &$printRequests, array $rawParams ) { |
|
187 | 104 | if ( !is_null( $printRequests ) ) { |
|
188 | 104 | $hasMainlabel = array_key_exists( 'mainlabel', $rawParams ); |
|
189 | |||
190 | 104 | if ( !$hasMainlabel || trim( $rawParams['mainlabel'] ) !== '-' ) { |
|
191 | 103 | array_unshift( $printRequests, new PrintRequest( |
|
192 | 103 | PrintRequest::PRINT_THIS, |
|
193 | 103 | $hasMainlabel ? $rawParams['mainlabel'] : '' |
|
194 | ) ); |
||
195 | } |
||
196 | } |
||
197 | 104 | } |
|
198 | |||
199 | /** |
||
200 | * Preprocess a query as given by an array of parameters as is typically |
||
201 | * produced by the #ask parser function. The parsing results in a querystring, |
||
202 | * an array of additional parameters, and an array of additional SMWPrintRequest |
||
203 | * objects, which are filled into call-by-ref parameters. |
||
204 | * $showMode is true if the input should be treated as if given by #show |
||
205 | * |
||
206 | * @param array $rawParams |
||
207 | * @param string $querystring |
||
208 | * @param array $params |
||
209 | * @param array $printouts array of SMWPrintRequest |
||
210 | * @param boolean $showMode |
||
211 | * @deprecated Will vanish after SMW 1.8 is released. |
||
212 | * Use getComponentsFromFunctionParams which has a cleaner interface. |
||
213 | */ |
||
214 | static public function processFunctionParams( array $rawParams, &$querystring, &$params, &$printouts, $showMode = false ) { |
||
215 | list( $querystring, $params, $printouts ) = self::getComponentsFromFunctionParams( $rawParams, $showMode ); |
||
216 | } |
||
217 | |||
218 | |||
219 | /** |
||
220 | * Preprocess a query as given by an array of parameters as is |
||
221 | * typically produced by the #ask parser function or by Special:Ask. |
||
222 | * The parsing results in a querystring, an array of additional |
||
223 | * parameters, and an array of additional SMWPrintRequest objects, |
||
224 | * which are returned in an array with three components. If |
||
225 | * $showMode is true then the input will be processed as if for #show. |
||
226 | * This uses a slightly different way to get the query, and different |
||
227 | * default labels (empty) for additional print requests. |
||
228 | * |
||
229 | * @param array $rawParams |
||
230 | * @param boolean $showMode |
||
231 | * @return array( string, array( string => string ), array( SMWPrintRequest ) ) |
||
232 | */ |
||
233 | 119 | static public function getComponentsFromFunctionParams( array $rawParams, $showMode ) { |
|
234 | 119 | $queryString = ''; |
|
235 | 119 | $parameters = array(); |
|
236 | 119 | $printouts = array(); |
|
237 | |||
238 | 119 | $lastprintout = null; |
|
239 | 119 | $printRequestFactory = new PrintRequestFactory(); |
|
240 | |||
241 | 119 | foreach ( $rawParams as $name => $rawParam ) { |
|
242 | // special handling for arrays - this can happen if the |
||
243 | // parameter came from a checkboxes input in Special:Ask: |
||
244 | 118 | if ( is_array( $rawParam ) ) { |
|
245 | $rawParam = implode( ',', array_keys( $rawParam ) ); |
||
246 | } |
||
247 | |||
248 | // Bug 32955 / #640 |
||
249 | // Modify (e.g. replace `=`) a condition string only if enclosed by [[ ... ]] |
||
250 | 118 | $rawParam = preg_replace_callback( |
|
251 | 118 | '/\[\[([^\[\]]*)\]\]/xu', |
|
252 | 118 | function( array $matches ) { |
|
253 | 113 | return str_replace( array( '=' ), array( '-3D' ), $matches[0] ); |
|
254 | 118 | }, |
|
255 | $rawParam |
||
256 | ); |
||
257 | |||
258 | // #1258 (named_args -> named args) |
||
259 | // accept 'name' => 'value' just as '' => 'name=value': |
||
260 | 118 | if ( is_string( $name ) && ( $name !== '' ) ) { |
|
261 | $rawParam = str_replace( "_", " ", $name ) . '=' . $rawParam; |
||
262 | } |
||
263 | |||
264 | 118 | if ( $rawParam === '' ) { |
|
265 | 118 | } elseif ( $rawParam { 0 } == '?' ) { // print statement |
|
266 | 85 | $rawParam = substr( $rawParam, 1 ); |
|
267 | 85 | $lastprintout = $printRequestFactory->newPrintRequestFromText( $rawParam, $showMode ); |
|
268 | 85 | if ( !is_null( $lastprintout ) ) { |
|
269 | 85 | $printouts[] = $lastprintout; |
|
270 | } |
||
271 | 118 | } elseif ( $rawParam[0] == '+' ) { // print request parameter |
|
272 | 10 | if ( !is_null( $lastprintout ) ) { |
|
273 | 9 | $rawParam = substr( $rawParam, 1 ); |
|
274 | 9 | $parts = explode( '=', $rawParam, 2 ); |
|
275 | 9 | if ( count( $parts ) == 2 ) { |
|
276 | 9 | $lastprintout->setParameter( trim( $parts[0] ), $parts[1] ); |
|
277 | } else { |
||
278 | 10 | $lastprintout->setParameter( trim( $parts[0] ), null ); |
|
279 | } |
||
280 | } |
||
281 | } else { // parameter or query |
||
282 | |||
283 | // #1645 |
||
284 | 118 | $parts = $showMode && $name == 0 ? $rawParam : explode( '=', $rawParam, 2 ); |
|
285 | |||
286 | 118 | if ( count( $parts ) >= 2 ) { |
|
287 | // don't trim here, some parameters care for " " |
||
288 | 86 | $parameters[strtolower( trim( $parts[0] ) )] = $parts[1]; |
|
289 | } else { |
||
290 | 118 | $queryString .= $rawParam; |
|
291 | } |
||
292 | } |
||
293 | } |
||
294 | |||
295 | 119 | $queryString = str_replace( array( '<', '>', '-3D' ), array( '<', '>', '=' ), $queryString ); |
|
296 | |||
297 | 119 | if ( $showMode ) { |
|
298 | 13 | $queryString = "[[:$queryString]]"; |
|
299 | } |
||
300 | |||
301 | 119 | return array( $queryString, $parameters, $printouts); |
|
302 | } |
||
303 | |||
304 | /** |
||
305 | * Process and answer a query as given by an array of parameters as is |
||
306 | * typically produced by the #ask parser function. The parameter |
||
307 | * $context defines in what context the query is used, which affects |
||
308 | * certain general settings. |
||
309 | * |
||
310 | * The main task of this function is to preprocess the raw parameters |
||
311 | * to obtain actual parameters, printout requests, and the query string |
||
312 | * for further processing. |
||
313 | * |
||
314 | * @since 1.8 |
||
315 | * @param array $rawParams user-provided list of unparsed parameters |
||
316 | * @param integer $outputMode SMW_OUTPUT_WIKI, SMW_OUTPUT_HTML, ... |
||
317 | * @param integer $context INLINE_QUERY, SPECIAL_PAGE, CONCEPT_DESC |
||
318 | * @param boolean $showMode process like #show parser function? |
||
319 | * @return array( SMWQuery, array of IParam ) |
||
320 | */ |
||
321 | 105 | static public function getQueryAndParamsFromFunctionParams( array $rawParams, $outputMode, $context, $showMode, $contextPage = null ) { |
|
333 | |||
334 | /** |
||
335 | * Process and answer a query as given by an array of parameters as is |
||
336 | * typically produced by the #ask parser function. The result is formatted |
||
337 | * according to the specified $outputformat. The parameter $context defines |
||
338 | * in what context the query is used, which affects ceretain general settings. |
||
339 | * |
||
340 | * The main task of this function is to preprocess the raw parameters to |
||
341 | * obtain actual parameters, printout requests, and the query string for |
||
342 | * further processing. |
||
343 | * |
||
344 | * @note Consider using getQueryAndParamsFromFunctionParams() and |
||
345 | * getResultFromQuery() instead. |
||
346 | * @deprecated Will vanish after release of SMW 1.8. |
||
347 | * See SMW_Ask.php for example code on how to get query results from |
||
348 | * #ask function parameters. |
||
349 | */ |
||
350 | static public function getResultFromFunctionParams( array $rawParams, $outputMode, $context = self::INLINE_QUERY, $showMode = false ) { |
||
361 | |||
362 | /** |
||
363 | * Process a query string in SMW's query language and return a formatted |
||
364 | * result set as specified by $outputmode. A parameter array of key-value-pairs |
||
365 | * constrains the query and determines the serialisation mode for results. The |
||
366 | * parameter $context defines in what context the query is used, which affects |
||
367 | * certain general settings. Finally, $extraprintouts supplies additional |
||
368 | * printout requests for the query results. |
||
369 | * |
||
370 | * @param string $queryString |
||
371 | * @param array $params These need to be the result of a list fed to getProcessedParams |
||
372 | * @param $extraPrintouts |
||
373 | * @param $outputMode |
||
374 | * @param $context |
||
375 | * |
||
376 | * @return string |
||
377 | * @deprecated Will vanish after release of SMW 1.8. |
||
378 | * See SMW_Ask.php for example code on how to get query results from |
||
379 | * #ask function parameters. |
||
380 | */ |
||
381 | static public function getResultFromQueryString( $queryString, array $params, $extraPrintouts, $outputMode, $context = self::INLINE_QUERY ) { |
||
389 | |||
390 | /** |
||
391 | * Create a fully formatted result string from a query and its |
||
392 | * parameters. The method takes care of processing various types of |
||
393 | * query result. Most cases are handled by printers, but counting and |
||
394 | * debugging uses special code. |
||
395 | * |
||
396 | * @param SMWQuery $query |
||
397 | * @param array $params These need to be the result of a list fed to getProcessedParams |
||
398 | * @param integer $outputMode |
||
399 | * @param integer $context |
||
400 | * @since before 1.7, but public only since 1.8 |
||
401 | * |
||
402 | * @return string |
||
403 | */ |
||
404 | 96 | public static function getResultFromQuery( SMWQuery $query, array $params, $outputMode, $context ) { |
|
442 | |||
443 | 96 | private static function getStoreFromParams( array $params ) { |
|
446 | |||
447 | /** |
||
448 | * Find suitable SMWResultPrinter for the given format. The context in |
||
449 | * which the query is to be used determines some basic settings of the |
||
450 | * returned printer object. Possible contexts are |
||
451 | * SMWQueryProcessor::SPECIAL_PAGE, SMWQueryProcessor::INLINE_QUERY, |
||
452 | * SMWQueryProcessor::CONCEPT_DESC. |
||
453 | * |
||
454 | * @param string $format |
||
455 | * @param $context |
||
456 | * |
||
457 | * @return SMWResultPrinter |
||
458 | * @throws MissingResultFormatException |
||
459 | */ |
||
460 | 113 | static public function getResultPrinter( $format, $context = self::SPECIAL_PAGE ) { |
|
471 | |||
472 | /** |
||
473 | * A function to describe the allowed parameters of a query using |
||
474 | * any specific format - most query printers should override this |
||
475 | * function. |
||
476 | * |
||
477 | * @since 1.6.2, return element type changed in 1.8 |
||
478 | * |
||
479 | * @return IParamDefinition[] |
||
480 | */ |
||
481 | 113 | public static function getParameters() { |
|
569 | |||
570 | 113 | private static function getSourceParam() { |
|
578 | |||
579 | /** |
||
580 | * Returns the definitions of all parameters supported by the specified format. |
||
581 | * |
||
582 | * @since 1.8 |
||
583 | * |
||
584 | * @param string $format |
||
585 | * |
||
586 | * @return ParamDefinition[] |
||
587 | */ |
||
588 | 1 | public static function getFormatParameters( $format ) { |
|
599 | |||
600 | } |
||
601 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.