| Conditions | 21 |
| Paths | 129 |
| Total Lines | 62 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 189 | public function loadData(): void |
||
| 190 | { |
||
| 191 | if (empty($this->data)) { |
||
| 192 | return; |
||
| 193 | } |
||
| 194 | if ($recordId = $this->request->getInteger('record')) { |
||
| 195 | $recordModel = \Vtiger_Record_Model::getInstanceById($recordId, $this->moduleName); |
||
| 196 | $this->response['recordModel'] = $recordModel; |
||
| 197 | $fieldsModel = $recordModel->getModule()->getFields(); |
||
| 198 | } else { |
||
| 199 | $fieldsModel = \Vtiger_Module_Model::getInstance($this->moduleName)->getFields(); |
||
| 200 | } |
||
| 201 | $additional = $fieldsData = $skip = []; |
||
| 202 | $rows = isset($this->data[0]) ? $this->data : [$this->data]; |
||
| 203 | foreach ($rows as $key => &$row) { |
||
| 204 | $dataCounter[$key] = 0; |
||
| 205 | if (empty($row)) { |
||
| 206 | continue; |
||
| 207 | } |
||
| 208 | foreach ($this->formFieldsToRecordMap[$this->moduleName] as $apiKey => $fieldName) { |
||
| 209 | if (empty($fieldsModel[$fieldName]) || !$fieldsModel[$fieldName]->isActiveField()) { |
||
| 210 | if (isset($row[$apiKey]) && '' !== $row[$apiKey] && null !== $row[$apiKey]) { |
||
| 211 | $skip[$fieldName]['data'][$key] = $row[$apiKey]; |
||
| 212 | if (isset($fieldsModel[$fieldName]) && empty($skip[$fieldName]['label'])) { |
||
| 213 | $skip[$fieldName]['label'] = \App\Language::translate($fieldsModel[$fieldName]->getFieldLabel(), $this->moduleName); |
||
| 214 | } else { |
||
| 215 | $skip[$fieldName]['label'] = $fieldName; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | unset($row[$apiKey]); |
||
| 219 | continue; |
||
| 220 | } |
||
| 221 | $value = ''; |
||
| 222 | if (isset($row[$apiKey])) { |
||
| 223 | $value = trim($row[$apiKey]); |
||
| 224 | unset($row[$apiKey]); |
||
| 225 | } |
||
| 226 | if ('' === $value && isset($fieldsData[$fieldName]['data'][$key])) { |
||
| 227 | continue; |
||
| 228 | } |
||
| 229 | if ($value) { |
||
| 230 | ++$dataCounter[$key]; |
||
|
|
|||
| 231 | } |
||
| 232 | $fieldModel = $fieldsModel[$fieldName]; |
||
| 233 | $fieldsData[$fieldName]['label'] = \App\Language::translate($fieldModel->getFieldLabel(), $this->moduleName); |
||
| 234 | $fieldsData[$fieldName]['data'][$key] = [ |
||
| 235 | 'raw' => $value, |
||
| 236 | 'edit' => $fieldModel->getEditViewDisplayValue($value), |
||
| 237 | 'display' => $fieldModel->getDisplayValue($value, false, false, false, 40), |
||
| 238 | ]; |
||
| 239 | } |
||
| 240 | foreach ($row as $name => $value) { |
||
| 241 | if ('' !== $value && null !== $value) { |
||
| 242 | $additional[$name][$key] = $value; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | $this->response['fields'] = $fieldsData; |
||
| 247 | $this->response['skip'] = $skip; |
||
| 248 | $this->response['keys'] = array_keys($rows); |
||
| 249 | $this->response['additional'] = $additional; |
||
| 250 | $this->response['dataCounter'] = $dataCounter; |
||
| 251 | } |
||
| 285 |