| Conditions | 14 |
| Paths | 8 |
| Total Lines | 75 |
| Code Lines | 44 |
| Lines | 70 |
| Ratio | 93.33 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | |||
| 299 |