Complex classes like Gallery 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 Gallery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Gallery extends ResultPrinter { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @see SMWResultPrinter::getName |
||
| 25 | * |
||
| 26 | * @return string |
||
| 27 | */ |
||
| 28 | public function getName() { |
||
| 29 | return $this->msg( 'srf_printername_gallery' )->text(); |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @see SMWResultPrinter::buildResult |
||
| 34 | * |
||
| 35 | * @since 1.8 |
||
| 36 | * |
||
| 37 | * @param SMWQueryResult $results |
||
| 38 | * |
||
| 39 | * @return string |
||
| 40 | */ |
||
| 41 | 1 | protected function buildResult( SMWQueryResult $results ) { |
|
| 42 | |||
| 43 | // Intro/outro are not planned to work with the widget option |
||
| 44 | 1 | if ( ( $this->params['intro'] !== '' || $this->params['outro'] !== '' ) && $this->params['widget'] !== '' ) { |
|
| 45 | return $results->addErrors( |
||
| 46 | [ |
||
| 47 | $this->msg( 'srf-error-option-mix', 'widget' )->inContentLanguage()->text() |
||
| 48 | ] |
||
| 49 | ); |
||
| 50 | }; |
||
| 51 | |||
| 52 | 1 | return $this->getResultText( $results, $this->outputMode ); |
|
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @see SMWResultPrinter::getResultText |
||
| 57 | * |
||
| 58 | * @param $results SMWQueryResult |
||
| 59 | * @param $outputmode integer |
||
| 60 | * |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | 1 | public function getResultText( SMWQueryResult $results, $outputmode ) { |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Handles queries where the images (and optionally their captions) are specified as properties. |
||
| 181 | * |
||
| 182 | * @since 1.5.3 |
||
| 183 | * |
||
| 184 | * @param SMWQueryResult $results |
||
| 185 | * @param ImageGallery $ig |
||
| 186 | * @param string $imageProperty |
||
| 187 | * @param string $captionProperty |
||
| 188 | * @param string $redirectProperty |
||
| 189 | * @param $outputMode |
||
| 190 | */ |
||
| 191 | protected function addImageProperties( SMWQueryResult $results, &$ig, $imageProperty, $captionProperty, $redirectProperty, $outputMode ) { |
||
| 192 | while ( /* array of SMWResultArray */ |
||
| 193 | $rows = $results->getNext() ) { // Objects (pages) |
||
| 194 | $images = []; |
||
| 195 | $captions = []; |
||
| 196 | $redirects = []; |
||
| 197 | |||
| 198 | for ( $i = 0, $n = count( $rows ); $i < $n; $i++ ) { // Properties |
||
| 199 | /** |
||
| 200 | * @var SMWResultArray $resultArray |
||
| 201 | * @var SMWDataValue $dataValue |
||
| 202 | */ |
||
| 203 | $resultArray = $rows[$i]; |
||
| 204 | |||
| 205 | $label = $resultArray->getPrintRequest()->getMode() == SMWPrintRequest::PRINT_THIS |
||
| 206 | ? '-' : $resultArray->getPrintRequest()->getLabel(); |
||
| 207 | |||
| 208 | // Make sure always use real label here otherwise it results in an empty array |
||
| 209 | if ( $resultArray->getPrintRequest()->getLabel() == $imageProperty ) { |
||
| 210 | while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) { // Property values |
||
| 211 | if ( $dataValue->getTypeID() == '_wpg' ) { |
||
| 212 | $images[] = $dataValue->getTitle(); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | } elseif ( $label == $captionProperty ) { |
||
| 216 | while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) { // Property values |
||
| 217 | $captions[] = $dataValue->getShortText( $outputMode, $this->getLinker( true ) ); |
||
| 218 | } |
||
| 219 | } elseif ( $label == $redirectProperty ) { |
||
| 220 | while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) { // Property values |
||
| 221 | if ( $dataValue->getDataItem()->getDIType() == SMWDataItem::TYPE_WIKIPAGE ) { |
||
| 222 | $redirects[] = $dataValue->getTitle(); |
||
| 223 | } elseif ( $dataValue->getDataItem()->getDIType() == SMWDataItem::TYPE_URI ) { |
||
| 224 | $redirects[] = $dataValue->getURL(); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | // Check available matches against captions |
||
| 231 | $amountMatches = count( $captions ) == count( $images ); |
||
| 232 | $hasCaption = $amountMatches || count( $captions ) > 0; |
||
| 233 | |||
| 234 | // Check available matches against redirects |
||
| 235 | $amountRedirects = count( $redirects ) == count( $images ); |
||
| 236 | $hasRedirect = $amountRedirects || count( $redirects ) > 0; |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @var Title $imgTitle |
||
| 240 | */ |
||
| 241 | foreach ( $images as $imgTitle ) { |
||
| 242 | if ( $imgTitle->exists() ) { |
||
| 243 | $imgCaption = $hasCaption ? ( $amountMatches ? array_shift( $captions ) : $captions[0] ) : ''; |
||
| 244 | $imgRedirect = $hasRedirect ? ( $amountRedirects ? array_shift( $redirects ) : $redirects[0] ) : ''; |
||
| 245 | $this->addImageToGallery( $ig, $imgTitle, $imgCaption, $imgRedirect ); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Handles queries where the result objects are image pages. |
||
| 253 | * |
||
| 254 | * @since 1.5.3 |
||
| 255 | * |
||
| 256 | * @param SMWQueryResult $results |
||
| 257 | * @param ImageGallery $ig |
||
| 258 | */ |
||
| 259 | 1 | protected function addImagePages( SMWQueryResult $results, &$ig ) { |
|
| 260 | 1 | while ( $row = $results->getNext() ) { |
|
| 261 | /** |
||
| 262 | * @var SMWResultArray $firstField |
||
| 263 | */ |
||
| 264 | 1 | $firstField = $row[0]; |
|
| 265 | 1 | $nextObject = $firstField->getNextDataValue(); |
|
| 266 | |||
| 267 | 1 | if ( $nextObject !== false ) { |
|
| 268 | 1 | $imgTitle = $nextObject->getTitle(); |
|
| 269 | |||
| 270 | // Ensure the title belongs to the image namespace |
||
| 271 | 1 | if ( $imgTitle instanceof Title && $imgTitle->getNamespace() === NS_FILE ) { |
|
| 272 | 1 | $imgCaption = ''; |
|
| 273 | |||
| 274 | // Is there a property queried for display with ?property |
||
| 275 | 1 | if ( isset( $row[1] ) ) { |
|
| 276 | 1 | $imgCaption = $row[1]->getNextDataValue(); |
|
| 277 | 1 | if ( is_object( $imgCaption ) ) { |
|
| 278 | 1 | $imgCaption = $imgCaption->getShortText( $this->outputMode, $this->getLinker( true ) ); |
|
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | 1 | $this->addImageToGallery( $ig, $imgTitle, $imgCaption ); |
|
| 283 | } |
||
| 284 | } |
||
| 285 | } |
||
| 286 | 1 | } |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Adds a single image to the gallery. |
||
| 290 | * Takes care of automatically adding a caption when none is provided and parsing it's wikitext. |
||
| 291 | * |
||
| 292 | * @since 1.5.3 |
||
| 293 | * |
||
| 294 | * @param ImageGallery $ig The gallery to add the image to |
||
| 295 | * @param Title $imgTitle The title object of the page of the image |
||
| 296 | * @param string $imgCaption An optional caption for the image |
||
| 297 | * @param string $imgRedirect |
||
| 298 | */ |
||
| 299 | 1 | protected function addImageToGallery( &$ig, Title $imgTitle, $imgCaption, $imgRedirect = '' ) { |
|
| 300 | |||
| 301 | 1 | if ( empty( $imgCaption ) ) { |
|
| 302 | if ( $this->params['autocaptions'] ) { |
||
| 303 | $imgCaption = $imgTitle->getBaseText(); |
||
| 304 | |||
| 305 | if ( !$this->params['fileextensions'] ) { |
||
| 306 | $imgCaption = preg_replace( '#\.[^.]+$#', '', $imgCaption ); |
||
| 307 | } |
||
| 308 | } else { |
||
| 309 | $imgCaption = ''; |
||
| 310 | } |
||
| 311 | } else { |
||
| 312 | 1 | if ( $imgTitle instanceof Title && $imgTitle->getNamespace() == NS_FILE && !$this->isSpecialPage() ) { |
|
| 313 | 1 | $imgCaption = $ig->mParser->recursiveTagParse( $imgCaption ); |
|
| 314 | } |
||
| 315 | } |
||
| 316 | // Use image alt as helper for either text |
||
| 317 | 1 | $imgAlt = $this->params['redirects'] === '' ? $imgCaption : $imgRedirect !== '' ? $imgRedirect : ''; |
|
| 318 | 1 | $ig->add( $imgTitle, $imgCaption, $imgAlt ); |
|
| 319 | 1 | } |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Returns the overlay setting |
||
| 323 | * |
||
| 324 | * @since 1.8 |
||
| 325 | * |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | 1 | private function getImageOverlay() { |
|
| 329 | 1 | if ( array_key_exists( 'overlay', $this->params ) && $this->params['overlay'] == true ) { |
|
| 330 | SMWOutputs::requireResource( 'ext.srf.gallery.overlay' ); |
||
| 331 | return ' srf-overlay'; |
||
| 332 | } else { |
||
| 333 | 1 | return ''; |
|
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Init carousel widget |
||
| 339 | * |
||
| 340 | * @since 1.8 |
||
| 341 | * |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | private function getCarouselWidget() { |
||
| 345 | |||
| 346 | // Set attributes for jcarousel |
||
| 347 | $dataAttribs = [ |
||
| 348 | 'wrap' => 'both', // Whether to wrap at the first/last item (or both) and jump back to the start/end. |
||
| 349 | 'vertical' => 'false', // Orientation: vertical = false means horizontal |
||
| 350 | 'rtl' => 'false', // Directionality: rtl = false means ltr |
||
| 351 | ]; |
||
| 352 | |||
| 353 | // Use the perrow parameter to determine the scroll sequence. |
||
| 354 | if ( empty( $this->params['perrow'] ) ) { |
||
| 355 | $dataAttribs['scroll'] = 1; // default 1 |
||
| 356 | } else { |
||
| 357 | $dataAttribs['scroll'] = $this->params['perrow']; |
||
| 358 | $dataAttribs['visible'] = $this->params['perrow']; |
||
| 359 | } |
||
| 360 | |||
| 361 | $attribs = [ |
||
| 362 | 'id' => uniqid(), |
||
| 363 | 'class' => 'jcarousel jcarousel-skin-smw' . $this->getImageOverlay(), |
||
| 364 | 'style' => 'display:none;', |
||
| 365 | ]; |
||
| 366 | |||
| 367 | foreach ( $dataAttribs as $name => $value ) { |
||
| 368 | $attribs['data-' . $name] = $value; |
||
| 369 | } |
||
| 370 | |||
| 371 | SMWOutputs::requireResource( 'ext.srf.gallery.carousel' ); |
||
| 372 | |||
| 373 | return $attribs; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Init slideshow widget |
||
| 378 | * |
||
| 379 | * @since 1.8 |
||
| 380 | * |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | private function getSlideshowWidget() { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @see SMWResultPrinter::getParamDefinitions |
||
| 399 | * |
||
| 400 | * @since 1.8 |
||
| 401 | * |
||
| 402 | * @param $definitions array of IParamDefinition |
||
| 403 | * |
||
| 404 | * @return array of IParamDefinition|array |
||
| 405 | */ |
||
| 406 | 1 | public function getParamDefinitions( array $definitions ) { |
|
| 485 | |||
| 486 | 1 | private function isSpecialPage() { |
|
| 490 | |||
| 491 | 1 | private function getFileNsTextForPageLanguage() { |
|
| 495 | |||
| 496 | } |
||
| 497 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: