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 |
||
22 | class Gallery extends ResultPrinter { |
||
23 | |||
24 | /** |
||
25 | * @see SMWResultPrinter::getName |
||
26 | * |
||
27 | * @return string |
||
28 | */ |
||
29 | public function getName() { |
||
32 | |||
33 | /** |
||
34 | * @see SMWResultPrinter::buildResult |
||
35 | * |
||
36 | * @since 1.8 |
||
37 | * |
||
38 | * @param SMWQueryResult $results |
||
39 | * |
||
40 | * @return string |
||
41 | */ |
||
42 | 1 | protected function buildResult( SMWQueryResult $results ) { |
|
57 | |||
58 | /** |
||
59 | * @see SMWResultPrinter::getResultText |
||
60 | * |
||
61 | * @param $results SMWQueryResult |
||
62 | * @param $outputmode integer |
||
63 | * |
||
64 | * @return string | array |
||
65 | */ |
||
66 | 1 | public function getResultText( SMWQueryResult $results, $outputmode ) { |
|
67 | |||
68 | 1 | $ig = new TraditionalImageGallery(); |
|
69 | |||
70 | 1 | $ig->setShowBytes( false ); |
|
71 | 1 | $ig->setShowFilename( false ); |
|
72 | |||
73 | 1 | if ( method_exists( $ig, 'setShowDimensions' ) ) { |
|
74 | 1 | $ig->setShowDimensions( false ); |
|
75 | } |
||
76 | |||
77 | 1 | $ig->setCaption( $this->mIntro ); // set caption to IQ header |
|
78 | |||
79 | // No need for a special page to use the parser but for the "normal" page |
||
80 | // view we have to ensure caption text is parsed correctly through the parser |
||
81 | 1 | if ( !$this->isSpecialPage() ) { |
|
82 | 1 | $ig->setParser( $GLOBALS['wgParser'] ); |
|
83 | } |
||
84 | |||
85 | 1 | $html = ''; |
|
86 | 1 | $processing = ''; |
|
87 | |||
88 | 1 | if ( $this->params['widget'] == 'carousel' ) { |
|
89 | // Carousel widget |
||
90 | $ig->setAttributes( $this->getCarouselWidget() ); |
||
91 | 1 | } elseif ( $this->params['widget'] == 'slideshow' ) { |
|
92 | // Slideshow widget |
||
93 | $ig->setAttributes( $this->getSlideshowWidget() ); |
||
94 | } else { |
||
95 | |||
96 | // Standard gallery attributes |
||
97 | $attribs = [ |
||
98 | 1 | 'id' => uniqid(), |
|
99 | 1 | 'class' => $this->getImageOverlay(), |
|
100 | ]; |
||
101 | |||
102 | 1 | $ig->setAttributes( $attribs ); |
|
103 | } |
||
104 | |||
105 | // Only use redirects where the overlay option is not used and redirect |
||
106 | // thumb images towards a different target |
||
107 | 1 | if ( $this->params['redirects'] !== '' && !$this->params['overlay'] ) { |
|
108 | SMWOutputs::requireResource( 'ext.srf.gallery.redirect' ); |
||
109 | } |
||
110 | |||
111 | // For the carousel widget, the perrow option should not be set |
||
112 | 1 | if ( $this->params['perrow'] !== '' && $this->params['widget'] !== 'carousel' ) { |
|
113 | $ig->setPerRow( $this->params['perrow'] ); |
||
114 | } |
||
115 | |||
116 | 1 | if ( $this->params['widths'] !== '' ) { |
|
117 | $ig->setWidths( $this->params['widths'] ); |
||
118 | } |
||
119 | |||
120 | 1 | if ( $this->params['heights'] !== '' ) { |
|
121 | $ig->setHeights( $this->params['heights'] ); |
||
122 | } |
||
123 | |||
124 | 1 | $printReqLabels = []; |
|
125 | 1 | $redirectType = ''; |
|
126 | |||
127 | /** |
||
128 | * @var SMWPrintRequest $printReq |
||
129 | */ |
||
130 | 1 | foreach ( $results->getPrintRequests() as $printReq ) { |
|
131 | 1 | $printReqLabels[] = $printReq->getLabel(); |
|
132 | |||
133 | // Get redirect type |
||
134 | 1 | if ( $this->params['redirects'] === $printReq->getLabel() ) { |
|
135 | 1 | $redirectType = $printReq->getTypeID(); |
|
136 | } |
||
137 | } |
||
138 | |||
139 | 1 | if ( $this->params['imageproperty'] !== '' && in_array( $this->params['imageproperty'], $printReqLabels ) || |
|
140 | 1 | $this->params['redirects'] !== '' && in_array( $this->params['redirects'], $printReqLabels ) ) { |
|
141 | |||
142 | $this->addImageProperties( |
||
143 | $results, |
||
144 | $ig, |
||
145 | $this->params['imageproperty'], |
||
146 | $this->params['captionproperty'], |
||
147 | $this->params['redirects'], |
||
148 | $outputmode |
||
149 | ); |
||
150 | } else { |
||
151 | 1 | $this->addImagePages( $results, $ig ); |
|
152 | } |
||
153 | |||
154 | // SRF Global settings |
||
155 | 1 | SRFUtils::addGlobalJSVariables(); |
|
156 | |||
157 | // Display a processing image as long as the DOM is no ready |
||
158 | 1 | if ( $this->params['widget'] !== '' ) { |
|
159 | $processing = SRFUtils::htmlProcessingElement(); |
||
160 | } |
||
161 | |||
162 | // Beautify the class selector |
||
163 | 1 | $class = $this->params['widget'] ? '-' . $this->params['widget'] . ' ' : ''; |
|
164 | 1 | $class = $this->params['redirects'] !== '' && $this->params['overlay'] === false ? $class . ' srf-redirect' . ' ' : $class; |
|
165 | 1 | $class = $this->params['class'] ? $class . ' ' . $this->params['class'] : $class; |
|
166 | |||
167 | // Separate content from result output |
||
168 | 1 | if ( !$ig->isEmpty() ) { |
|
169 | $attribs = [ |
||
170 | 1 | 'class' => 'srf-gallery' . $class, |
|
171 | 1 | 'data-redirect-type' => $redirectType, |
|
172 | 1 | 'data-ns-text' => $this->getFileNsTextForPageLanguage() |
|
173 | ]; |
||
174 | |||
175 | 1 | $html = Html::rawElement( 'div', $attribs, $processing . $ig->toHTML() ); |
|
176 | } |
||
177 | |||
178 | // If available, create a link that points to further results |
||
179 | 1 | if ( $this->linkFurtherResults( $results ) ) { |
|
180 | $html .= $this->getLink( $results, SMW_OUTPUT_HTML )->getText( SMW_OUTPUT_HTML, $this->mLinker ); |
||
181 | } |
||
182 | |||
183 | 1 | return [ $html, 'nowiki' => true, 'isHTML' => true ]; |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * Handles queries where the images (and optionally their captions) are specified as properties. |
||
188 | * |
||
189 | * @since 1.5.3 |
||
190 | * |
||
191 | * @param SMWQueryResult $results |
||
192 | * @param TraditionalImageGallery $ig |
||
193 | * @param string $imageProperty |
||
194 | * @param string $captionProperty |
||
195 | * @param string $redirectProperty |
||
196 | * @param $outputMode |
||
197 | */ |
||
198 | protected function addImageProperties( SMWQueryResult $results, &$ig, $imageProperty, $captionProperty, $redirectProperty, $outputMode ) { |
||
257 | |||
258 | /** |
||
259 | * Handles queries where the result objects are image pages. |
||
260 | * |
||
261 | * @since 1.5.3 |
||
262 | * |
||
263 | * @param SMWQueryResult $results |
||
264 | * @param TraditionalImageGallery $ig |
||
265 | */ |
||
266 | 1 | protected function addImagePages( SMWQueryResult $results, &$ig ) { |
|
297 | |||
298 | /** |
||
299 | * Adds a single image to the gallery. |
||
300 | * Takes care of automatically adding a caption when none is provided and parsing it's wikitext. |
||
301 | * |
||
302 | * @since 1.5.3 |
||
303 | * |
||
304 | * @param TraditionalImageGallery $ig The gallery to add the image to |
||
305 | * @param Title $imgTitle The title object of the page of the image |
||
306 | * @param string $imgCaption An optional caption for the image |
||
307 | * @param string $imgRedirect |
||
308 | */ |
||
309 | 1 | protected function addImageToGallery( &$ig, Title $imgTitle, $imgCaption, $imgRedirect = '' ) { |
|
330 | |||
331 | /** |
||
332 | * Returns the overlay setting |
||
333 | * |
||
334 | * @since 1.8 |
||
335 | * |
||
336 | * @return string |
||
337 | */ |
||
338 | 1 | private function getImageOverlay() { |
|
346 | |||
347 | /** |
||
348 | * Init carousel widget |
||
349 | * |
||
350 | * @since 1.8 |
||
351 | * |
||
352 | * @return string[] |
||
353 | */ |
||
354 | private function getCarouselWidget() { |
||
385 | |||
386 | /** |
||
387 | * Init slideshow widget |
||
388 | * |
||
389 | * @since 1.8 |
||
390 | * |
||
391 | * @return string[] |
||
392 | */ |
||
393 | private function getSlideshowWidget() { |
||
406 | |||
407 | /** |
||
408 | * @see SMWResultPrinter::getParamDefinitions |
||
409 | * |
||
410 | * @since 1.8 |
||
411 | * |
||
412 | * @param $definitions array of IParamDefinition |
||
413 | * |
||
414 | * @return array of IParamDefinition|array |
||
415 | */ |
||
416 | 1 | public function getParamDefinitions( array $definitions ) { |
|
495 | |||
496 | /** |
||
497 | * @return bool |
||
498 | */ |
||
499 | 1 | private function isSpecialPage() { |
|
503 | |||
504 | /** |
||
505 | * @return bool|null|string |
||
506 | */ |
||
507 | 1 | private function getFileNsTextForPageLanguage() { |
|
511 | |||
512 | } |
||
513 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: