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