| Conditions | 14 |
| Paths | 24 |
| Total Lines | 59 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 32 | public function editViewPreSave(App\EventHandler $eventHandler, array $handler) |
||
| 33 | { |
||
| 34 | $recordModel = $eventHandler->getRecordModel(); |
||
| 35 | $response = ['result' => true]; |
||
| 36 | $values = []; |
||
| 37 | foreach ($recordModel->getModule()->getFieldsByType('email', true) as $fieldModel) { |
||
| 38 | if (($value = $recordModel->get($fieldModel->getName())) && $fieldModel->isViewable()) { |
||
| 39 | $values[] = $value; |
||
| 40 | } |
||
| 41 | } |
||
| 42 | if ($values) { |
||
| 43 | $modules = explode(',', $handler['include_modules']); |
||
| 44 | foreach ($modules as $moduleName) { |
||
| 45 | $queryGenerator = new \App\QueryGenerator($moduleName); |
||
| 46 | $queryGenerator->setFields(array_merge(['id'], self::FIELDS_DETAILS[$moduleName] ?? []))->permissions = false; |
||
| 47 | if ($moduleName === $recordModel->getModuleName() && $recordModel->getId()) { |
||
| 48 | $queryGenerator->addCondition('id', $recordModel->getId(), 'n'); |
||
| 49 | } |
||
| 50 | $fields = []; |
||
| 51 | foreach ($queryGenerator->getModuleModel()->getFieldsByType('email', true) as $fieldName => $fieldModel) { |
||
| 52 | $queryGenerator->addCondition($fieldName, $values, 'e', false); |
||
| 53 | $fields[$fieldName] = $fieldModel; |
||
| 54 | } |
||
| 55 | if ($row = $queryGenerator->createQuery()->one()) { |
||
| 56 | $label = ''; |
||
| 57 | foreach (self::FIELDS_DETAILS[$recordModel->getModuleName()] ?? [] as $fieldName) { |
||
| 58 | $fieldModel = $recordModel->getModule()->getFieldByName($fieldName); |
||
| 59 | if ('' !== $recordModel->get($fieldName) && $fieldModel->isViewable()) { |
||
| 60 | $label .= '<br>' . $fieldModel->getFullLabelTranslation() . ': ' . $recordModel->getDisplayValue($fieldName); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | if (self::ALLOW_SAVE) { |
||
| 64 | $response = [ |
||
| 65 | 'result' => false, |
||
| 66 | 'type' => 'confirm', |
||
| 67 | 'hash' => hash('sha256', implode('|', $recordModel->getData())), |
||
| 68 | 'message' => App\Language::translateArgs( |
||
| 69 | 'LBL_DUPLICATE_EMAIL_ADDRESS', |
||
| 70 | $moduleName, |
||
| 71 | \App\Language::translate($moduleName, $moduleName) |
||
| 72 | ) . '<br>' . |
||
| 73 | \App\Record::getHtmlLink($row['id'], $moduleName) . $label |
||
| 74 | ]; |
||
| 75 | } else { |
||
| 76 | $response = [ |
||
| 77 | 'result' => false, |
||
| 78 | 'message' => App\Language::translateArgs( |
||
| 79 | 'LBL_DUPLICATE_EMAIL_ADDRESS', |
||
| 80 | $moduleName, |
||
| 81 | \App\Language::translate($moduleName, $moduleName), |
||
| 82 | ) . '<br>' . |
||
| 83 | \App\Record::getHtmlLink($row['id'], $moduleName) . $label |
||
| 84 | ]; |
||
| 85 | } |
||
| 86 | break; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 | return $response; |
||
| 91 | } |
||
| 93 |