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 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 namespace Anomaly\Streams\Platform\Image; |
||
26 | class Image |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * The publish flag. |
||
31 | * |
||
32 | * @var bool |
||
33 | */ |
||
34 | protected $publish = false; |
||
35 | |||
36 | /** |
||
37 | * The publishable base directory. |
||
38 | * |
||
39 | * @var null |
||
40 | */ |
||
41 | protected $directory = null; |
||
42 | |||
43 | /** |
||
44 | * The image object. |
||
45 | * |
||
46 | * @var null|string |
||
47 | */ |
||
48 | protected $image = null; |
||
49 | |||
50 | /** |
||
51 | * The file extension. |
||
52 | * |
||
53 | * @var null|string |
||
54 | */ |
||
55 | protected $extension = null; |
||
56 | |||
57 | /** |
||
58 | * The desired filename. |
||
59 | * |
||
60 | * @var null|string |
||
61 | */ |
||
62 | protected $filename = null; |
||
63 | |||
64 | /** |
||
65 | * The original filename. |
||
66 | * |
||
67 | * @var null|string |
||
68 | */ |
||
69 | protected $original = null; |
||
70 | |||
71 | /** |
||
72 | * The version flag. |
||
73 | * |
||
74 | * @var null|boolean |
||
75 | */ |
||
76 | protected $version = null; |
||
77 | |||
78 | /** |
||
79 | * The default output method. |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $output = 'url'; |
||
84 | |||
85 | /** |
||
86 | * The image attributes. |
||
87 | * |
||
88 | * @var array |
||
89 | */ |
||
90 | protected $attributes = []; |
||
91 | |||
92 | /** |
||
93 | * Applied alterations. |
||
94 | * |
||
95 | * @var array |
||
96 | */ |
||
97 | protected $alterations = []; |
||
98 | |||
99 | /** |
||
100 | * Image srcsets. |
||
101 | * |
||
102 | * @var array |
||
103 | */ |
||
104 | protected $srcsets = []; |
||
105 | |||
106 | /** |
||
107 | * Image sources. |
||
108 | * |
||
109 | * @var array |
||
110 | */ |
||
111 | protected $sources = []; |
||
112 | |||
113 | /** |
||
114 | * Allowed methods. |
||
115 | * |
||
116 | * @var array |
||
117 | */ |
||
118 | protected $allowedMethods = [ |
||
119 | 'blur', |
||
120 | 'brightness', |
||
121 | 'colorize', |
||
122 | 'resizeCanvas', |
||
123 | 'contrast', |
||
124 | 'crop', |
||
125 | 'encode', |
||
126 | 'fit', |
||
127 | 'flip', |
||
128 | 'gamma', |
||
129 | 'greyscale', |
||
130 | 'heighten', |
||
131 | 'insert', |
||
132 | 'interlace', |
||
133 | 'invert', |
||
134 | 'limitColors', |
||
135 | 'pixelate', |
||
136 | 'opacity', |
||
137 | 'resize', |
||
138 | 'rotate', |
||
139 | 'amount', |
||
140 | 'widen', |
||
141 | 'orientate', |
||
142 | ]; |
||
143 | |||
144 | /** |
||
145 | * The quality of the output. |
||
146 | * |
||
147 | * @var null|int |
||
148 | */ |
||
149 | protected $quality = null; |
||
150 | |||
151 | /** |
||
152 | * The image width. |
||
153 | * |
||
154 | * @var null|int |
||
155 | */ |
||
156 | protected $width = null; |
||
157 | |||
158 | /** |
||
159 | * The image height. |
||
160 | * |
||
161 | * @var null|int |
||
162 | */ |
||
163 | protected $height = null; |
||
164 | |||
165 | /** |
||
166 | * The URL generator. |
||
167 | * |
||
168 | * @var UrlGenerator |
||
169 | */ |
||
170 | protected $url; |
||
171 | |||
172 | /** |
||
173 | * The HTML builder. |
||
174 | * |
||
175 | * @var HtmlBuilder |
||
176 | */ |
||
177 | protected $html; |
||
178 | |||
179 | /** |
||
180 | * Image path hints by namespace. |
||
181 | * |
||
182 | * @var ImagePaths |
||
183 | */ |
||
184 | protected $paths; |
||
185 | |||
186 | /** |
||
187 | * The image macros. |
||
188 | * |
||
189 | * @var ImageMacros |
||
190 | */ |
||
191 | protected $macros; |
||
192 | |||
193 | /** |
||
194 | * The file system. |
||
195 | * |
||
196 | * @var Filesystem |
||
197 | */ |
||
198 | protected $files; |
||
199 | |||
200 | /** |
||
201 | * The user agent utility. |
||
202 | * |
||
203 | * @var Mobile_Detect |
||
204 | */ |
||
205 | protected $agent; |
||
206 | |||
207 | /** |
||
208 | * The config repository. |
||
209 | * |
||
210 | * @var Repository |
||
211 | */ |
||
212 | protected $config; |
||
213 | |||
214 | /** |
||
215 | * The request object. |
||
216 | * |
||
217 | * @var Request |
||
218 | */ |
||
219 | protected $request; |
||
220 | |||
221 | /** |
||
222 | * The image manager. |
||
223 | * |
||
224 | * @var ImageManager |
||
225 | */ |
||
226 | protected $manager; |
||
227 | |||
228 | /** |
||
229 | * The stream application. |
||
230 | * |
||
231 | * @var Application |
||
232 | */ |
||
233 | protected $application; |
||
234 | |||
235 | /** |
||
236 | * Create a new Image instance. |
||
237 | * |
||
238 | * @param UrlGenerator $url |
||
239 | * @param HtmlBuilder $html |
||
240 | * @param Filesystem $files |
||
241 | * @param Mobile_Detect $agent |
||
242 | * @param Repository $config |
||
243 | * @param ImageManager $manager |
||
244 | * @param Request $request |
||
245 | * @param Application $application |
||
246 | * @param ImagePaths $paths |
||
247 | * @param ImageMacros $macros |
||
248 | */ |
||
249 | View Code Duplication | public function __construct( |
|
272 | |||
273 | /** |
||
274 | * Make a new image instance. |
||
275 | * |
||
276 | * @param mixed $image |
||
277 | * @param null $output |
||
278 | * @return $this |
||
279 | */ |
||
280 | public function make($image, $output = null) |
||
303 | |||
304 | /** |
||
305 | * Return the path to an image. |
||
306 | * |
||
307 | * @return string |
||
308 | */ |
||
309 | public function path() |
||
315 | |||
316 | /** |
||
317 | * Return the asset path to an image. |
||
318 | * |
||
319 | * @return string |
||
320 | */ |
||
321 | public function asset() |
||
327 | |||
328 | /** |
||
329 | * Run a macro on the image. |
||
330 | * |
||
331 | * @param $macro |
||
332 | * @return Image |
||
333 | * @throws \Exception |
||
334 | */ |
||
335 | public function macro($macro) |
||
339 | |||
340 | /** |
||
341 | * Return the URL to an image. |
||
342 | * |
||
343 | * @param array $parameters |
||
344 | * @param null $secure |
||
345 | * @return string |
||
346 | */ |
||
347 | public function url(array $parameters = [], $secure = null) |
||
351 | |||
352 | /** |
||
353 | * Return the image tag to an image. |
||
354 | * |
||
355 | * @param null $alt |
||
356 | * @param array $attributes |
||
357 | * @return string |
||
358 | */ |
||
359 | public function image($alt = null, array $attributes = []) |
||
385 | |||
386 | /** |
||
387 | * Return the image tag to an image. |
||
388 | * |
||
389 | * @param null $alt |
||
390 | * @param array $attributes |
||
391 | * @return string |
||
392 | */ |
||
393 | public function img($alt = null, array $attributes = []) |
||
397 | |||
398 | /** |
||
399 | * Return a picture tag. |
||
400 | * |
||
401 | * @return string |
||
402 | */ |
||
403 | public function picture(array $attributes = []) |
||
424 | |||
425 | /** |
||
426 | * Return a source tag. |
||
427 | * |
||
428 | * @return string |
||
429 | */ |
||
430 | public function source() |
||
442 | |||
443 | /** |
||
444 | * Return the image response. |
||
445 | * |
||
446 | * @param null $format |
||
447 | * @param int $quality |
||
448 | * @return String |
||
449 | */ |
||
450 | public function encode($format = null, $quality = null) |
||
454 | |||
455 | /** |
||
456 | * Return the image contents. |
||
457 | * |
||
458 | * @return string |
||
459 | */ |
||
460 | public function data() |
||
464 | |||
465 | /** |
||
466 | * Return the output. |
||
467 | * |
||
468 | * @return string |
||
469 | */ |
||
470 | public function output() |
||
474 | |||
475 | /** |
||
476 | * Set the filename. |
||
477 | * |
||
478 | * @param $filename |
||
479 | * @return $this |
||
480 | */ |
||
481 | public function rename($filename = null) |
||
485 | |||
486 | /** |
||
487 | * Set the version flag. |
||
488 | * |
||
489 | * @param bool $version |
||
490 | * @return $this |
||
491 | */ |
||
492 | public function version($version = true) |
||
496 | |||
497 | /** |
||
498 | * Set the quality. |
||
499 | * |
||
500 | * @param $quality |
||
501 | * @return $this |
||
502 | */ |
||
503 | public function quality($quality) |
||
507 | |||
508 | /** |
||
509 | * Set the width attribute. |
||
510 | * |
||
511 | * @param null $width |
||
512 | * @return Image |
||
513 | */ |
||
514 | public function width($width = null) |
||
518 | |||
519 | /** |
||
520 | * Set the height attribute. |
||
521 | * |
||
522 | * @param null $height |
||
523 | * @return Image |
||
524 | */ |
||
525 | public function height($height = null) |
||
529 | |||
530 | /** |
||
531 | * Set the quality. |
||
532 | * |
||
533 | * @param $quality |
||
534 | * @return $this |
||
535 | */ |
||
536 | public function setQuality($quality) |
||
542 | |||
543 | /** |
||
544 | * Get the cache path of the image. |
||
545 | * |
||
546 | * @return string |
||
547 | */ |
||
548 | protected function getCachePath() |
||
576 | |||
577 | /** |
||
578 | * Determine if the image needs to be published |
||
579 | * |
||
580 | * @param $path |
||
581 | * @return bool |
||
582 | */ |
||
583 | private function shouldPublish($path) |
||
612 | |||
613 | /** |
||
614 | * Publish an image to the publish directory. |
||
615 | * |
||
616 | * @param $path |
||
617 | */ |
||
618 | protected function publish($path) |
||
672 | |||
673 | /** |
||
674 | * Set an attribute value. |
||
675 | * |
||
676 | * @param $attribute |
||
677 | * @param $value |
||
678 | * @return $this |
||
679 | */ |
||
680 | public function attr($attribute, $value) |
||
686 | |||
687 | /** |
||
688 | * Return the image srcsets by set. |
||
689 | * |
||
690 | * @return array |
||
691 | */ |
||
692 | public function srcset() |
||
703 | |||
704 | /** |
||
705 | * Set the srcsets/alterations. |
||
706 | * |
||
707 | * @param array $srcsets |
||
708 | */ |
||
709 | public function srcsets(array $srcsets) |
||
729 | |||
730 | /** |
||
731 | * Set the sources/alterations. |
||
732 | * |
||
733 | * @param array $sources |
||
734 | * @param bool $merge |
||
735 | * @return $this |
||
736 | */ |
||
737 | public function sources(array $sources, $merge = true) |
||
765 | |||
766 | /** |
||
767 | * Alter the image based on the user agents. |
||
768 | * |
||
769 | * @param array $agents |
||
770 | * @param bool $exit |
||
771 | * @return $this |
||
772 | */ |
||
773 | public function agents(array $agents, $exit = false) |
||
799 | |||
800 | /** |
||
801 | * Return if an extension is supported. |
||
802 | * |
||
803 | * @param $extension |
||
804 | * @return bool |
||
805 | */ |
||
806 | protected function supportsType($extension) |
||
810 | |||
811 | /** |
||
812 | * Set the image. |
||
813 | * |
||
814 | * @param $image |
||
815 | * @return $this |
||
816 | */ |
||
817 | public function setImage($image) |
||
871 | |||
872 | /** |
||
873 | * Make an image instance. |
||
874 | * |
||
875 | * @return \Intervention\Image\Image |
||
876 | */ |
||
877 | protected function makeImage() |
||
901 | |||
902 | /** |
||
903 | * Dump an image instance's data. |
||
904 | * |
||
905 | * @return string |
||
906 | */ |
||
907 | protected function dumpImage() |
||
939 | |||
940 | /** |
||
941 | * Get the image instance. |
||
942 | * |
||
943 | * @return \Intervention\Image\Image |
||
944 | */ |
||
945 | public function getImage() |
||
949 | |||
950 | /** |
||
951 | * Get the file name. |
||
952 | * |
||
953 | * @return null|string |
||
954 | */ |
||
955 | public function getFilename() |
||
959 | |||
960 | /** |
||
961 | * Set the file name. |
||
962 | * |
||
963 | * @param $filename |
||
964 | * @return $this |
||
965 | */ |
||
966 | public function setFilename($filename = null) |
||
972 | |||
973 | /** |
||
974 | * Get the original name. |
||
975 | * |
||
976 | * @return null|string |
||
977 | */ |
||
978 | public function getOriginal() |
||
982 | |||
983 | /** |
||
984 | * Set the original name. |
||
985 | * |
||
986 | * @param $original |
||
987 | * @return $this |
||
988 | */ |
||
989 | public function setOriginal($original = null) |
||
995 | |||
996 | /** |
||
997 | * Get the file name. |
||
998 | * |
||
999 | * @return null|string |
||
1000 | */ |
||
1001 | public function getVersion() |
||
1005 | |||
1006 | /** |
||
1007 | * Set the file name. |
||
1008 | * |
||
1009 | * @param $version |
||
1010 | * @return $this |
||
1011 | */ |
||
1012 | public function setVersion($version = true) |
||
1018 | |||
1019 | /** |
||
1020 | * Get the alterations. |
||
1021 | * |
||
1022 | * @return array |
||
1023 | */ |
||
1024 | public function getAlterations() |
||
1028 | |||
1029 | /** |
||
1030 | * Set the alterations. |
||
1031 | * |
||
1032 | * @param array $alterations |
||
1033 | * @return $this |
||
1034 | */ |
||
1035 | public function setAlterations(array $alterations) |
||
1041 | |||
1042 | /** |
||
1043 | * Add an alteration. |
||
1044 | * |
||
1045 | * @param $method |
||
1046 | * @param $arguments |
||
1047 | * @return $this |
||
1048 | */ |
||
1049 | public function addAlteration($method, $arguments = []) |
||
1055 | |||
1056 | /** |
||
1057 | * Get the attributes. |
||
1058 | * |
||
1059 | * @return array |
||
1060 | */ |
||
1061 | public function getAttributes() |
||
1065 | |||
1066 | /** |
||
1067 | * Set the attributes. |
||
1068 | * |
||
1069 | * @param array $attributes |
||
1070 | * @return $this |
||
1071 | */ |
||
1072 | public function setAttributes(array $attributes) |
||
1078 | |||
1079 | /** |
||
1080 | * Add an attribute. |
||
1081 | * |
||
1082 | * @param $attribute |
||
1083 | * @param $value |
||
1084 | * @return $this |
||
1085 | */ |
||
1086 | protected function addAttribute($attribute, $value) |
||
1092 | |||
1093 | /** |
||
1094 | * Get the srcsets. |
||
1095 | * |
||
1096 | * @return array |
||
1097 | */ |
||
1098 | public function getSrcsets() |
||
1102 | |||
1103 | /** |
||
1104 | * Set the srcsets. |
||
1105 | * |
||
1106 | * @param array $srcsets |
||
1107 | * @return $this |
||
1108 | */ |
||
1109 | public function setSrcsets(array $srcsets) |
||
1115 | |||
1116 | /** |
||
1117 | * Get the sources. |
||
1118 | * |
||
1119 | * @return array |
||
1120 | */ |
||
1121 | public function getSources() |
||
1125 | |||
1126 | /** |
||
1127 | * Set the sources. |
||
1128 | * |
||
1129 | * @param array $sources |
||
1130 | * @return $this |
||
1131 | */ |
||
1132 | public function setSources(array $sources) |
||
1138 | |||
1139 | /** |
||
1140 | * Get the quality. |
||
1141 | * |
||
1142 | * @param null $default |
||
1143 | * @return int |
||
1144 | */ |
||
1145 | public function getQuality($default = null) |
||
1153 | |||
1154 | /** |
||
1155 | * Set the output mode. |
||
1156 | * |
||
1157 | * @param $output |
||
1158 | * @return $this |
||
1159 | */ |
||
1160 | public function setOutput($output) |
||
1166 | |||
1167 | /** |
||
1168 | * Get the extension. |
||
1169 | * |
||
1170 | * @return null|string |
||
1171 | */ |
||
1172 | public function getExtension() |
||
1176 | |||
1177 | /** |
||
1178 | * Set the extension. |
||
1179 | * |
||
1180 | * @param $extension |
||
1181 | * @return $this |
||
1182 | */ |
||
1183 | public function setExtension($extension) |
||
1189 | |||
1190 | /** |
||
1191 | * Get the allowed methods. |
||
1192 | * |
||
1193 | * @return array |
||
1194 | */ |
||
1195 | public function getAllowedMethods() |
||
1199 | |||
1200 | /** |
||
1201 | * Add a path by it's namespace hint. |
||
1202 | * |
||
1203 | * @param $namespace |
||
1204 | * @param $path |
||
1205 | * @return $this |
||
1206 | */ |
||
1207 | public function addPath($namespace, $path) |
||
1213 | |||
1214 | |||
1215 | /** |
||
1216 | * Get the width. |
||
1217 | * |
||
1218 | * @return int|null |
||
1219 | */ |
||
1220 | public function getWidth() |
||
1224 | |||
1225 | /** |
||
1226 | * Set the width. |
||
1227 | * |
||
1228 | * @param $width |
||
1229 | * @return $this |
||
1230 | */ |
||
1231 | public function setWidth($width) |
||
1237 | |||
1238 | /** |
||
1239 | * Get the height. |
||
1240 | * |
||
1241 | * @return int|null |
||
1242 | */ |
||
1243 | public function getHeight() |
||
1247 | |||
1248 | /** |
||
1249 | * Set the height. |
||
1250 | * |
||
1251 | * @param $height |
||
1252 | * @return $this |
||
1253 | */ |
||
1254 | public function setHeight($height) |
||
1260 | |||
1261 | /** |
||
1262 | * Guess the resize callback value |
||
1263 | * from a boolean. |
||
1264 | * |
||
1265 | * @param array $arguments |
||
1266 | */ |
||
1267 | protected function guessResizeArguments(array &$arguments) |
||
1282 | |||
1283 | /** |
||
1284 | * Set the public base directory. |
||
1285 | * |
||
1286 | * @param $directory |
||
1287 | * @return $this |
||
1288 | */ |
||
1289 | public function setDirectory($directory) |
||
1295 | |||
1296 | /** |
||
1297 | * Return the output. |
||
1298 | * |
||
1299 | * @return string |
||
1300 | */ |
||
1301 | public function __toString() |
||
1305 | |||
1306 | /** |
||
1307 | * If the method does not exist then |
||
1308 | * add an attribute and return. |
||
1309 | * |
||
1310 | * @param $name |
||
1311 | * @param $arguments |
||
1312 | * @return $this|mixed |
||
1313 | */ |
||
1314 | public function __call($name, $arguments) |
||
1332 | } |
||
1333 |
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.