Complex classes like ChangesListSpecialPage 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 ChangesListSpecialPage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | abstract class ChangesListSpecialPage extends SpecialPage { |
||
| 31 | /** @var string */ |
||
| 32 | protected $rcSubpage; |
||
| 33 | |||
| 34 | /** @var FormOptions */ |
||
| 35 | protected $rcOptions; |
||
| 36 | |||
| 37 | /** @var array */ |
||
| 38 | protected $customFilters; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Main execution point |
||
| 42 | * |
||
| 43 | * @param string $subpage |
||
| 44 | */ |
||
| 45 | public function execute( $subpage ) { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get the database result for this special page instance. Used by ApiFeedRecentChanges. |
||
| 84 | * |
||
| 85 | * @return bool|ResultWrapper Result or false |
||
| 86 | */ |
||
| 87 | public function getRows() { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get the current FormOptions for this request |
||
| 96 | * |
||
| 97 | * @return FormOptions |
||
| 98 | */ |
||
| 99 | public function getOptions() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Create a FormOptions object with options as specified by the user |
||
| 109 | * |
||
| 110 | * @param array $parameters |
||
| 111 | * |
||
| 112 | * @return FormOptions |
||
| 113 | */ |
||
| 114 | public function setup( $parameters ) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get a FormOptions object containing the default options. By default returns some basic options, |
||
| 134 | * you might want to not call parent method and discard them, or to override default values. |
||
| 135 | * |
||
| 136 | * @return FormOptions |
||
| 137 | */ |
||
| 138 | public function getDefaultOptions() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get custom show/hide filters |
||
| 162 | * |
||
| 163 | * @return array Map of filter URL param names to properties (msg/default) |
||
| 164 | */ |
||
| 165 | protected function getCustomFilters() { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Fetch values for a FormOptions object from the WebRequest associated with this instance. |
||
| 176 | * |
||
| 177 | * Intended for subclassing, e.g. to add a backwards-compatibility layer. |
||
| 178 | * |
||
| 179 | * @param FormOptions $opts |
||
| 180 | * @return FormOptions |
||
| 181 | */ |
||
| 182 | protected function fetchOptionsFromRequest( $opts ) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Process $par and put options found in $opts. Used when including the page. |
||
| 190 | * |
||
| 191 | * @param string $par |
||
| 192 | * @param FormOptions $opts |
||
| 193 | */ |
||
| 194 | public function parseParameters( $par, FormOptions $opts ) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Validate a FormOptions object generated by getDefaultOptions() with values already populated. |
||
| 200 | * |
||
| 201 | * @param FormOptions $opts |
||
| 202 | */ |
||
| 203 | public function validateOptions( FormOptions $opts ) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Return an array of conditions depending of options set in $opts |
||
| 209 | * |
||
| 210 | * @param FormOptions $opts |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | public function buildMainQueryConds( FormOptions $opts ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Process the query |
||
| 290 | * |
||
| 291 | * @param array $conds |
||
| 292 | * @param FormOptions $opts |
||
| 293 | * @return bool|ResultWrapper Result or false |
||
| 294 | */ |
||
| 295 | public function doMainQuery( $conds, $opts ) { |
||
| 327 | |||
| 328 | protected function runMainQueryHook( &$tables, &$fields, &$conds, |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Return a IDatabase object for reading |
||
| 339 | * |
||
| 340 | * @return IDatabase |
||
| 341 | */ |
||
| 342 | protected function getDB() { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Send output to the OutputPage object, only called if not used feeds |
||
| 348 | * |
||
| 349 | * @param ResultWrapper $rows Database rows |
||
| 350 | * @param FormOptions $opts |
||
| 351 | */ |
||
| 352 | public function webOutput( $rows, $opts ) { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Output feed links. |
||
| 363 | */ |
||
| 364 | public function outputFeedLinks() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Build and output the actual changes list. |
||
| 370 | * |
||
| 371 | * @param ResultWrapper $rows Database rows |
||
| 372 | * @param FormOptions $opts |
||
| 373 | */ |
||
| 374 | abstract public function outputChangesList( $rows, $opts ); |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Set the text to be displayed above the changes |
||
| 378 | * |
||
| 379 | * @param FormOptions $opts |
||
| 380 | * @param int $numRows Number of rows in the result to show after this header |
||
| 381 | */ |
||
| 382 | public function doHeader( $opts, $numRows ) { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Send the text to be displayed before the options. Should use $this->getOutput()->addWikiText() |
||
| 392 | * or similar methods to print the text. |
||
| 393 | * |
||
| 394 | * @param FormOptions $opts |
||
| 395 | */ |
||
| 396 | public function setTopText( FormOptions $opts ) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Send the text to be displayed after the options. Should use $this->getOutput()->addWikiText() |
||
| 402 | * or similar methods to print the text. |
||
| 403 | * |
||
| 404 | * @param FormOptions $opts |
||
| 405 | */ |
||
| 406 | public function setBottomText( FormOptions $opts ) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Get options to be displayed in a form |
||
| 412 | * @todo This should handle options returned by getDefaultOptions(). |
||
| 413 | * @todo Not called by anything, should be called by something… doHeader() maybe? |
||
| 414 | * |
||
| 415 | * @param FormOptions $opts |
||
| 416 | * @return array |
||
| 417 | */ |
||
| 418 | public function getExtraOptions( $opts ) { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Return the legend displayed within the fieldset |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | public function makeLegend() { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Add page-specific modules. |
||
| 474 | */ |
||
| 475 | protected function addModules() { |
||
| 476 | $out = $this->getOutput(); |
||
| 477 | // Styles and behavior for the legend box (see makeLegend()) |
||
| 478 | $out->addModuleStyles( [ |
||
| 479 | 'mediawiki.special.changeslist.legend', |
||
| 480 | 'mediawiki.special.changeslist', |
||
| 481 | ] ); |
||
| 482 | $out->addModules( 'mediawiki.special.changeslist.legend.js' ); |
||
| 483 | } |
||
| 484 | |||
| 485 | protected function getGroupName() { |
||
| 488 | } |
||
| 489 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.