Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CompoundQueryProcessor 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 CompoundQueryProcessor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class CompoundQueryProcessor extends QueryProcessor { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Comparison helper function, used in sorting results. |
||
| 23 | */ |
||
| 24 | public static function compareQueryResults( $a, $b ) { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Handler for the #compound_query parser function. |
||
| 35 | * |
||
| 36 | * @param Parser $parser |
||
| 37 | * |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | public static function doCompoundQuery( Parser &$parser ) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Separates $queryParams from $otherParams. |
||
| 64 | * |
||
| 65 | * @param $params |
||
| 66 | * @return array |
||
| 67 | */ |
||
| 68 | public static function separateParams( $params ) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Query and merge results of subqueries. |
||
| 91 | * |
||
| 92 | * @param $queryParams |
||
| 93 | * @param $otherParams |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | public static function queryAndMergeResults( $queryParams, $otherParams ) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * An alternative to explode() - that function won't work here, |
||
| 130 | * because we don't want to split the string on all semicolons, just |
||
| 131 | * the ones that aren't contained within square brackets |
||
| 132 | * |
||
| 133 | * @param string $param |
||
| 134 | * |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | protected static function getSubParams( $param ) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param $rawparams |
||
| 168 | * @param $context |
||
| 169 | * @param $showmode |
||
| 170 | * |
||
| 171 | * @return SMWQueryResult |
||
| 172 | */ |
||
| 173 | protected static function getQueryResultFromFunctionParams( $rawparams, $context = QueryProcessor::INLINE_QUERY, $showmode = false ) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Combine two arrays of SMWWikiPageValue objects into one |
||
| 181 | * |
||
| 182 | * @param array $result1 |
||
| 183 | * @param array $result2 |
||
| 184 | * |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | protected static function mergeSMWQueryResults( $result1, $result2 ) { |
||
| 206 | |||
| 207 | protected static function mergeSMWPrintRequests( $printRequests1, $printRequests2 ) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param $querystring |
||
| 224 | * @param array $params |
||
| 225 | * @param $extraPrintouts |
||
| 226 | * @param $outputMode |
||
| 227 | * @param $context |
||
| 228 | * |
||
| 229 | * @return SMWQueryResult |
||
| 230 | */ |
||
| 231 | protected static function getQueryResultFromQueryString( $querystring, array $params, $extraPrintouts, $context = QueryProcessor::INLINE_QUERY ) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Matches getResultFromQueryResult() from SMWQueryProcessor, |
||
| 261 | * except that formats of type 'debug' and 'count' aren't handled. |
||
| 262 | * |
||
| 263 | * @param CompoundQueryResult $res |
||
| 264 | * @param array $params These need to be the result of a list fed to getProcessedParams as of SMW 1.6.2 |
||
| 265 | * @param $outputmode |
||
| 266 | * @param $context |
||
| 267 | * @param string $format |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | protected static function getResultFromQueryResult( CompoundQueryResult $res, array $params, $outputmode, $context = QueryProcessor::INLINE_QUERY, $format = '' ) { |
||
| 288 | |||
| 289 | } |
||
| 290 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.