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 |
||
21 | class ImageWrapper extends \yii\base\Object |
||
22 | { |
||
23 | |||
24 | /** @var \rmrevin\yii\module\File\models\File */ |
||
25 | public $File = null; |
||
26 | |||
27 | /** @var array */ |
||
28 | public $result = [null, null]; |
||
29 | |||
30 | /** @var array */ |
||
31 | private $mark = []; |
||
32 | |||
33 | /** |
||
34 | * @param \rmrevin\yii\module\File\models\File $File |
||
35 | * @return self |
||
36 | */ |
||
37 | 4 | public static function load(File $File) |
|
41 | |||
42 | /** |
||
43 | * @return string |
||
44 | */ |
||
45 | 1 | public function __toString() |
|
49 | |||
50 | /** |
||
51 | * @param callable $handler |
||
52 | */ |
||
53 | 1 | public function save($handler) |
|
68 | |||
69 | /** |
||
70 | * @param integer $width |
||
71 | * @param integer $height |
||
72 | * @param string $filter |
||
73 | * @return self |
||
74 | */ |
||
75 | 1 | View Code Duplication | public function resize($width, $height, $filter = ImageInterface::FILTER_UNDEFINED) |
86 | |||
87 | /** |
||
88 | * @param integer $width |
||
89 | * @param string $filter |
||
90 | * @return self |
||
91 | */ |
||
92 | 1 | View Code Duplication | public function resizeByWidth($width, $filter = ImageInterface::FILTER_UNDEFINED) |
104 | |||
105 | /** |
||
106 | * @param integer $height |
||
107 | * @param string $filter |
||
108 | * @return self |
||
109 | */ |
||
110 | 1 | View Code Duplication | public function resizeByHeight($height, $filter = ImageInterface::FILTER_UNDEFINED) |
121 | |||
122 | /** |
||
123 | * @param integer $width |
||
124 | * @param integer $height |
||
125 | * @param array $start |
||
126 | * @return self |
||
127 | */ |
||
128 | 1 | View Code Duplication | public function crop($width, $height, array $start = [0, 0]) |
139 | |||
140 | /** |
||
141 | * @param integer $width |
||
142 | * @param integer $height |
||
143 | * @param string $mode |
||
144 | * @return self |
||
145 | */ |
||
146 | 1 | View Code Duplication | public function thumbnail($width, $height, $mode = ManipulatorInterface::THUMBNAIL_OUTBOUND) |
157 | |||
158 | /** |
||
159 | * @param string $watermarkFilename |
||
160 | * @param array $start |
||
161 | * @return self |
||
162 | */ |
||
163 | 1 | public function watermark($watermarkFilename, array $start = [0, 0]) |
|
174 | |||
175 | /** |
||
176 | * @param string $text |
||
177 | * @param string $fontFile |
||
178 | * @param array $start |
||
179 | * @param array $fontOptions |
||
180 | * @return self |
||
181 | */ |
||
182 | 1 | View Code Duplication | public function text($text, $fontFile, array $start = [0, 0], array $fontOptions = []) |
193 | |||
194 | /** |
||
195 | * @param int $margin |
||
196 | * @param string $color |
||
197 | * @param int $alpha |
||
198 | * @return self |
||
199 | */ |
||
200 | 1 | public function frame($margin = 20, $color = '666', $alpha = 100) |
|
211 | |||
212 | /** |
||
213 | * @return string[] |
||
214 | */ |
||
215 | 1 | private function getPath() |
|
227 | |||
228 | /** |
||
229 | * @param string $mark |
||
230 | * @return string[] |
||
231 | */ |
||
232 | 1 | private function getMarkedFilePath($mark) |
|
244 | |||
245 | /** |
||
246 | * @param \Imagine\Image\ImageInterface $Image |
||
247 | * @param string $mark |
||
248 | */ |
||
249 | 1 | private function createMarkedFile(ImageInterface $Image, $mark) |
|
265 | |||
266 | |||
267 | /** |
||
268 | * @param string $method |
||
269 | * @param array $data |
||
270 | */ |
||
271 | 1 | private function mark($method, array $data) |
|
275 | |||
276 | /** |
||
277 | * @return string |
||
278 | */ |
||
279 | 1 | private function calculateMark() |
|
283 | } |
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.