| Conditions | 15 |
| Paths | 72 |
| Total Lines | 54 |
| Code Lines | 41 |
| 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 |
||
| 172 | public function loadData(): void |
||
| 173 | { |
||
| 174 | if ($recordId = $this->request->getInteger('record')) { |
||
| 175 | $recordModel = \Vtiger_Record_Model::getInstanceById($recordId, $this->moduleName); |
||
| 176 | $this->response['recordModel'] = $recordModel; |
||
| 177 | $fieldsModel = $recordModel->getModule()->getFields(); |
||
| 178 | } else { |
||
| 179 | $fieldsModel = \Vtiger_Module_Model::getInstance($this->moduleName)->getFields(); |
||
| 180 | } |
||
| 181 | $fieldsData = $skip = []; |
||
| 182 | $rows = isset($this->data[0]) ? $this->data : [$this->data]; |
||
| 183 | foreach ($rows as $key => &$row) { |
||
| 184 | $dataCounter[$key] = 0; |
||
| 185 | if (empty($row)) { |
||
| 186 | continue; |
||
| 187 | } |
||
| 188 | foreach ($this->formFieldsToRecordMap[$this->moduleName] as $apiKey => $fieldName) { |
||
| 189 | if (empty($fieldsModel[$fieldName]) || !$fieldsModel[$fieldName]->isActiveField()) { |
||
| 190 | if (isset($row[$apiKey]) && '' !== $row[$apiKey]) { |
||
| 191 | $skip[$fieldName]['data'][$key] = $row[$apiKey]; |
||
| 192 | if (isset($fieldsModel[$fieldName]) && empty($skip[$fieldName]['label'])) { |
||
| 193 | $skip[$fieldName]['label'] = \App\Language::translate($fieldsModel[$fieldName]->getFieldLabel(), $this->moduleName); |
||
| 194 | } else { |
||
| 195 | $skip[$fieldName]['label'] = $fieldName; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | unset($row[$apiKey]); |
||
| 199 | continue; |
||
| 200 | } |
||
| 201 | $value = ''; |
||
| 202 | if (isset($row[$apiKey])) { |
||
| 203 | $value = trim($row[$apiKey]); |
||
| 204 | unset($row[$apiKey]); |
||
| 205 | } |
||
| 206 | if ($value) { |
||
| 207 | ++$dataCounter[$key]; |
||
| 208 | } |
||
| 209 | $fieldModel = $fieldsModel[$fieldName]; |
||
| 210 | $fieldsData[$fieldName]['label'] = \App\Language::translate($fieldModel->getFieldLabel(), $this->moduleName); |
||
| 211 | $fieldsData[$fieldName]['data'][$key] = [ |
||
| 212 | 'raw' => $value, |
||
| 213 | 'edit' => $fieldModel->getEditViewDisplayValue($value), |
||
| 214 | 'display' => $fieldModel->getDisplayValue($value), |
||
| 215 | ]; |
||
| 216 | } |
||
| 217 | foreach ($row as $name => $value) { |
||
| 218 | $additional[$name][$key] = $value; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | $this->response['fields'] = $fieldsData; |
||
| 222 | $this->response['skip'] = $skip; |
||
| 223 | $this->response['keys'] = array_keys($rows); |
||
| 224 | $this->response['additional'] = $additional; |
||
| 225 | $this->response['dataCounter'] = $dataCounter; |
||
| 226 | } |
||
| 228 |