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 |
||
| 29 | class ImageField extends Handler |
||
| 30 | { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * {@inheritDoc} |
||
| 34 | */ |
||
| 35 | View Code Duplication | public function info() |
|
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritDoc} |
||
| 49 | */ |
||
| 50 | View Code Duplication | public function render(Field $field, View $view) |
|
| 51 | { |
||
| 52 | if ($field->metadata->settings['multi'] === 'custom') { |
||
| 53 | $settings = $field->metadata->settings; |
||
| 54 | $settings['multi'] = $field->metadata->settings['multi_custom']; |
||
| 55 | $field->metadata->set('settings', $settings); |
||
| 56 | } |
||
| 57 | |||
| 58 | return $view->element('Field.ImageField/display', compact('field')); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritDoc} |
||
| 63 | */ |
||
| 64 | public function edit(Field $field, View $view) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritDoc} |
||
| 71 | */ |
||
| 72 | View Code Duplication | public function fieldAttached(Field $field) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritDoc} |
||
| 92 | */ |
||
| 93 | View Code Duplication | public function validate(Field $field, Validator $validator) |
|
| 94 | { |
||
| 95 | if ($field->metadata->required) { |
||
| 96 | $validator |
||
| 97 | ->add($field->name, 'isRequired', [ |
||
| 98 | 'rule' => function ($value, $context) use ($field) { |
||
| 99 | if (isset($context['data'][$field->name])) { |
||
| 100 | $count = 0; |
||
| 101 | foreach ($context['data'][$field->name] as $k => $file) { |
||
| 102 | if (is_integer($k)) { |
||
| 103 | $count++; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | return $count > 0; |
||
| 108 | } |
||
| 109 | |||
| 110 | return false; |
||
| 111 | }, |
||
| 112 | 'message' => __d('field', 'You must upload one image at least.') |
||
| 113 | ]); |
||
| 114 | } |
||
| 115 | |||
| 116 | if ($field->metadata->settings['multi'] !== 'custom') { |
||
| 117 | $maxFiles = intval($field->metadata->settings['multi']); |
||
| 118 | } else { |
||
| 119 | $maxFiles = intval($field->metadata->settings['multi_custom']); |
||
| 120 | } |
||
| 121 | |||
| 122 | $validator |
||
| 123 | ->add($field->name, 'numberOfFiles', [ |
||
| 124 | 'rule' => function ($value, $context) use ($field, $maxFiles) { |
||
| 125 | if (isset($context['data'][$field->name])) { |
||
| 126 | $count = 0; |
||
| 127 | foreach ($context['data'][$field->name] as $k => $file) { |
||
| 128 | if (is_integer($k)) { |
||
| 129 | $count++; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | return $count <= $maxFiles; |
||
| 134 | } |
||
| 135 | |||
| 136 | return false; |
||
| 137 | }, |
||
| 138 | 'message' => __d('field', 'You can upload {0} images as maximum.', $maxFiles) |
||
| 139 | ]); |
||
| 140 | |||
| 141 | if (!empty($field->metadata->settings['extensions'])) { |
||
| 142 | $extensions = $field->metadata->settings['extensions']; |
||
| 143 | $extensions = array_map('strtolower', array_map('trim', explode(',', $extensions))); |
||
| 144 | $validator |
||
| 145 | ->add($field->name, 'extensions', [ |
||
| 146 | 'rule' => function ($value, $context) use ($field, $extensions) { |
||
| 147 | if (isset($context['data'][$field->name])) { |
||
| 148 | foreach ($context['data'][$field->name] as $k => $file) { |
||
| 149 | if (is_integer($k)) { |
||
| 150 | $ext = strtolower(str_replace('.', '', strrchr($file['file_name'], '.'))); |
||
| 151 | if (!in_array($ext, $extensions)) { |
||
| 152 | return false; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | return true; |
||
| 158 | } |
||
| 159 | |||
| 160 | return false; |
||
| 161 | }, |
||
| 162 | 'message' => __d('field', 'Invalid image extension. Allowed extension are: {0}', $field->metadata->settings['extensions']) |
||
| 163 | ]); |
||
| 164 | } |
||
| 165 | |||
| 166 | return true; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritDoc} |
||
| 171 | * |
||
| 172 | * - extra: Holds a list (array) of files and their in formation (mime-icon, |
||
| 173 | * file name, etc). |
||
| 174 | * |
||
| 175 | * - value: Holds a text containing all file names separated by space. |
||
| 176 | */ |
||
| 177 | public function beforeSave(Field $field, $post) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * {@inheritDoc} |
||
| 236 | */ |
||
| 237 | public function beforeDelete(Field $field) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritDoc} |
||
| 248 | */ |
||
| 249 | public function settings(FieldInstance $instance, View $view) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritDoc} |
||
| 256 | */ |
||
| 257 | public function defaultSettings(FieldInstance $instance) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * {@inheritDoc} |
||
| 279 | */ |
||
| 280 | public function viewModeSettings(FieldInstance $instance, View $view, $viewMode) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritDoc} |
||
| 287 | */ |
||
| 288 | public function defaultViewModeSettings(FieldInstance $instance, $viewMode) |
||
| 298 | } |
||
| 299 |