Conditions | 3 |
Paths | 3 |
Total Lines | 54 |
Code Lines | 37 |
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 |
||
112 | protected function _conversationReply(Event $event) |
||
113 | { |
||
114 | if (!is_integer($event->getData('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->getData('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->getData('sender_id') |
||
135 | ]) |
||
136 | ->first(); |
||
137 | |||
138 | //Check if this user hasn't already a notification. (Prevent for spam) |
||
139 | $hasReplied = $this->Notifications |
||
140 | ->find() |
||
141 | ->where([ |
||
142 | 'Notifications.foreign_key' => $conversation->id, |
||
143 | 'Notifications.type' => $event->getData('type'), |
||
144 | 'Notifications.user_id' => $event->getData('participant')->user->id |
||
145 | ]) |
||
146 | ->first(); |
||
147 | |||
148 | if (!is_null($hasReplied)) { |
||
149 | $hasReplied->data = serialize(['sender' => $sender, 'conversation' => $conversation]); |
||
150 | $hasReplied->is_read = 0; |
||
151 | |||
152 | $this->Notifications->save($hasReplied); |
||
153 | } else { |
||
154 | $data = []; |
||
155 | $data['user_id'] = $event->getData('participant')->user->id; |
||
156 | $data['type'] = $event->getData('type'); |
||
157 | $data['data'] = serialize(['sender' => $sender, 'conversation' => $conversation]); |
||
158 | $data['foreign_key'] = $conversation->id; |
||
159 | |||
160 | $entity = $this->Notifications->newEntity($data); |
||
161 | $this->Notifications->save($entity); |
||
162 | } |
||
163 | |||
164 | return true; |
||
165 | } |
||
166 | |||
244 |
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: