| Conditions | 14 |
| Paths | 8 |
| Total Lines | 78 |
| 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 |
||
| 16 | public function onAfterWrite() |
||
| 17 | { |
||
| 18 | /** @var DataObject */ |
||
| 19 | $owner = $this->getOwner(); |
||
| 20 | $changed_fields = $owner->getChangedFields(true, DataObject::CHANGE_VALUE); |
||
| 21 | $new_values = []; |
||
| 22 | |||
| 23 | foreach ($changed_fields as $key => $value) { |
||
| 24 | $new_values[$key] = $value['after']; |
||
| 25 | } |
||
| 26 | |||
| 27 | $notifications = Notification::get() |
||
| 28 | ->filter('BaseClassName', $owner->ClassName); |
||
| 29 | |||
| 30 | // if created, shortcut below logic |
||
| 31 | if (in_array('ID', array_keys($changed_fields))) { |
||
| 32 | Notifier::processNotifications( |
||
| 33 | $notifications->filter(Notification::STATE_CREATED, true), |
||
| 34 | $owner |
||
| 35 | ); |
||
| 36 | return; |
||
| 37 | } |
||
| 38 | |||
| 39 | // If no fields were changed, then no need to continue |
||
| 40 | if (count(array_keys($changed_fields)) == 0) { |
||
| 41 | return; |
||
| 42 | } |
||
| 43 | |||
| 44 | // Get a list of all relevent rules |
||
| 45 | $rules = NotificationRule::get()->filter([ |
||
| 46 | 'Notification.BaseClassName' => $owner->ClassName, |
||
| 47 | 'FieldName' => array_keys($changed_fields), |
||
| 48 | 'Value:not' => null |
||
| 49 | ]); |
||
| 50 | |||
| 51 | $rule_ids = $rules->filterByCallback( |
||
| 52 | function($item, $list) use ($new_values) { |
||
|
|
|||
| 53 | if (in_array($item->FieldName, array_keys($new_values)) |
||
| 54 | && $new_values[$item->FieldName] == $item->Value) { |
||
| 55 | return true; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | )->column('ID'); |
||
| 59 | |||
| 60 | if (count($rule_ids) > 0) { |
||
| 61 | $notifications = $notifications->filter('Rules.ID', $rule_ids); |
||
| 62 | } |
||
| 63 | |||
| 64 | $notifications = $notifications->filterByCallback( |
||
| 65 | function($item, $list) use ($changed_fields) { |
||
| 66 | /** |
||
| 67 | * @var Notification $item |
||
| 68 | * @var DataList $list |
||
| 69 | */ |
||
| 70 | if ($item->dbObject(Notification::STATE_UPDATED)->getValue()) { |
||
| 71 | return true; |
||
| 72 | } |
||
| 73 | |||
| 74 | foreach ($item->Rules() as $rule) { |
||
| 75 | /** @var NotificationRule $rule */ |
||
| 76 | if (in_array($rule->FieldName, array_keys($changed_fields)) |
||
| 77 | && $rule->WasChanged == true) { |
||
| 78 | return true; |
||
| 79 | } |
||
| 80 | |||
| 81 | if (in_array($rule->FieldName, array_keys($changed_fields)) |
||
| 82 | && isset($changed_fields[$rule->FieldName]['after']) |
||
| 83 | && $changed_fields[$rule->FieldName]['after'] == $rule->Value |
||
| 84 | ) { |
||
| 85 | return true; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | return false; |
||
| 90 | } |
||
| 91 | ); |
||
| 92 | |||
| 93 | Notifier::processNotifications($notifications, $owner); |
||
| 94 | } |
||
| 115 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.