Complex classes like Image 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 Image, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Image extends File implements Flushable { |
||
10 | |||
11 | const ORIENTATION_SQUARE = 0; |
||
12 | const ORIENTATION_PORTRAIT = 1; |
||
13 | const ORIENTATION_LANDSCAPE = 2; |
||
14 | |||
15 | private static $backend = "GDBackend"; |
||
16 | |||
17 | private static $casting = array( |
||
18 | 'Tag' => 'HTMLText', |
||
19 | ); |
||
20 | |||
21 | /** |
||
22 | * @config |
||
23 | * @var int The width of an image thumbnail in a strip. |
||
24 | */ |
||
25 | private static $strip_thumbnail_width = 50; |
||
26 | |||
27 | /** |
||
28 | * @config |
||
29 | * @var int The height of an image thumbnail in a strip. |
||
30 | */ |
||
31 | private static $strip_thumbnail_height = 50; |
||
32 | |||
33 | /** |
||
34 | * @config |
||
35 | * @var int The width of an image thumbnail in the CMS. |
||
36 | */ |
||
37 | private static $cms_thumbnail_width = 100; |
||
38 | |||
39 | /** |
||
40 | * @config |
||
41 | * @var int The height of an image thumbnail in the CMS. |
||
42 | */ |
||
43 | private static $cms_thumbnail_height = 100; |
||
44 | |||
45 | /** |
||
46 | * @config |
||
47 | * @var int The width of an image thumbnail in the Asset section. |
||
48 | */ |
||
49 | private static $asset_thumbnail_width = 100; |
||
50 | |||
51 | /** |
||
52 | * @config |
||
53 | * @var int The height of an image thumbnail in the Asset section. |
||
54 | */ |
||
55 | private static $asset_thumbnail_height = 100; |
||
56 | |||
57 | /** |
||
58 | * @config |
||
59 | * @var int The width of an image preview in the Asset section. |
||
60 | */ |
||
61 | private static $asset_preview_width = 400; |
||
62 | |||
63 | /** |
||
64 | * @config |
||
65 | * @var int The height of an image preview in the Asset section. |
||
66 | */ |
||
67 | private static $asset_preview_height = 200; |
||
68 | |||
69 | /** |
||
70 | * @config |
||
71 | * @var bool Force all images to resample in all cases |
||
72 | */ |
||
73 | private static $force_resample = false; |
||
74 | |||
75 | /** |
||
76 | * @config |
||
77 | * @var bool Regenerates images if set to true. This is set by {@link flush()} |
||
78 | */ |
||
79 | private static $flush = false; |
||
80 | |||
81 | /** |
||
82 | * Triggered early in the request when someone requests a flush. |
||
83 | */ |
||
84 | public static function flush() { |
||
87 | |||
88 | public static function set_backend($backend) { |
||
91 | |||
92 | public static function get_backend() { |
||
95 | |||
96 | /** |
||
97 | * Set up template methods to access the transformations generated by 'generate' methods. |
||
98 | */ |
||
99 | public function defineMethods() { |
||
109 | |||
110 | public function getCMSFields() { |
||
124 | |||
125 | /** |
||
126 | * Return an XHTML img tag for this Image, |
||
127 | * or NULL if the image file doesn't exist on the filesystem. |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | public function getTag() { |
||
145 | |||
146 | /** |
||
147 | * Return an XHTML img tag for this Image. |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | public function forTemplate() { |
||
154 | |||
155 | /** |
||
156 | * File names are filtered through {@link FileNameFilter}, see class documentation |
||
157 | * on how to influence this behaviour. |
||
158 | * |
||
159 | * @deprecated 4.0 |
||
160 | */ |
||
161 | public function loadUploadedImage($tmpFile) { |
||
205 | |||
206 | /** |
||
207 | * Scale image proportionally to fit within the specified bounds |
||
208 | * |
||
209 | * @param integer $width The width to size within |
||
210 | * @param integer $height The height to size within |
||
211 | * @return Image|null |
||
212 | */ |
||
213 | public function Fit($width, $height) { |
||
232 | |||
233 | /** |
||
234 | * Scale image proportionally to fit within the specified bounds |
||
235 | * |
||
236 | * @param Image_Backend $backend |
||
237 | * @param integer $width The width to size within |
||
238 | * @param integer $height The height to size within |
||
239 | * @return Image_Backend |
||
240 | */ |
||
241 | public function generateFit(Image_Backend $backend, $width, $height) { |
||
244 | |||
245 | /** |
||
246 | * Proportionally scale down this image if it is wider or taller than the specified dimensions. |
||
247 | * Similar to Fit but without up-sampling. Use in templates with $FitMax. |
||
248 | * |
||
249 | * @uses Image::Fit() |
||
250 | * @param integer $width The maximum width of the output image |
||
251 | * @param integer $height The maximum height of the output image |
||
252 | * @return Image |
||
253 | */ |
||
254 | public function FitMax($width, $height) { |
||
262 | |||
263 | /** |
||
264 | * Resize and crop image to fill specified dimensions. |
||
265 | * Use in templates with $Fill |
||
266 | * |
||
267 | * @param integer $width Width to crop to |
||
268 | * @param integer $height Height to crop to |
||
269 | * @return Image|null |
||
270 | */ |
||
271 | public function Fill($width, $height) { |
||
276 | |||
277 | /** |
||
278 | * Resize and crop image to fill specified dimensions. |
||
279 | * Use in templates with $Fill |
||
280 | * |
||
281 | * @param Image_Backend $backend |
||
282 | * @param integer $width Width to crop to |
||
283 | * @param integer $height Height to crop to |
||
284 | * @return Image_Backend |
||
285 | */ |
||
286 | public function generateFill(Image_Backend $backend, $width, $height) { |
||
289 | |||
290 | /** |
||
291 | * Crop this image to the aspect ratio defined by the specified width and height, |
||
292 | * then scale down the image to those dimensions if it exceeds them. |
||
293 | * Similar to Fill but without up-sampling. Use in templates with $FillMax. |
||
294 | * |
||
295 | * @uses Image::Fill() |
||
296 | * @param integer $width The relative (used to determine aspect ratio) and maximum width of the output image |
||
297 | * @param integer $height The relative (used to determine aspect ratio) and maximum height of the output image |
||
298 | * @return Image |
||
299 | */ |
||
300 | public function FillMax($width, $height) { |
||
320 | |||
321 | /** |
||
322 | * Fit image to specified dimensions and fill leftover space with a solid colour (default white). Use in templates with $Pad. |
||
323 | * |
||
324 | * @param integer $width The width to size to |
||
325 | * @param integer $height The height to size to |
||
326 | * @return Image|null |
||
327 | */ |
||
328 | public function Pad($width, $height, $backgroundColor='FFFFFF') { |
||
333 | |||
334 | /** |
||
335 | * Fit image to specified dimensions and fill leftover space with a solid colour (default white). Use in templates with $Pad. |
||
336 | * |
||
337 | * @param Image_Backend $backend |
||
338 | * @param integer $width The width to size to |
||
339 | * @param integer $height The height to size to |
||
340 | * @return Image_Backend |
||
341 | */ |
||
342 | public function generatePad(Image_Backend $backend, $width, $height, $backgroundColor='FFFFFF') { |
||
345 | |||
346 | /** |
||
347 | * Scale image proportionally by width. Use in templates with $ScaleWidth. |
||
348 | * |
||
349 | * @param integer $width The width to set |
||
350 | * @return Image|null |
||
351 | */ |
||
352 | public function ScaleWidth($width) { |
||
357 | |||
358 | /** |
||
359 | * Scale image proportionally by width. Use in templates with $ScaleWidth. |
||
360 | * |
||
361 | * @param Image_Backend $backend |
||
362 | * @param int $width The width to set |
||
363 | * @return Image_Backend |
||
364 | */ |
||
365 | public function generateScaleWidth(Image_Backend $backend, $width) { |
||
368 | |||
369 | /** |
||
370 | * Proportionally scale down this image if it is wider than the specified width. |
||
371 | * Similar to ScaleWidth but without up-sampling. Use in templates with $ScaleMaxWidth. |
||
372 | * |
||
373 | * @uses Image::ScaleWidth() |
||
374 | * @param integer $width The maximum width of the output image |
||
375 | * @return Image |
||
376 | */ |
||
377 | public function ScaleMaxWidth($width) { |
||
385 | |||
386 | /** |
||
387 | * Scale image proportionally by height. Use in templates with $ScaleHeight. |
||
388 | * |
||
389 | * @param integer $height The height to set |
||
390 | * @return Image|null |
||
391 | */ |
||
392 | public function ScaleHeight($height) { |
||
397 | |||
398 | /** |
||
399 | * Scale image proportionally by height. Use in templates with $ScaleHeight. |
||
400 | * |
||
401 | * @param Image_Backend $backend |
||
402 | * @param integer $height The height to set |
||
403 | * @return Image_Backend |
||
404 | */ |
||
405 | public function generateScaleHeight(Image_Backend $backend, $height){ |
||
408 | |||
409 | /** |
||
410 | * Proportionally scale down this image if it is taller than the specified height. |
||
411 | * Similar to ScaleHeight but without up-sampling. Use in templates with $ScaleMaxHeight. |
||
412 | * |
||
413 | * @uses Image::ScaleHeight() |
||
414 | * @param integer $height The maximum height of the output image |
||
415 | * @return Image |
||
416 | */ |
||
417 | public function ScaleMaxHeight($height) { |
||
425 | |||
426 | /** |
||
427 | * Crop image on X axis if it exceeds specified width. Retain height. |
||
428 | * Use in templates with $CropWidth. Example: $Image.ScaleHeight(100).$CropWidth(100) |
||
429 | * |
||
430 | * @uses Image::Fill() |
||
431 | * @param integer $width The maximum width of the output image |
||
432 | * @return Image |
||
433 | */ |
||
434 | public function CropWidth($width) { |
||
442 | |||
443 | /** |
||
444 | * Crop image on Y axis if it exceeds specified height. Retain width. |
||
445 | * Use in templates with $CropHeight. Example: $Image.ScaleWidth(100).CropHeight(100) |
||
446 | * |
||
447 | * @uses Image::Fill() |
||
448 | * @param integer $height The maximum height of the output image |
||
449 | * @return Image |
||
450 | */ |
||
451 | public function CropHeight($height) { |
||
459 | |||
460 | /** |
||
461 | * Resize the image by preserving aspect ratio, keeping the image inside the |
||
462 | * $width and $height |
||
463 | * |
||
464 | * @param integer $width The width to size within |
||
465 | * @param integer $height The height to size within |
||
466 | * @return Image |
||
467 | * @deprecated 4.0 Use Fit instead |
||
468 | */ |
||
469 | public function SetRatioSize($width, $height) { |
||
473 | |||
474 | /** |
||
475 | * Resize the image by preserving aspect ratio, keeping the image inside the |
||
476 | * $width and $height |
||
477 | * |
||
478 | * @param Image_Backend $backend |
||
479 | * @param integer $width The width to size within |
||
480 | * @param integer $height The height to size within |
||
481 | * @return Image_Backend |
||
482 | * @deprecated 4.0 Use generateFit instead |
||
483 | */ |
||
484 | public function generateSetRatioSize(Image_Backend $backend, $width, $height) { |
||
488 | |||
489 | /** |
||
490 | * Resize this Image by width, keeping aspect ratio. Use in templates with $SetWidth. |
||
491 | * |
||
492 | * @param integer $width The width to set |
||
493 | * @return Image |
||
494 | * @deprecated 4.0 Use ScaleWidth instead |
||
495 | */ |
||
496 | public function SetWidth($width) { |
||
500 | |||
501 | /** |
||
502 | * Resize this Image by width, keeping aspect ratio. Use in templates with $SetWidth. |
||
503 | * |
||
504 | * @param Image_Backend $backend |
||
505 | * @param int $width The width to set |
||
506 | * @return Image_Backend |
||
507 | * @deprecated 4.0 Use generateScaleWidth instead |
||
508 | */ |
||
509 | public function generateSetWidth(Image_Backend $backend, $width) { |
||
513 | |||
514 | /** |
||
515 | * Resize this Image by height, keeping aspect ratio. Use in templates with $SetHeight. |
||
516 | * |
||
517 | * @param integer $height The height to set |
||
518 | * @return Image |
||
519 | * @deprecated 4.0 Use ScaleHeight instead |
||
520 | */ |
||
521 | public function SetHeight($height) { |
||
525 | |||
526 | /** |
||
527 | * Resize this Image by height, keeping aspect ratio. Use in templates with $SetHeight. |
||
528 | * |
||
529 | * @param Image_Backend $backend |
||
530 | * @param integer $height The height to set |
||
531 | * @return Image_Backend |
||
532 | * @deprecated 4.0 Use generateScaleHeight instead |
||
533 | */ |
||
534 | public function generateSetHeight(Image_Backend $backend, $height){ |
||
538 | |||
539 | /** |
||
540 | * Resize this Image by both width and height, using padded resize. Use in templates with $SetSize. |
||
541 | * @see Image::PaddedImage() |
||
542 | * |
||
543 | * @param integer $width The width to size to |
||
544 | * @param integer $height The height to size to |
||
545 | * @return Image |
||
546 | * @deprecated 4.0 Use Pad instead |
||
547 | */ |
||
548 | public function SetSize($width, $height) { |
||
552 | |||
553 | /** |
||
554 | * Resize this Image by both width and height, using padded resize. Use in templates with $SetSize. |
||
555 | * |
||
556 | * @param Image_Backend $backend |
||
557 | * @param integer $width The width to size to |
||
558 | * @param integer $height The height to size to |
||
559 | * @return Image_Backend |
||
560 | * @deprecated 4.0 Use generatePad instead |
||
561 | */ |
||
562 | public function generateSetSize(Image_Backend $backend, $width, $height) { |
||
566 | |||
567 | /** |
||
568 | * Resize this image for the CMS. Use in templates with $CMSThumbnail |
||
569 | * |
||
570 | * @return Image_Cached|null |
||
571 | */ |
||
572 | public function CMSThumbnail() { |
||
575 | |||
576 | /** |
||
577 | * Resize this image for the CMS. Use in templates with $CMSThumbnail. |
||
578 | * @return Image_Backend |
||
579 | */ |
||
580 | public function generateCMSThumbnail(Image_Backend $backend) { |
||
583 | |||
584 | /** |
||
585 | * Resize this image for preview in the Asset section. Use in templates with $AssetLibraryPreview. |
||
586 | * @return Image_Backend |
||
587 | */ |
||
588 | public function generateAssetLibraryPreview(Image_Backend $backend) { |
||
591 | |||
592 | /** |
||
593 | * Resize this image for thumbnail in the Asset section. Use in templates with $AssetLibraryThumbnail. |
||
594 | * @return Image_Backend |
||
595 | */ |
||
596 | public function generateAssetLibraryThumbnail(Image_Backend $backend) { |
||
599 | |||
600 | /** |
||
601 | * Resize this image for use as a thumbnail in a strip. Use in templates with $StripThumbnail. |
||
602 | * @return Image_Backend |
||
603 | */ |
||
604 | public function generateStripThumbnail(Image_Backend $backend) { |
||
607 | |||
608 | /** |
||
609 | * Resize this Image by both width and height, using padded resize. Use in templates with $PaddedImage. |
||
610 | * @see Image::SetSize() |
||
611 | * |
||
612 | * @param integer $width The width to size to |
||
613 | * @param integer $height The height to size to |
||
614 | * @return Image |
||
615 | * @deprecated 4.0 Use Pad instead |
||
616 | */ |
||
617 | public function PaddedImage($width, $height, $backgroundColor='FFFFFF') { |
||
621 | |||
622 | /** |
||
623 | * Resize this Image by both width and height, using padded resize. Use in templates with $PaddedImage. |
||
624 | * |
||
625 | * @param Image_Backend $backend |
||
626 | * @param integer $width The width to size to |
||
627 | * @param integer $height The height to size to |
||
628 | * @return Image_Backend |
||
629 | * @deprecated 4.0 Use generatePad instead |
||
630 | */ |
||
631 | public function generatePaddedImage(Image_Backend $backend, $width, $height, $backgroundColor='FFFFFF') { |
||
635 | |||
636 | /** |
||
637 | * Determine if this image is of the specified size |
||
638 | * |
||
639 | * @param integer $width Width to check |
||
640 | * @param integer $height Height to check |
||
641 | * @return boolean |
||
642 | */ |
||
643 | public function isSize($width, $height) { |
||
646 | |||
647 | /** |
||
648 | * Determine if this image is of the specified width |
||
649 | * |
||
650 | * @param integer $width Width to check |
||
651 | * @return boolean |
||
652 | */ |
||
653 | public function isWidth($width) { |
||
656 | |||
657 | /** |
||
658 | * Determine if this image is of the specified width |
||
659 | * |
||
660 | * @param integer $height Height to check |
||
661 | * @return boolean |
||
662 | */ |
||
663 | public function isHeight($height) { |
||
666 | |||
667 | /** |
||
668 | * Return an image object representing the image in the given format. |
||
669 | * This image will be generated using generateFormattedImage(). |
||
670 | * The generated image is cached, to flush the cache append ?flush=1 to your URL. |
||
671 | * |
||
672 | * Just pass the correct number of parameters expected by the working function |
||
673 | * |
||
674 | * @param string $format The name of the format. |
||
675 | * @return Image_Cached|null |
||
676 | */ |
||
677 | public function getFormattedImage($format) { |
||
695 | |||
696 | /** |
||
697 | * Return the filename for the cached image, given its format name and arguments. |
||
698 | * @param string $format The format name. |
||
699 | * @return string |
||
700 | * @throws InvalidArgumentException |
||
701 | */ |
||
702 | public function cacheFilename($format) { |
||
717 | |||
718 | /** |
||
719 | * Generate an image on the specified format. It will save the image |
||
720 | * at the location specified by cacheFilename(). The image will be generated |
||
721 | * using the specific 'generate' method for the specified format. |
||
722 | * |
||
723 | * @param string $format Name of the format to generate. |
||
724 | */ |
||
725 | public function generateFormattedImage($format) { |
||
753 | |||
754 | /** |
||
755 | * Generate a resized copy of this image with the given width & height. |
||
756 | * This can be used in templates with $ResizedImage but should be avoided, |
||
757 | * as it's the only image manipulation function which can skew an image. |
||
758 | * |
||
759 | * @param integer $width Width to resize to |
||
760 | * @param integer $height Height to resize to |
||
761 | * @return Image |
||
762 | */ |
||
763 | public function ResizedImage($width, $height) { |
||
768 | |||
769 | /** |
||
770 | * Generate a resized copy of this image with the given width & height. |
||
771 | * Use in templates with $ResizedImage. |
||
772 | * |
||
773 | * @param Image_Backend $backend |
||
774 | * @param integer $width Width to resize to |
||
775 | * @param integer $height Height to resize to |
||
776 | * @return Image_Backend|null |
||
777 | */ |
||
778 | public function generateResizedImage(Image_Backend $backend, $width, $height) { |
||
786 | |||
787 | /** |
||
788 | * Generate a resized copy of this image with the given width & height, cropping to maintain aspect ratio. |
||
789 | * Use in templates with $CroppedImage |
||
790 | * |
||
791 | * @param integer $width Width to crop to |
||
792 | * @param integer $height Height to crop to |
||
793 | * @return Image |
||
794 | * @deprecated 4.0 Use Fill instead |
||
795 | */ |
||
796 | public function CroppedImage($width, $height) { |
||
800 | |||
801 | /** |
||
802 | * Generate a resized copy of this image with the given width & height, cropping to maintain aspect ratio. |
||
803 | * Use in templates with $CroppedImage |
||
804 | * |
||
805 | * @param Image_Backend $backend |
||
806 | * @param integer $width Width to crop to |
||
807 | * @param integer $height Height to crop to |
||
808 | * @return Image_Backend |
||
809 | * @deprecated 4.0 Use generateFill instead |
||
810 | */ |
||
811 | public function generateCroppedImage(Image_Backend $backend, $width, $height) { |
||
815 | |||
816 | /** |
||
817 | * Generate patterns that will help to match filenames of cached images |
||
818 | * @param string $filename Filename of source image |
||
819 | * @return array |
||
820 | */ |
||
821 | private function getFilenamePatterns($filename) { |
||
839 | |||
840 | /** |
||
841 | * Generate a list of images that were generated from this image |
||
842 | */ |
||
843 | private function getGeneratedImages() { |
||
888 | |||
889 | /** |
||
890 | * Regenerate all of the formatted cached images for this image. |
||
891 | * |
||
892 | * @return int The number of formatted images regenerated |
||
893 | */ |
||
894 | public function regenerateFormattedImages() { |
||
918 | |||
919 | /** |
||
920 | * Remove all of the formatted cached images for this image. |
||
921 | * |
||
922 | * @return int The number of formatted images deleted |
||
923 | */ |
||
924 | public function deleteFormattedImages() { |
||
936 | |||
937 | /** |
||
938 | * Get the dimensions of this Image. |
||
939 | * @param string $dim If this is equal to "string", return the dimensions in string form, |
||
940 | * if it is 0 return the height, if it is 1 return the width. |
||
941 | * @return string|int|null |
||
942 | */ |
||
943 | public function getDimensions($dim = "string") { |
||
955 | |||
956 | /** |
||
957 | * Get the width of this image. |
||
958 | * @return int |
||
959 | */ |
||
960 | public function getWidth() { |
||
963 | |||
964 | /** |
||
965 | * Get the height of this image. |
||
966 | * @return int |
||
967 | */ |
||
968 | public function getHeight() { |
||
971 | |||
972 | /** |
||
973 | * Get the orientation of this image. |
||
974 | * @return ORIENTATION_SQUARE | ORIENTATION_PORTRAIT | ORIENTATION_LANDSCAPE |
||
975 | */ |
||
976 | public function getOrientation() { |
||
987 | |||
988 | public function onAfterUpload() { |
||
992 | |||
993 | protected function onBeforeDelete() { |
||
1001 | } |
||
1002 | |||
1057 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.