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:
1 | <?php |
||
9 | class ImageResizer |
||
10 | { |
||
11 | protected $imageSet; |
||
12 | |||
13 | /** |
||
14 | * ImageResizer constructor. |
||
15 | * |
||
16 | * @param $imageSet |
||
17 | */ |
||
18 | public function __construct($imageSet) |
||
22 | |||
23 | /** |
||
24 | * @param $imagePath |
||
25 | * |
||
26 | * @return array |
||
27 | * @throws \Exception |
||
28 | */ |
||
29 | public function applyImageSetToImage($imagePath) |
||
49 | |||
50 | /** |
||
51 | * @param string $imagePath |
||
52 | * @param string $width |
||
53 | * @param string $height |
||
54 | * @return string |
||
55 | * @throws \Exception |
||
56 | */ |
||
57 | public function resize($imagePath='', $width='',$height='') |
||
62 | |||
63 | /** |
||
64 | * @param string $imagePath |
||
65 | * @param string $width |
||
66 | * @param string $height |
||
67 | * @return string |
||
68 | * @throws \Exception |
||
69 | */ |
||
70 | public function smartcrop($imagePath='', $width='',$height='') |
||
75 | |||
76 | /** |
||
77 | * @param string $imagePath |
||
78 | * @param string $width |
||
79 | * @param string $height |
||
80 | * @return string |
||
81 | * @throws \Exception |
||
82 | */ |
||
83 | public function boxcrop($imagePath='', $width='',$height='') |
||
88 | |||
89 | /** |
||
90 | * @param $imagePath |
||
91 | * @param string $modifier |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | private function modifyName($imagePath, $modifier='') |
||
125 | |||
126 | private function applyMethod($method, $imagePath, $width, $height, $modifier) |
||
145 | } |
||
146 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.