Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ImagesManager 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 ImagesManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class ImagesManager |
||
|
|
|||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Path name for origins images folder without uploads path |
||
| 25 | */ |
||
| 26 | const IMAGES_ORIGIN_DIR_PATH = '/uploads/images/bimages/'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Path name for origins images folder without uploads path |
||
| 30 | */ |
||
| 31 | const IMAGES_TUNED_DIR_PATH = '/uploads/banners/tuned/'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Image file input name |
||
| 35 | */ |
||
| 36 | const IMAGE_FILE_FIELD = 'file-image'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Image upload file allowed types |
||
| 40 | */ |
||
| 41 | const IMAGES_UPLOAD_ALLOWED_TYPES = 'jpg|gif|png|jpeg'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Image file max size |
||
| 45 | */ |
||
| 46 | const IMAGE_MAX_SIZE = '51200'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var ImagesManager instance |
||
| 50 | */ |
||
| 51 | private static $instance = NULL; |
||
| 52 | |||
| 53 | private function __construct() { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get ImagesManager instance |
||
| 59 | * @return ImagesManager instance |
||
| 60 | */ |
||
| 61 | public static function getInstance() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Delete images if they not used |
||
| 70 | * @deprecated |
||
| 71 | */ |
||
| 72 | public function deleteNotExistingImages() { |
||
| 88 | |||
| 89 | /** Get tuned image path |
||
| 90 | * @param string $image_name - banner image name |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | public function getImageOriginPath($image_name = NULL) { |
||
| 96 | |||
| 97 | /** Get origin image path |
||
| 98 | * @param string $image_name - banner image name |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | public function getImageTunedPath($image_name = NULL) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Delete image files: origin image and tuned image from uploads |
||
| 107 | * @param int $imageId |
||
| 108 | * @param null|string $locale |
||
| 109 | * @return bool |
||
| 110 | * @throws PropelException |
||
| 111 | */ |
||
| 112 | public function delete($imageId, $locale = NULL) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Delete image file |
||
| 140 | * @param string $image_path - path to image file |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | private function deleteImageFile($image_path) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param string $path |
||
| 153 | */ |
||
| 154 | protected function buildImagePath($path) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Save image file |
||
| 166 | * @param $imageId |
||
| 167 | * @param $locale |
||
| 168 | * @return string |
||
| 169 | * @throws Exception |
||
| 170 | */ |
||
| 171 | public function saveImage($imageId = NULL, $locale = NULL) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Upload image |
||
| 205 | * @throws \Exception |
||
| 206 | */ |
||
| 207 | public function uploadImage() { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Check on errors exists while upload file |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | private function checkImageUploadErrors() { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get images ordered by page types |
||
| 257 | * @param Banners $banner - banner object |
||
| 258 | * @param string $locale - locale name |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | public function getImagesByPageType($banner, $locale) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Prepare banner image data for save into DB |
||
| 316 | * @param array $data - image data array |
||
| 317 | * @param $bannerId - banner id |
||
| 318 | * @param $locale - locale name |
||
| 319 | * @return array |
||
| 320 | */ |
||
| 321 | public function prepareImageData(array $data, $bannerId, $locale) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Make inactive banner images that has time out active_to date |
||
| 350 | */ |
||
| 351 | public function setInactiveOnTimeOut() { |
||
| 357 | |||
| 358 | } |