Complex classes like ImageModalTrigger 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 ImageModalTrigger, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class ImageModalTrigger { |
||
41 | /** |
||
42 | * @var \File $file |
||
43 | */ |
||
44 | private $file; |
||
45 | |||
46 | /** |
||
47 | * @var string $id |
||
48 | */ |
||
49 | private $id; |
||
50 | |||
51 | /** |
||
52 | * ImageModal constructor. |
||
53 | * |
||
54 | * @param string $id |
||
55 | * @param \File $file |
||
56 | */ |
||
57 | 14 | public function __construct( $id, $file ) { |
|
61 | |||
62 | /** |
||
63 | * @param array $sanitizedFrameParams |
||
64 | * @param array $handlerParams |
||
65 | * |
||
66 | * @throws \ConfigException cascading {@see \BootstrapComponents\ImageModalTrigger::generateTriggerCreateThumb} |
||
67 | * @throws \Exception cascading {@see \BootstrapComponents\ImageModalTrigger::wrapAndFinalize} |
||
68 | * |
||
69 | * @return false|string |
||
70 | */ |
||
71 | 13 | public function generate( $sanitizedFrameParams, $handlerParams ) { |
|
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | 12 | public function getId() { |
|
105 | |||
106 | /** |
||
107 | * @param \File $file |
||
108 | * @param \MediaTransformOutput $thumb |
||
109 | * @param array $sanitizedFrameParams |
||
110 | * @param array $thumbHandlerParams |
||
111 | * |
||
112 | * @return array |
||
113 | */ |
||
114 | 12 | protected function calculateHtmlOptions( $file, $thumb, $sanitizedFrameParams, $thumbHandlerParams ) { |
|
136 | |||
137 | /** |
||
138 | * @param \File $file |
||
139 | * @param array $sanitizedFrameParams |
||
140 | * @param array $handlerParams |
||
141 | * |
||
142 | * @throws \ConfigException cascading {@see \BootstrapComponents\ImageModal::generateTriggerReevaluateImageDimensions} |
||
143 | * |
||
144 | * @return array [ \MediaTransformOutput|false, handlerParams ] |
||
145 | */ |
||
146 | 13 | protected function createThumb( $file, $sanitizedFrameParams, $handlerParams ) { |
|
167 | |||
168 | /** |
||
169 | * @return \File |
||
170 | */ |
||
171 | 13 | protected function getFile() { |
|
174 | |||
175 | /** |
||
176 | * This is mostly taken from {@see \Linker::makeImageLink}, rest originates from {@see \Linker::makeThumbLink2}. Extracts are heavily |
||
177 | * squashed and condensed |
||
178 | * |
||
179 | * @param \File $file |
||
180 | * @param array $sanitizedFrameParams |
||
181 | * @param array $handlerParams |
||
182 | * |
||
183 | * @throws \ConfigException cascading {@see \BootstrapComponents\ImageModal::generateTriggerCalculateImageWidth} |
||
184 | * |
||
185 | * @return array thumbnail handler params |
||
186 | */ |
||
187 | 13 | protected function reevaluateImageDimensions( $file, $sanitizedFrameParams, $handlerParams ) { |
|
201 | |||
202 | /** |
||
203 | * Envelops the publication trigger img-tag. |
||
204 | * |
||
205 | * @param string $publicationString |
||
206 | * @param array $sanitizedFrameParams |
||
207 | * @param int $publicationWidth |
||
208 | * |
||
209 | * @throws \Exception cascading {@see ImageModalTrigger::buildThumbnailTrigger} |
||
210 | * |
||
211 | * @return string |
||
212 | */ |
||
213 | 12 | protected function wrapAndFinalize( $publicationString, $sanitizedFrameParams, $publicationWidth ) { |
|
229 | |||
230 | /** |
||
231 | * @param $sanitizedFrameParams |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | 13 | private function amIThumbnailRelated( $sanitizedFrameParams ) { |
|
241 | |||
242 | /** |
||
243 | * Envelops a publication trigger img-tag that is a thumbnail. |
||
244 | * |
||
245 | * @param string $publicationString |
||
246 | * @param array $sanitizedFrameParams |
||
247 | * @param int $publicationWidth |
||
248 | * |
||
249 | * @throws \Exception cascading {@see \RequestContext::getMain} |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | 5 | private function buildThumbnailTrigger( $publicationString, $sanitizedFrameParams, $publicationWidth ) { |
|
283 | |||
284 | /** |
||
285 | * @param array $sanitizedFrameParams |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | 5 | private function buildZoomIcon( $sanitizedFrameParams ) { |
|
308 | |||
309 | /** |
||
310 | * Calculates a with from File, $sanitizedFrameParams, and $handlerParams |
||
311 | * |
||
312 | * @param \File $file |
||
313 | * @param array $sanitizedFrameParams |
||
314 | * @param array $handlerParams |
||
315 | * |
||
316 | * @throws \ConfigException cascading {@see ImageModal::getInitialWidthSuggestion} or {@see ImageModal::getPreferredWidth} |
||
317 | * |
||
318 | * @return array thumbnail handler params |
||
319 | */ |
||
320 | 9 | private function calculateImageWidth( $file, $sanitizedFrameParams, $handlerParams ) { |
|
344 | |||
345 | /** |
||
346 | * @param string $fileTitle |
||
347 | * |
||
348 | * @return bool|\File |
||
349 | */ |
||
350 | 3 | private function getFileFromTitle( $fileTitle ) { |
|
357 | |||
358 | /** |
||
359 | * @param \Config $globalConfig |
||
360 | * @param \File $file |
||
361 | * @param array $handlerParams |
||
362 | * |
||
363 | * @throws \ConfigException cascading {@see \Config::get} |
||
364 | * |
||
365 | * @return mixed |
||
366 | */ |
||
367 | 9 | private function getInitialWidthSuggestion( $globalConfig, $file, $handlerParams ) { |
|
376 | |||
377 | /** |
||
378 | * @param \Config $globalConfig |
||
379 | * @param array $sanitizedFrameParams |
||
380 | * |
||
381 | * @throws \ConfigException cascading {@see \Config::get} |
||
382 | * |
||
383 | * @return float |
||
384 | */ |
||
385 | 5 | private function getPreferredWidth( $globalConfig, $sanitizedFrameParams ) { |
|
396 | |||
397 | /** |
||
398 | * @param array $thumbLimits |
||
399 | * |
||
400 | * @return int|string |
||
401 | */ |
||
402 | 5 | private function getWidthOptionForThumbLimits( $thumbLimits ) { |
|
418 | |||
419 | /** |
||
420 | * Do not present an image bigger than the source, for bitmap-style images |
||
421 | * |
||
422 | * @param \File $file |
||
423 | * @param array $sanitizedFrameParams |
||
424 | * @param array $handlerParams |
||
425 | * |
||
426 | * @return array |
||
427 | */ |
||
428 | 7 | private function limitSizeToSourceOnBitmapImages( $file, $sanitizedFrameParams, $handlerParams ) { |
|
439 | } |
||
440 |