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 ContentsFileHelper extends Helper { |
||
| 9 | |||
| 10 | public $helpers = ['Html', 'Url', 'Form']; |
||
| 11 | private $defaultOption = [ |
||
| 12 | 'target' => '_blank', |
||
| 13 | 'escape' => false, |
||
| 14 | 'download' => false |
||
| 15 | ]; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * link |
||
| 19 | * @author hagiwara |
||
| 20 | * @param array $fileInfo |
||
| 21 | * @param array $options |
||
| 22 | * @param string $title |
||
| 23 | */ |
||
| 24 | public function link($fileInfo, $options = [], $title = null) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * image |
||
| 51 | * @author hagiwara |
||
| 52 | * @param array $fileInfo |
||
| 53 | * @param array $options |
||
| 54 | */ |
||
| 55 | public function image($fileInfo, $options = []) |
||
| 56 | { |
||
| 57 | if (empty($fileInfo)) { |
||
| 58 | return ''; |
||
| 59 | } |
||
| 60 | $options = array_merge( |
||
| 61 | $this->defaultOption, |
||
| 62 | $options |
||
| 63 | ); |
||
| 64 | if (!empty($fileInfo)) { |
||
| 65 | View Code Duplication | if (isset($options['resize'])) { |
|
| 66 | $fileInfo['resize'] = $options['resize']; |
||
| 67 | unset($options['resize']); |
||
| 68 | } |
||
| 69 | return $this->Html->image($this->urlArray($fileInfo, $options), $options); |
||
| 70 | } |
||
| 71 | return ''; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * url |
||
| 76 | * @author hagiwara |
||
| 77 | * @param array $fileInfo |
||
| 78 | * @param boolean $full |
||
| 79 | * @param array $options |
||
| 80 | */ |
||
| 81 | public function url($fileInfo, $full = false, $options = []) |
||
| 82 | { |
||
| 83 | if (empty($fileInfo)) { |
||
| 84 | return []; |
||
| 85 | } |
||
| 86 | if (!isset($fileInfo['resize'])) { |
||
| 87 | $fileInfo['resize'] = false; |
||
| 88 | } |
||
| 89 | $options = array_merge( |
||
| 90 | $this->defaultOption, |
||
| 91 | $options |
||
| 92 | ); |
||
| 93 | return $this->Url->build($this->urlArray($fileInfo, $options), $full); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * contentsFileHidden |
||
| 98 | * |
||
| 99 | * バリデーションに引っかかった際にファイルをそのまま送る用 |
||
| 100 | * |
||
| 101 | * @author hagiwara |
||
| 102 | * @param array|null $contentFileData |
||
| 103 | * @param text $field |
||
| 104 | */ |
||
| 105 | public function contentsFileHidden($contentFileData, $field) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * urlArray |
||
| 118 | * @author hagiwara |
||
| 119 | * @param array $fileInfo |
||
| 120 | */ |
||
| 121 | private function urlArray($fileInfo, $options) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * makeStaticS3Url |
||
| 168 | * 静的ホスティング用のURL作成 |
||
| 169 | * @author hagiwara |
||
| 170 | * @param array $fileInfo |
||
| 171 | */ |
||
| 172 | private function makeStaticS3Url($fileInfo) |
||
| 206 | } |
||
| 207 |
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.