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 |
||
25 | class SMWQueryProcessor { |
||
|
|||
26 | |||
27 | // "query contexts" define restrictions during query parsing and |
||
28 | // are used to preconfigure query (e.g. special pages show no further |
||
29 | // results link): |
||
30 | const SPECIAL_PAGE = 0; // query for special page |
||
31 | const INLINE_QUERY = 1; // query for inline use |
||
32 | const CONCEPT_DESC = 2; // query for concept definition |
||
33 | |||
34 | /** |
||
35 | * Takes an array of unprocessed parameters, processes them using |
||
36 | * Validator, and returns them. |
||
37 | * |
||
38 | * Both input and output arrays are |
||
39 | * param name (string) => param value (mixed) |
||
40 | * |
||
41 | * @since 1.6.2 |
||
42 | * The return value changed in SMW 1.8 from an array with result values |
||
43 | * to an array with Param objects. |
||
44 | * |
||
45 | * @param array $params |
||
46 | * @param array $printRequests |
||
47 | * @param boolean $unknownInvalid |
||
48 | * |
||
49 | * @return Param[] |
||
50 | 90 | */ |
|
51 | 90 | public static function getProcessedParams( array $params, array $printRequests = array(), $unknownInvalid = true ) { |
|
56 | |||
57 | /** |
||
58 | * Takes an array of unprocessed parameters, |
||
59 | * and sets them on a new Validator object, |
||
60 | * which is returned and ready to process the parameters. |
||
61 | * |
||
62 | * @since 1.8 |
||
63 | * |
||
64 | * @param array $params |
||
65 | * @param array $printRequests |
||
66 | * @param boolean $unknownInvalid |
||
67 | * |
||
68 | * @return Processor |
||
69 | 90 | */ |
|
70 | 90 | public static function getValidatorForParams( array $params, array $printRequests = array(), $unknownInvalid = true ) { |
|
84 | |||
85 | /** |
||
86 | * Parse a query string given in SMW's query language to create |
||
87 | * an SMWQuery. Parameters are given as key-value-pairs in the |
||
88 | * given array. The parameter $context defines in what context the |
||
89 | * query is used, which affects ceretain general settings. |
||
90 | * An object of type SMWQuery is returned. |
||
91 | * |
||
92 | * The format string is used to specify the output format if already |
||
93 | * known. Otherwise it will be determined from the parameters when |
||
94 | * needed. This parameter is just for optimisation in a common case. |
||
95 | * |
||
96 | * @param string $queryString |
||
97 | * @param array $params These need to be the result of a list fed to getProcessedParams |
||
98 | * @param $context |
||
99 | * @param string $format |
||
100 | * @param array $extraPrintouts |
||
101 | * |
||
102 | * @return SMWQuery |
||
103 | 90 | */ |
|
104 | 90 | static public function createQuery( $queryString, array $params, $context = self::INLINE_QUERY, $format = '', array $extraPrintouts = array(), $contextPage = null ) { |
|
166 | |||
167 | /** |
||
168 | * Takes the sort and order parameters and returns a list of sort keys and a list of errors. |
||
169 | * |
||
170 | * @since 1.7 |
||
171 | * |
||
172 | * @param array $sortParam |
||
173 | * @param array $orders |
||
174 | * @param string $defaultSort |
||
175 | * |
||
176 | * @return array ( keys => array(), errors => array() ) |
||
177 | 90 | */ |
|
178 | 90 | protected static function getSortKeys( array $sortParam, array $orderParam, $defaultSort ) { |
|
229 | |||
230 | /** |
||
231 | * Add the subject print request, unless mainlabel is set to "-". |
||
232 | * |
||
233 | * @since 1.7 |
||
234 | * |
||
235 | * @param array $printRequests |
||
236 | * @param array $rawParams |
||
237 | 84 | */ |
|
238 | 84 | public static function addThisPrintout( array &$printRequests, array $rawParams ) { |
|
250 | |||
251 | /** |
||
252 | * Preprocess a query as given by an array of parameters as is typically |
||
253 | * produced by the #ask parser function. The parsing results in a querystring, |
||
254 | * an array of additional parameters, and an array of additional SMWPrintRequest |
||
255 | * objects, which are filled into call-by-ref parameters. |
||
256 | * $showMode is true if the input should be treated as if given by #show |
||
257 | * |
||
258 | * @param array $rawParams |
||
259 | * @param string $querystring |
||
260 | * @param array $params |
||
261 | * @param array $printouts array of SMWPrintRequest |
||
262 | * @param boolean $showMode |
||
263 | * @deprecated Will vanish after SMW 1.8 is released. |
||
264 | * Use getComponentsFromFunctionParams which has a cleaner interface. |
||
265 | */ |
||
266 | static public function processFunctionParams( array $rawParams, &$querystring, &$params, &$printouts, $showMode = false ) { |
||
269 | |||
270 | |||
271 | /** |
||
272 | * Preprocess a query as given by an array of parameters as is |
||
273 | * typically produced by the #ask parser function or by Special:Ask. |
||
274 | * The parsing results in a querystring, an array of additional |
||
275 | * parameters, and an array of additional SMWPrintRequest objects, |
||
276 | * which are returned in an array with three components. If |
||
277 | * $showMode is true then the input will be processed as if for #show. |
||
278 | * This uses a slightly different way to get the query, and different |
||
279 | * default labels (empty) for additional print requests. |
||
280 | * |
||
281 | * @param array $rawParams |
||
282 | * @param boolean $showMode |
||
283 | * @return array( string, array( string => string ), array( SMWPrintRequest ) ) |
||
284 | 99 | */ |
|
285 | 99 | static public function getComponentsFromFunctionParams( array $rawParams, $showMode ) { |
|
354 | |||
355 | /** |
||
356 | * Process and answer a query as given by an array of parameters as is |
||
357 | * typically produced by the #ask parser function. The parameter |
||
358 | * $context defines in what context the query is used, which affects |
||
359 | * certain general settings. |
||
360 | * |
||
361 | * The main task of this function is to preprocess the raw parameters |
||
362 | * to obtain actual parameters, printout requests, and the query string |
||
363 | * for further processing. |
||
364 | * |
||
365 | * @since 1.8 |
||
366 | * @param array $rawParams user-provided list of unparsed parameters |
||
367 | * @param integer $outputMode SMW_OUTPUT_WIKI, SMW_OUTPUT_HTML, ... |
||
368 | * @param integer $context INLINE_QUERY, SPECIAL_PAGE, CONCEPT_DESC |
||
369 | * @param boolean $showMode process like #show parser function? |
||
370 | * @return array( SMWQuery, array of IParam ) |
||
371 | 86 | */ |
|
372 | 86 | static public function getQueryAndParamsFromFunctionParams( array $rawParams, $outputMode, $context, $showMode, $contextPage = null ) { |
|
384 | |||
385 | /** |
||
386 | * Process and answer a query as given by an array of parameters as is |
||
387 | * typically produced by the #ask parser function. The result is formatted |
||
388 | * according to the specified $outputformat. The parameter $context defines |
||
389 | * in what context the query is used, which affects ceretain general settings. |
||
390 | * |
||
391 | * The main task of this function is to preprocess the raw parameters to |
||
392 | * obtain actual parameters, printout requests, and the query string for |
||
393 | * further processing. |
||
394 | * |
||
395 | * @note Consider using getQueryAndParamsFromFunctionParams() and |
||
396 | * getResultFromQuery() instead. |
||
397 | * @deprecated Will vanish after release of SMW 1.8. |
||
398 | * See SMW_Ask.php for example code on how to get query results from |
||
399 | * #ask function parameters. |
||
400 | */ |
||
401 | static public function getResultFromFunctionParams( array $rawParams, $outputMode, $context = self::INLINE_QUERY, $showMode = false ) { |
||
412 | |||
413 | /** |
||
414 | * Process a query string in SMW's query language and return a formatted |
||
415 | * result set as specified by $outputmode. A parameter array of key-value-pairs |
||
416 | * constrains the query and determines the serialisation mode for results. The |
||
417 | * parameter $context defines in what context the query is used, which affects |
||
418 | * certain general settings. Finally, $extraprintouts supplies additional |
||
419 | * printout requests for the query results. |
||
420 | * |
||
421 | * @param string $queryString |
||
422 | * @param array $params These need to be the result of a list fed to getProcessedParams |
||
423 | * @param $extraPrintouts |
||
424 | * @param $outputMode |
||
425 | * @param $context |
||
426 | * |
||
427 | * @return string |
||
428 | * @deprecated Will vanish after release of SMW 1.8. |
||
429 | * See SMW_Ask.php for example code on how to get query results from |
||
430 | * #ask function parameters. |
||
431 | */ |
||
432 | static public function getResultFromQueryString( $queryString, array $params, $extraPrintouts, $outputMode, $context = self::INLINE_QUERY ) { |
||
440 | |||
441 | /** |
||
442 | * Create a fully formatted result string from a query and its |
||
443 | * parameters. The method takes care of processing various types of |
||
444 | * query result. Most cases are handled by printers, but counting and |
||
445 | * debugging uses special code. |
||
446 | * |
||
447 | * @param SMWQuery $query |
||
448 | * @param array $params These need to be the result of a list fed to getProcessedParams |
||
449 | * @param integer $outputMode |
||
450 | * @param integer $context |
||
451 | * @since before 1.7, but public only since 1.8 |
||
452 | * |
||
453 | * @return string |
||
454 | 77 | */ |
|
455 | public static function getResultFromQuery( SMWQuery $query, array $params, $outputMode, $context ) { |
||
493 | 77 | ||
494 | 77 | private static function getStoreFromParams( array $params ) { |
|
497 | |||
498 | /** |
||
499 | * Find suitable SMWResultPrinter for the given format. The context in |
||
500 | * which the query is to be used determines some basic settings of the |
||
501 | * returned printer object. Possible contexts are |
||
502 | * SMWQueryProcessor::SPECIAL_PAGE, SMWQueryProcessor::INLINE_QUERY, |
||
503 | * SMWQueryProcessor::CONCEPT_DESC. |
||
504 | * |
||
505 | * @param string $format |
||
506 | * @param $context |
||
507 | * |
||
508 | * @return SMWResultPrinter |
||
509 | * @throws MWException if no printer is known for the given format |
||
510 | 92 | */ |
|
511 | 92 | static public function getResultPrinter( $format, $context = self::SPECIAL_PAGE ) { |
|
522 | |||
523 | /** |
||
524 | * A function to describe the allowed parameters of a query using |
||
525 | * any specific format - most query printers should override this |
||
526 | * function. |
||
527 | * |
||
528 | * @since 1.6.2, return element type changed in 1.8 |
||
529 | * |
||
530 | * @return IParamDefinition[] |
||
531 | 92 | */ |
|
532 | 92 | public static function getParameters() { |
|
620 | 92 | ||
621 | 92 | private static function getSourceParam() { |
|
629 | |||
630 | /** |
||
631 | * Returns the definitions of all parameters supported by the specified format. |
||
632 | * |
||
633 | * @since 1.8 |
||
634 | * |
||
635 | * @param string $format |
||
636 | * |
||
637 | * @return ParamDefinition[] |
||
638 | 1 | */ |
|
639 | 1 | public static function getFormatParameters( $format ) { |
|
650 | |||
651 | } |
||
652 |
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.