Conditions | 21 |
Paths | 149 |
Total Lines | 81 |
Code Lines | 62 |
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 |
||
171 | public function search(): array |
||
172 | { |
||
173 | if (!$this->isActive()) { |
||
174 | return []; |
||
175 | } |
||
176 | $vatId = preg_replace('/[^0-9]/', '', $this->request->getByType('vatId', 'Text')); |
||
177 | $taxNumber = str_replace([' ', ',', '.', '-'], '', $this->request->getByType('taxNumber', 'Text')); |
||
178 | $ncr = str_replace([' ', ',', '.', '-'], '', $this->request->getByType('ncr', 'Text')); |
||
179 | $response = []; |
||
180 | $moduleName = $this->request->getModule(); |
||
181 | $client = \App\RecordCollectors\Helper\GusClient::getInstance($this->getClientParams($moduleName)); |
||
182 | try { |
||
183 | $infoFromGus = $client->search($vatId, $ncr, $taxNumber); |
||
184 | if ($recordId = $this->request->getInteger('record')) { |
||
185 | $recordModel = \Vtiger_Record_Model::getInstanceById($recordId, $moduleName); |
||
186 | $response['recordModel'] = $recordModel; |
||
187 | $fieldsModel = $recordModel->getModule()->getFields(); |
||
188 | } else { |
||
189 | $fieldsModel = \Vtiger_Module_Model::getInstance($moduleName)->getFields(); |
||
190 | } |
||
191 | if ($infoFromGus && isset($this->formFieldsToRecordMap[$moduleName])) { |
||
192 | $additional = $fieldsData = $skip = $dataCounter = []; |
||
193 | foreach ($infoFromGus as $key => &$row) { |
||
194 | $dataCounter[$key] = 0; |
||
195 | if (empty($row)) { |
||
196 | continue; |
||
197 | } |
||
198 | foreach ($this->formFieldsToRecordMap[$moduleName] as $apiKey => $fieldName) { |
||
199 | if (empty($fieldsModel[$fieldName]) || !$fieldsModel[$fieldName]->isActiveField()) { |
||
200 | if (isset($row[$apiKey]) && '' !== $row[$apiKey]) { |
||
201 | $skip[$fieldName]['data'][$key] = $row[$apiKey]; |
||
202 | if (isset($fieldsModel[$fieldName]) && empty($skip[$fieldName]['label'])) { |
||
203 | $skip[$fieldName]['label'] = \App\Language::translate($fieldsModel[$fieldName]->getFieldLabel(), $moduleName); |
||
204 | } else { |
||
205 | $skip[$fieldName]['label'] = $fieldName; |
||
206 | } |
||
207 | } |
||
208 | unset($row[$apiKey]); |
||
209 | continue; |
||
210 | } |
||
211 | $value = ''; |
||
212 | if (isset($row[$apiKey])) { |
||
213 | $value = trim($row[$apiKey]); |
||
214 | unset($row[$apiKey]); |
||
215 | } |
||
216 | $fieldModel = $fieldsModel[$fieldName]; |
||
217 | if ($value) { |
||
218 | ++$dataCounter[$key]; |
||
219 | if ('phone' === $fieldModel->getFieldDataType()) { |
||
220 | $details = $fieldModel->getUITypeModel()->getPhoneDetails($value, 'PL'); |
||
|
|||
221 | $value = $details['number']; |
||
222 | if ($fieldName !== $details['fieldName']) { |
||
223 | $fieldName = $details['fieldName']; |
||
224 | $fieldModel = $fieldsModel[$fieldName]; |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | $fieldsData[$fieldName]['label'] = \App\Language::translate($fieldModel->getFieldLabel(), $moduleName); |
||
229 | $fieldsData[$fieldName]['data'][$key] = [ |
||
230 | 'raw' => $value, |
||
231 | 'edit' => $fieldModel->getEditViewDisplayValue($value), |
||
232 | 'display' => $fieldModel->getDisplayValue($value), |
||
233 | ]; |
||
234 | } |
||
235 | foreach ($row as $name => $value) { |
||
236 | if ('' !== $value) { |
||
237 | $additional[$name][$key] = $value; |
||
238 | } |
||
239 | } |
||
240 | } |
||
241 | $response['fields'] = $fieldsData; |
||
242 | $response['additional'] = $additional; |
||
243 | $response['keys'] = array_keys($infoFromGus); |
||
244 | $response['skip'] = $skip; |
||
245 | $response['dataCounter'] = $dataCounter; |
||
246 | } |
||
247 | } catch (\SoapFault $e) { |
||
248 | \App\Log::warning($e->faultstring, 'RecordCollectors'); |
||
249 | $response['error'] = $e->faultstring; |
||
250 | } |
||
251 | return $response; |
||
252 | } |
||
270 |