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) { |
||
| 113 | |||
| 114 | /** Get origin image path |
||
| 115 | * @param string $image_name - banner image name |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function getImageTunedPath($image_name = NULL) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Delete image files: origin image and tuned image from uploads |
||
| 124 | * @param int $imageId |
||
| 125 | * @param null|string $locale |
||
| 126 | * @return bool |
||
| 127 | * @throws PropelException |
||
| 128 | */ |
||
| 129 | public function delete($imageId, $locale = NULL) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Delete image file |
||
| 157 | * @param string $image_path - path to image file |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | private function deleteImageFile($image_path) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $path |
||
| 170 | */ |
||
| 171 | protected function buildImagePath($path) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Save image file |
||
| 183 | * @param $imageId |
||
| 184 | * @param $locale |
||
| 185 | * @return string |
||
| 186 | * @throws Exception |
||
| 187 | */ |
||
| 188 | public function saveImage($imageId = NULL, $locale = NULL) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Upload image |
||
| 222 | * @throws \Exception |
||
| 223 | */ |
||
| 224 | public function uploadImage() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Check on errors exists while upload file |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | private function checkImageUploadErrors() { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get images ordered by page types |
||
| 274 | * @param Banners $banner - banner object |
||
| 275 | * @param string $locale - locale name |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | public function getImagesByPageType($banner, $locale) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Prepare banner image data for save into DB |
||
| 333 | * @param array $data - image data array |
||
| 334 | * @param $bannerId - banner id |
||
| 335 | * @param $locale - locale name |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | public function prepareImageData(array $data, $bannerId, $locale) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Make inactive banner images that has time out active_to date |
||
| 367 | */ |
||
| 368 | public function setInactiveOnTimeOut() { |
||
| 374 | |||
| 375 | } |