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 |
||
| 8 | class Image |
||
| 9 | { |
||
| 10 | private $data; |
||
| 11 | |||
| 12 | public function __construct($data) |
||
| 16 | |||
| 17 | public function exist(): bool |
||
| 21 | |||
| 22 | public function hasCropped(): bool |
||
| 26 | |||
| 27 | public function croppedOrOriginalSourceUrl($default = null) |
||
| 28 | { |
||
| 29 | $source = Arr::get($this->data, 'sources.cropped') ?: $this->originalSource(); |
||
| 30 | if (!$source) { |
||
| 31 | return $default; |
||
| 32 | } |
||
| 33 | |||
| 34 | if ($this->isEncoded()) { |
||
| 35 | return $source; |
||
| 36 | } |
||
| 37 | |||
| 38 | return Storage::disk($this->getDisk())->url($source); |
||
| 39 | } |
||
| 40 | |||
| 41 | View Code Duplication | public function originalSourceUrl($default = null) |
|
| 51 | |||
| 52 | public function originalSource($default = null) |
||
| 53 | { |
||
| 54 | return Arr::get($this->data, 'sources.original') ?: $default; |
||
| 55 | } |
||
| 56 | |||
| 57 | View Code Duplication | public function croppedSourceUrl($default = null) |
|
| 67 | |||
| 68 | public function croppedSource($default = null) |
||
| 69 | { |
||
| 70 | return Arr::get($this->data, 'sources.cropped') ?: $default; |
||
| 71 | } |
||
| 72 | |||
| 73 | public function isEncoded(): bool |
||
| 83 | |||
| 84 | public function getDisk() |
||
| 88 | |||
| 89 | public function cropWidth() |
||
| 90 | { |
||
| 91 | return Arr::get($this->data, 'crop.width'); |
||
| 93 | |||
| 94 | public function cropHeight() |
||
| 98 | |||
| 99 | public function cropX() |
||
| 103 | |||
| 104 | public function cropY() |
||
| 108 | |||
| 109 | public function cropRotate() |
||
| 113 | |||
| 114 | public function cropRotateBackground() |
||
| 118 | } |
||
| 119 |