| Conditions | 3 |
| Paths | 3 |
| Total Lines | 55 |
| Code Lines | 37 |
| 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 |
||
| 112 | protected function _conversationReply(Event $event) |
||
| 113 | { |
||
| 114 | if (!is_integer($event->data['conversation_id'])) { |
||
| 115 | return false; |
||
| 116 | } |
||
| 117 | |||
| 118 | $this->Conversations = TableRegistry::get('Conversations'); |
||
| 119 | $this->Users = TableRegistry::get('Users'); |
||
| 120 | |||
| 121 | $conversation = $this->Conversations |
||
| 122 | ->find() |
||
| 123 | ->where([ |
||
| 124 | 'Conversations.id' => $event->data['conversation_id'] |
||
| 125 | ]) |
||
| 126 | ->select([ |
||
| 127 | 'id', 'user_id', 'title', 'last_message_id' |
||
| 128 | ]) |
||
| 129 | ->first(); |
||
| 130 | |||
| 131 | $sender = $this->Users |
||
| 132 | ->find('medium') |
||
| 133 | ->where([ |
||
| 134 | 'Users.id' => $event->data['sender_id'] |
||
| 135 | ]) |
||
| 136 | ->first(); |
||
| 137 | |||
| 138 | |||
| 139 | //Check if this user hasn't already a notification. (Prevent for spam) |
||
| 140 | $hasReplied = $this->Notifications |
||
| 141 | ->find() |
||
| 142 | ->where([ |
||
| 143 | 'Notifications.foreign_key' => $conversation->id, |
||
| 144 | 'Notifications.type' => $event->data['type'], |
||
| 145 | 'Notifications.user_id' => $event->data['participant']->user->id |
||
| 146 | ]) |
||
| 147 | ->first(); |
||
| 148 | |||
| 149 | if (!is_null($hasReplied)) { |
||
| 150 | $hasReplied->data = serialize(['sender' => $sender, 'conversation' => $conversation]); |
||
| 151 | $hasReplied->is_read = 0; |
||
| 152 | |||
| 153 | $this->Notifications->save($hasReplied); |
||
| 154 | } else { |
||
| 155 | $data = []; |
||
| 156 | $data['user_id'] = $event->data['participant']->user->id; |
||
| 157 | $data['type'] = $event->data['type']; |
||
| 158 | $data['data'] = serialize(['sender' => $sender, 'conversation' => $conversation]); |
||
| 159 | $data['foreign_key'] = $conversation->id; |
||
| 160 | |||
| 161 | $entity = $this->Notifications->newEntity($data); |
||
| 162 | $this->Notifications->save($entity); |
||
| 163 | } |
||
| 164 | |||
| 165 | return true; |
||
| 166 | } |
||
| 167 | |||
| 245 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: