| Conditions | 9 |
| Paths | 80 |
| Total Lines | 67 |
| Code Lines | 40 |
| 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 |
||
| 161 | public function doAddToList($data, /** @scrutinizer ignore-unused */ $form) |
||
| 162 | { |
||
| 163 | $className = $this->gridField->list->dataClass; |
||
| 164 | $controller = $this->getToplevelController(); |
||
| 165 | $form = $controller->EditForm(); |
||
| 166 | $return = array(); |
||
| 167 | |||
| 168 | if (isset($data['RecordIDs'])) { |
||
| 169 | $ids = explode(",", $data['RecordIDs']); |
||
| 170 | } else { |
||
| 171 | $ids = array(); |
||
| 172 | } |
||
| 173 | |||
| 174 | $list_id = (isset($data['ContactListID'])) ? $data['ContactListID'] : 0; |
||
| 175 | $list = ContactList::get()->byID($list_id); |
||
| 176 | |||
| 177 | try { |
||
| 178 | foreach ($ids as $record_id) { |
||
| 179 | if ($list_id) { |
||
| 180 | $record = DataObject::get_by_id($className, $record_id); |
||
| 181 | |||
| 182 | if ($record->hasMethod("Lists")) { |
||
| 183 | $list->Contacts()->add($record); |
||
| 184 | $list->write(); |
||
| 185 | } |
||
| 186 | |||
| 187 | $return[] = $record->ID; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | } catch (\Exception $e) { |
||
| 191 | $form->sessionMessage( |
||
| 192 | $e->getMessage(), |
||
| 193 | ValidationResult::TYPE_ERROR |
||
| 194 | ); |
||
| 195 | |||
| 196 | $responseNegotiator = new PjaxResponseNegotiator(array( |
||
| 197 | 'CurrentForm' => function () use (&$form) { |
||
| 198 | return $form->forTemplate(); |
||
| 199 | }, |
||
| 200 | 'default' => function () use (&$controller) { |
||
| 201 | return $controller->redirectBack(); |
||
| 202 | } |
||
| 203 | )); |
||
| 204 | |||
| 205 | if ($controller->getRequest()->isAjax()) { |
||
| 206 | $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); |
||
| 207 | } |
||
| 208 | |||
| 209 | return $responseNegotiator->respond($controller->getRequest()); |
||
| 210 | } |
||
| 211 | |||
| 212 | if (!empty($list)) { |
||
| 213 | $message = "Added " . count($return) . " contacts to mailing list '{$list->Title}'"; |
||
| 214 | } else { |
||
| 215 | $message = _t("Contacts.NoListSelected", "No list selected"); |
||
| 216 | } |
||
| 217 | |||
| 218 | $form->sessionMessage( |
||
| 219 | $message, |
||
| 220 | ValidationResult::TYPE_GOOD |
||
| 221 | ); |
||
| 222 | |||
| 223 | // Changes to the record properties might've excluded the record from |
||
| 224 | // a filtered list, so return back to the main view if it can't be found |
||
| 225 | $link = $controller->Link(); |
||
| 226 | $controller->getRequest()->addHeader('X-Pjax', 'Content'); |
||
| 227 | return $controller->redirect($link); |
||
| 228 | } |
||
| 230 |