Complex classes like ImageModal 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 ImageModal, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class ImageModal implements NestableInterface { |
||
41 | |||
42 | const CSS_CLASS_PREVENTING_MODAL = 'no-modal'; |
||
43 | |||
44 | /** |
||
45 | * The components listed here prevent the generation of an image modal. |
||
46 | * @var array |
||
47 | */ |
||
48 | const PARENTS_PREVENTING_MODAL = [ 'button', 'collapse ', 'image_modal', 'modal', 'popover', 'tooltip' ]; |
||
49 | |||
50 | /** |
||
51 | * @var \DummyLinker $dummyLinker |
||
52 | */ |
||
53 | private $dummyLinker; |
||
54 | |||
55 | /** |
||
56 | * @var \File $file |
||
57 | */ |
||
58 | private $file; |
||
59 | |||
60 | /** |
||
61 | * @var string $id |
||
62 | */ |
||
63 | private $id; |
||
64 | |||
65 | /** |
||
66 | * @var NestingController $nestingController |
||
67 | */ |
||
68 | private $nestingController; |
||
69 | |||
70 | /** |
||
71 | * @var NestableInterface|false $parentComponent |
||
72 | */ |
||
73 | private $parentComponent; |
||
74 | |||
75 | /** |
||
76 | * @var ParserOutputHelper $parserOutputHelper |
||
77 | */ |
||
78 | private $parserOutputHelper; |
||
79 | |||
80 | /** |
||
81 | * @var bool $disableSourceLink |
||
82 | */ |
||
83 | private $disableSourceLink; |
||
84 | |||
85 | /** |
||
86 | * @var Title $title |
||
87 | */ |
||
88 | private $title; |
||
89 | |||
90 | /** |
||
91 | * ImageModal constructor. |
||
92 | * |
||
93 | * @param \DummyLinker $dummyLinker |
||
94 | * @param \Title $title |
||
95 | * @param \File $file |
||
96 | * @param NestingController $nestingController |
||
97 | * @param ParserOutputHelper $parserOutputHelper DI for unit testing |
||
98 | * |
||
99 | * @throws \MWException cascading {@see \BootstrapComponents\ApplicationFactory} methods |
||
100 | */ |
||
101 | 23 | public function __construct( $dummyLinker, $title, $file, $nestingController = null, $parserOutputHelper = null ) { |
|
119 | |||
120 | /** |
||
121 | * @inheritdoc |
||
122 | */ |
||
123 | 23 | public function getComponentName() { |
|
126 | |||
127 | /** |
||
128 | * @inheritdoc |
||
129 | */ |
||
130 | 15 | public function getId() { |
|
133 | |||
134 | /** |
||
135 | * @param array $frameParams Associative array of parameters external to the media handler. |
||
136 | * Boolean parameters are indicated by presence or absence, the value is arbitrary and |
||
137 | * will often be false. |
||
138 | * thumbnail If present, downscale and frame |
||
139 | * manualthumb Image name to use as a thumbnail, instead of automatic scaling |
||
140 | * framed Shows image in original size in a frame |
||
141 | * frameless Downscale but don't frame |
||
142 | * upright If present, tweak default sizes for portrait orientation |
||
143 | * upright_factor Fudge factor for "upright" tweak (default 0.75) |
||
144 | * border If present, show a border around the image |
||
145 | * align Horizontal alignment (left, right, center, none) |
||
146 | * valign Vertical alignment (baseline, sub, super, top, text-top, middle, |
||
147 | * bottom, text-bottom) |
||
148 | * alt Alternate text for image (i.e. alt attribute). Plain text. |
||
149 | * class HTML for image classes. Plain text. |
||
150 | * caption HTML for image caption. |
||
151 | * link-url URL to link to |
||
152 | * link-title Title object to link to |
||
153 | * link-target Value for the target attribute, only with link-url |
||
154 | * no-link Boolean, suppress description link |
||
155 | * @param array $handlerParams Associative array of media handler parameters, to be passed |
||
156 | * to transform(). Typical keys are "width" and "page". |
||
157 | * @param string|bool $time Timestamp of the file, set as false for current |
||
158 | * @param string $res Final HTML output, used if this returns false |
||
159 | * |
||
160 | * @throws \MWException cascading {@see \BootstrapComponents\NestingController::open} |
||
161 | * @throws \ConfigException cascading {@see \BootstrapComponents\ImageModal::generateTrigger} |
||
162 | * |
||
163 | * @return bool |
||
164 | */ |
||
165 | 22 | public function parse( &$frameParams, &$handlerParams, &$time, &$res ) { |
|
190 | |||
191 | /** |
||
192 | * After this, all bool params ( 'thumbnail', 'framed', 'frameless', 'border' ) are true, if they were present before, false otherwise and all |
||
193 | * string params are set (to the original value or the empty string). |
||
194 | * |
||
195 | * This method is public, because it is used in {@see \BootstrapComponents\Tests\ImageModalTest::doTestCompareTriggerWithOriginalThumb} |
||
196 | * |
||
197 | * @param array $frameParams |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | 15 | public function sanitizeFrameParams( $frameParams ) { |
|
212 | |||
213 | /** |
||
214 | * Disables the source link in modal content. |
||
215 | */ |
||
216 | public function disableSourceLink() { |
||
219 | |||
220 | /** |
||
221 | * Runs various tests, to see, if we delegate processing back to {@see \Linker::makeImageLink} |
||
222 | * After this, we can assume: |
||
223 | * * file is a {@see \File} and exists |
||
224 | * * there is no link param set (link-url, link-title, link-target, no-link) |
||
225 | * * file allows inline display (ref {@see \File::allowInlineDisplay}) |
||
226 | * * we are not inside an image modal or an otherwise compromising component (thanks to {@see ImageModal::getNestingController}) |
||
227 | * * no magic word suppressing image modals is on the page |
||
228 | * * image does not have the "no-modal" class {@see ImageModal::CSS_CLASS_PREVENTING_MODAL} |
||
229 | * |
||
230 | * @param \File $file |
||
231 | * @param array $frameParams |
||
232 | * |
||
233 | * @return bool true, if all assertions hold, false if one fails (see above) |
||
234 | */ |
||
235 | 22 | protected function assertResponsibility( $file, $frameParams ) { |
|
241 | |||
242 | /** |
||
243 | * @param \File $file |
||
244 | * @param array $sanitizedFrameParams |
||
245 | * @param array $handlerParams |
||
246 | * |
||
247 | * @return array bool|string bool (large image yes or no) |
||
248 | */ |
||
249 | 13 | protected function generateContent( $file, $sanitizedFrameParams, $handlerParams ) { |
|
263 | |||
264 | /** |
||
265 | * @return \DummyLinker |
||
266 | */ |
||
267 | /** @scrutinizer ignore-unused */ |
||
268 | protected function getDummyLinker() { |
||
271 | |||
272 | /** |
||
273 | * @return \File |
||
274 | */ |
||
275 | 22 | protected function getFile() { |
|
278 | |||
279 | /** |
||
280 | * @return NestingController |
||
281 | */ |
||
282 | 23 | protected function getNestingController() { |
|
285 | |||
286 | /** |
||
287 | * @return null|NestableInterface |
||
288 | */ |
||
289 | 20 | protected function getParentComponent() { |
|
292 | |||
293 | /** |
||
294 | * @return ParserOutputHelper |
||
295 | */ |
||
296 | 15 | protected function getParserOutputHelper() { |
|
299 | |||
300 | /** |
||
301 | * @return Title |
||
302 | */ |
||
303 | 12 | protected function getTitle() { |
|
306 | |||
307 | /** |
||
308 | * @param $sanitizedFrameParams |
||
309 | * @param $handlerParams |
||
310 | * |
||
311 | * @throws \ConfigException |
||
312 | * |
||
313 | * @return string rendered modal on success, empty string on failure. |
||
314 | */ |
||
315 | 15 | protected function turnParamsIntoModal( $sanitizedFrameParams, $handlerParams ) { |
|
364 | |||
365 | /** |
||
366 | * @param \File $file |
||
367 | * @param array $frameParams |
||
368 | * |
||
369 | * @return bool |
||
370 | */ |
||
371 | 22 | private function assertImageTagValid( $file, $frameParams ) { |
|
386 | |||
387 | /** |
||
388 | * @param array $frameParams |
||
389 | * |
||
390 | * @return bool |
||
391 | */ |
||
392 | 20 | private function assertImageModalNotSuppressed( $frameParams ) { |
|
404 | |||
405 | /** |
||
406 | * Performs all the mandatory actions on the parser output for the component class |
||
407 | * |
||
408 | * @throws \MWException cascading {@see \BootstrapComponents\ApplicationFactory::getComponentLibrary} |
||
409 | */ |
||
410 | 15 | private function augmentParserOutput() { |
|
417 | |||
418 | /** |
||
419 | * @param \MediaTransformOutput $img |
||
420 | * @param array $sanitizedFrameParams |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | 12 | private function buildContentImageString( $img, $sanitizedFrameParams ) { |
|
440 | |||
441 | /** |
||
442 | * @param Title $title |
||
443 | * @param array $handlerParams |
||
444 | * |
||
445 | * @return string |
||
446 | */ |
||
447 | 12 | private function generateButtonToSource( $title, $handlerParams ) { |
|
462 | |||
463 | /** |
||
464 | * We don't want a modal inside a modal. Unfortunately, the caption (and title) are parsed, before the modal is generated. So instead of |
||
465 | * building the modal from the outside, it is build from the inside. This method tries to detect this construct and removes any modal from |
||
466 | * the supplied text and replaces it with the image tag found inside the modal caption content. |
||
467 | * |
||
468 | * @param string $text |
||
469 | * |
||
470 | * @return string |
||
471 | */ |
||
472 | 15 | private function preventModalInception( $text ) { |
|
480 | |||
481 | /** |
||
482 | * @param string $caption |
||
483 | * |
||
484 | * @return string |
||
485 | */ |
||
486 | 3 | private function sanitizeCaption( $caption ) { |
|
489 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.