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 ContentsFileTrait 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 ContentsFileTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | trait ContentsFileTrait |
||
| 14 | { |
||
| 15 | private $contentsFileSettings = []; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * contentsFileSettings |
||
| 19 | * 設定値のセッティング |
||
| 20 | * |
||
| 21 | * @author hagiwara |
||
| 22 | */ |
||
| 23 | private function contentsFileSettings() |
||
| 30 | |||
| 31 | /** |
||
| 32 | * getContentsFileSettings |
||
| 33 | * 設定値のセッティングの取得 |
||
| 34 | * |
||
| 35 | * @author hagiwara |
||
| 36 | */ |
||
| 37 | public function getContentsFileSettings() |
||
| 44 | |||
| 45 | /** |
||
| 46 | * getContentsFile |
||
| 47 | * ファイルのgetterのセッティング |
||
| 48 | * |
||
| 49 | * @author hagiwara |
||
| 50 | * @param string $property |
||
| 51 | * @param array $value |
||
| 52 | */ |
||
| 53 | public function getContentsFile($property, $value) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * setContentsFile |
||
| 94 | * ファイルのsetterのセッティング |
||
| 95 | * |
||
| 96 | * @author hagiwara |
||
| 97 | */ |
||
| 98 | public function setContentsFile() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * normalSetContentsFile |
||
| 114 | * ファイルのsetterのセッティング |
||
| 115 | * |
||
| 116 | * @author hagiwara |
||
| 117 | */ |
||
| 118 | private function normalSetContentsFile($field, $fieldSetting) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * ddSetContentsFile |
||
| 163 | * ファイルのsetterのセッティング |
||
| 164 | * |
||
| 165 | * @author hagiwara |
||
| 166 | */ |
||
| 167 | private function ddSetContentsFile($field, $fieldSetting) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * getExt |
||
| 220 | * 拡張子の取得 |
||
| 221 | * |
||
| 222 | * @author hagiwara |
||
| 223 | * @param string $file |
||
| 224 | */ |
||
| 225 | private function getExt($file) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * tmpUpload |
||
| 237 | * tmpディレクトリへのアップロード |
||
| 238 | * |
||
| 239 | * @author hagiwara |
||
| 240 | * @param string $tmpFileName |
||
| 241 | * @param array $fieldSetting |
||
| 242 | * @param string $tmpFileName |
||
| 243 | */ |
||
| 244 | private function tmpUpload($tmpName, $fieldSetting, $tmpFileName) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * orientationFixedImage |
||
| 264 | * http://www.glic.co.jp/blog/archives/88 よりコピペ |
||
| 265 | * 画像の方向を正す |
||
| 266 | * 向きだけロジックが逆そうなので調整 |
||
| 267 | * |
||
| 268 | * @author hagiwara |
||
| 269 | */ |
||
| 270 | private function orientationFixedImage($input, $output){ |
||
| 271 | $imagetype = exif_imagetype($input); |
||
| 272 | // 何も取れない場合何もしない |
||
| 273 | if ($imagetype === false) { |
||
| 274 | return; |
||
| 275 | } |
||
| 276 | // exif情報の取得 |
||
| 277 | $exif_datas = @exif_read_data($input); |
||
| 278 | // 画像読み込み |
||
| 279 | View Code Duplication | switch ($imagetype) { |
|
| 280 | case IMAGETYPE_GIF: |
||
| 281 | $image = ImageCreateFromGIF($input); |
||
| 282 | break; |
||
| 283 | case IMAGETYPE_JPEG: |
||
| 284 | $image = ImageCreateFromJPEG($input); |
||
| 285 | break; |
||
| 286 | case IMAGETYPE_PNG: |
||
| 287 | $image = ImageCreateFromPNG($input); |
||
| 288 | break; |
||
| 289 | default: |
||
| 290 | $image = false; |
||
| 291 | } |
||
| 292 | |||
| 293 | // 画像以外は何もしない |
||
| 294 | if (!$image) { |
||
| 295 | return; |
||
| 296 | } |
||
| 297 | |||
| 298 | // 向き補正 |
||
| 299 | if(isset($exif_datas['Orientation'])){ |
||
| 300 | $orientation = $exif_datas['Orientation']; |
||
| 301 | if($image){ |
||
| 302 | // 未定義 |
||
| 303 | if($orientation == 0) { |
||
| 304 | // 通常 |
||
| 305 | }else if($orientation == 1) { |
||
| 306 | // 左右反転 |
||
| 307 | }else if($orientation == 2) { |
||
| 308 | $image = $this->imageFlop($image); |
||
| 309 | // 180°回転 |
||
| 310 | }else if($orientation == 3) { |
||
| 311 | $image = $this->imageRotate($image,180, 0); |
||
| 312 | // 上下反転 |
||
| 313 | }else if($orientation == 4) { |
||
| 314 | $image = $this->imageFlip($image); |
||
| 315 | // 反時計回りに90°回転 上下反転 |
||
| 316 | }else if($orientation == 5) { |
||
| 317 | $image = $this->imageRotate($image,90, 0); |
||
| 318 | $image = $this->imageFlip($image); |
||
| 319 | // 時計回りに90°回転 |
||
| 320 | }else if($orientation == 6) { |
||
| 321 | $image = $this->imageRotate($image,-90, 0); |
||
| 322 | // 時計回りに90°回転 上下反転 |
||
| 323 | }else if($orientation == 7) { |
||
| 324 | $image = $this->imageRotate($image,-90, 0); |
||
| 325 | $image = $this->imageFlip($image); |
||
| 326 | // 反時計回りに90°回転 |
||
| 327 | }else if($orientation == 8) { |
||
| 328 | $image = $this->imageRotate($image,90, 0); |
||
| 329 | } |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | switch ($imagetype) { |
||
| 334 | case IMAGETYPE_GIF: |
||
| 335 | ImageGIF($image ,$output); |
||
| 336 | break; |
||
| 337 | case IMAGETYPE_JPEG: |
||
| 338 | ImageJPEG($image ,$output, 100); |
||
| 339 | break; |
||
| 340 | case IMAGETYPE_PNG: |
||
| 341 | ImagePNG($image ,$output); |
||
| 342 | break; |
||
| 343 | default: |
||
| 344 | return; |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * imageFlop |
||
| 350 | * http://www.glic.co.jp/blog/archives/88 よりコピペ |
||
| 351 | * 画像の左右反転 |
||
| 352 | * |
||
| 353 | * @author hagiwara |
||
| 354 | */ |
||
| 355 | View Code Duplication | private function imageFlop($image) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * imageFlip |
||
| 376 | * http://www.glic.co.jp/blog/archives/88 よりコピペ |
||
| 377 | * 上下反転 |
||
| 378 | * @param resource $image |
||
| 379 | * |
||
| 380 | * @author hagiwara |
||
| 381 | */ |
||
| 382 | View Code Duplication | private function imageFlip($image) |
|
| 400 | |||
| 401 | |||
| 402 | /** |
||
| 403 | * imageRotate |
||
| 404 | * http://www.glic.co.jp/blog/archives/88 よりコピペ |
||
| 405 | * 画像を回転 |
||
| 406 | * @param integer $angle |
||
| 407 | * @param integer $bgd_color |
||
| 408 | * |
||
| 409 | * @author hagiwara |
||
| 410 | */ |
||
| 411 | private function imageRotate($image, $angle, $bgd_color) |
||
| 415 | } |
||
| 416 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: