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