| Conditions | 12 |
| Paths | 6 |
| Total Lines | 78 |
| Code Lines | 51 |
| 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 |
||
| 20 | public function php($data) |
||
| 21 | { |
||
| 22 | $valid = true; |
||
| 23 | $areaErrors = []; |
||
| 24 | $areaFieldNames = $this->getElementalAreaFieldNames($data['ClassName']); |
||
| 25 | foreach ($areaFieldNames as $areaFieldName) { |
||
| 26 | $elementsData = $data[$areaFieldName] ?? []; |
||
| 27 | if (empty($elementsData)) { |
||
| 28 | continue; |
||
| 29 | } |
||
| 30 | foreach (array_values($elementsData) as $elementData) { |
||
| 31 | $elementID = $this->getElementID($elementData); |
||
| 32 | if (!$elementID) { |
||
| 33 | continue; |
||
| 34 | } |
||
| 35 | $key = sprintf(EditFormFactory::FIELD_NAMESPACE_TEMPLATE, $elementID, 'ClassName'); |
||
| 36 | $className = $elementData[$key] ?? ''; |
||
| 37 | if (!$className) { |
||
| 38 | continue; |
||
| 39 | } |
||
| 40 | /** @var BaseElement $element */ |
||
| 41 | $element = DataObject::get_by_id($className, $elementID, false); |
||
|
|
|||
| 42 | if (!$element) { |
||
| 43 | continue; |
||
| 44 | } |
||
| 45 | $originalTitle = $element->Title ?? |
||
| 46 | sprintf('(Untitled %s)', ucfirst($element->config()->get('singular_name'))); |
||
| 47 | $formData = ElementalAreaController::removeNamespacesFromFields($elementData, $elementID); |
||
| 48 | $element->updateFromFormData($formData); |
||
| 49 | /** @var ValidationResult $validationResult */ |
||
| 50 | $validationResult = $element->validate(); |
||
| 51 | if ($validationResult->isValid()) { |
||
| 52 | continue; |
||
| 53 | } |
||
| 54 | if (!array_key_exists($areaFieldName, $areaErrors)) { |
||
| 55 | $areaErrors[$areaFieldName] = [ |
||
| 56 | 'The elements below have the following errors:' // TODO _t() |
||
| 57 | ]; |
||
| 58 | } |
||
| 59 | foreach ($validationResult->getMessages() as $message) { |
||
| 60 | $this->validationError( |
||
| 61 | "PageElements_{$elementID}_{$message['fieldName']}", |
||
| 62 | $message['message'], |
||
| 63 | $message['messageType'], |
||
| 64 | $message['messageCast'] |
||
| 65 | ); |
||
| 66 | $areaErrors[$areaFieldName][] = sprintf( |
||
| 67 | '%s - %s', |
||
| 68 | $originalTitle, |
||
| 69 | $message['message'] |
||
| 70 | ); |
||
| 71 | } |
||
| 72 | $valid = false; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | if (!$valid) { |
||
| 76 | foreach ($areaErrors as $areaFieldName => $errors) { |
||
| 77 | $this->validationError( |
||
| 78 | $areaFieldName, |
||
| 79 | implode('<br>', $errors), |
||
| 80 | ValidationResult::TYPE_ERROR, |
||
| 81 | ValidationResult::CAST_HTML |
||
| 82 | ); |
||
| 83 | } |
||
| 84 | // TODO: see what happens when you have multiple cms tabs |
||
| 85 | // Show a generic form message. Ideally this would be done in admin LeftAndMain.EditForm.js |
||
| 86 | // TODO: this is defined in en.js, needs to be in en.yml too (preferably admin, not elemental) |
||
| 87 | $msg = _t( |
||
| 88 | 'VALIDATION_ERRORS_ON_PAGE', |
||
| 89 | 'There are validation errors on this page, please fix them before saving or publishing.' |
||
| 90 | ); |
||
| 91 | // If message above is change to javascript, instead set a blank string here to hide the |
||
| 92 | // generic form message by overriding any PageElement_3_Title type of message which will |
||
| 93 | // show as a generic form message since it won't match dataFieldByName($field) in |
||
| 94 | // Form::loadMessageFrom($data) |
||
| 95 | $this->validationError('GenericFormMessage', $msg); |
||
| 96 | } |
||
| 97 | return $valid; |
||
| 98 | } |
||
| 133 |