Conditions | 25 |
Paths | 352 |
Total Lines | 188 |
Code Lines | 112 |
Lines | 60 |
Ratio | 31.91 % |
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 |
||
63 | public function execute() |
||
64 | { |
||
65 | $message = $this->getMessage(); |
||
66 | |||
67 | $chat = $message->getChat(); |
||
68 | $user = $message->getFrom(); |
||
69 | $text = trim($message->getText(true)); |
||
70 | $chat_id = $chat->getId(); |
||
71 | $user_id = $user->getId(); |
||
72 | |||
73 | //Preparing Response |
||
74 | $data = [ |
||
75 | 'chat_id' => $chat_id, |
||
76 | ]; |
||
77 | |||
78 | if ($chat->isGroupChat() || $chat->isSuperGroup()) { |
||
79 | //reply to message id is applied by default |
||
80 | //Force reply is applied by default so it can work with privacy on |
||
81 | $data['reply_markup'] = Keyboard::forceReply(['selective' => true]); |
||
82 | } |
||
83 | |||
84 | //Conversation start |
||
85 | $this->conversation = new Conversation($user_id, $chat_id, $this->getName()); |
||
86 | |||
87 | $notes = &$this->conversation->notes; |
||
88 | !is_array($notes) && $notes = []; |
||
89 | |||
90 | //cache data from the tracking session if any |
||
91 | $state = 0; |
||
92 | if (isset($notes['state'])) { |
||
93 | $state = $notes['state']; |
||
94 | } |
||
95 | |||
96 | $result = Request::emptyResponse(); |
||
97 | |||
98 | //State machine |
||
99 | //Entrypoint of the machine state if given by the track |
||
100 | //Every time a step is achieved the track is updated |
||
101 | switch ($state) { |
||
102 | case 0: |
||
103 | View Code Duplication | if ($text === '') { |
|
|
|||
104 | $notes['state'] = 0; |
||
105 | $this->conversation->update(); |
||
106 | |||
107 | $data['text'] = 'Type your name:'; |
||
108 | $data['reply_markup'] = Keyboard::remove(['selective' => true]); |
||
109 | |||
110 | $result = Request::sendMessage($data); |
||
111 | break; |
||
112 | } |
||
113 | |||
114 | $notes['name'] = $text; |
||
115 | $text = ''; |
||
116 | |||
117 | // no break |
||
118 | case 1: |
||
119 | View Code Duplication | if ($text === '') { |
|
120 | $notes['state'] = 1; |
||
121 | $this->conversation->update(); |
||
122 | |||
123 | $data['text'] = 'Type your surname:'; |
||
124 | |||
125 | $result = Request::sendMessage($data); |
||
126 | break; |
||
127 | } |
||
128 | |||
129 | $notes['surname'] = $text; |
||
130 | $text = ''; |
||
131 | |||
132 | // no break |
||
133 | case 2: |
||
134 | if ($text === '' || !is_numeric($text)) { |
||
135 | $notes['state'] = 2; |
||
136 | $this->conversation->update(); |
||
137 | |||
138 | $data['text'] = 'Type your age:'; |
||
139 | if ($text !== '') { |
||
140 | $data['text'] = 'Type your age, must be a number:'; |
||
141 | } |
||
142 | |||
143 | $result = Request::sendMessage($data); |
||
144 | break; |
||
145 | } |
||
146 | |||
147 | $notes['age'] = $text; |
||
148 | $text = ''; |
||
149 | |||
150 | // no break |
||
151 | case 3: |
||
152 | if ($text === '' || !in_array($text, ['M', 'F'], true)) { |
||
153 | $notes['state'] = 3; |
||
154 | $this->conversation->update(); |
||
155 | |||
156 | $data['reply_markup'] = (new Keyboard(['M', 'F'])) |
||
157 | ->setResizeKeyboard(true) |
||
158 | ->setOneTimeKeyboard(true) |
||
159 | ->setSelective(true); |
||
160 | |||
161 | $data['text'] = 'Select your gender:'; |
||
162 | if ($text !== '') { |
||
163 | $data['text'] = 'Select your gender, choose a keyboard option:'; |
||
164 | } |
||
165 | |||
166 | $result = Request::sendMessage($data); |
||
167 | break; |
||
168 | } |
||
169 | |||
170 | $notes['gender'] = $text; |
||
171 | |||
172 | // no break |
||
173 | case 4: |
||
174 | View Code Duplication | if ($message->getLocation() === null) { |
|
175 | $notes['state'] = 4; |
||
176 | $this->conversation->update(); |
||
177 | |||
178 | $data['reply_markup'] = (new Keyboard( |
||
179 | (new KeyboardButton('Share Location'))->setRequestLocation(true) |
||
180 | )) |
||
181 | ->setOneTimeKeyboard(true) |
||
182 | ->setResizeKeyboard(true) |
||
183 | ->setSelective(true); |
||
184 | |||
185 | $data['text'] = 'Share your location:'; |
||
186 | |||
187 | $result = Request::sendMessage($data); |
||
188 | break; |
||
189 | } |
||
190 | |||
191 | $notes['longitude'] = $message->getLocation()->getLongitude(); |
||
192 | $notes['latitude'] = $message->getLocation()->getLatitude(); |
||
193 | |||
194 | // no break |
||
195 | case 5: |
||
196 | View Code Duplication | if ($message->getPhoto() === null) { |
|
197 | $notes['state'] = 5; |
||
198 | $this->conversation->update(); |
||
199 | |||
200 | $data['text'] = 'Insert your picture:'; |
||
201 | |||
202 | $result = Request::sendMessage($data); |
||
203 | break; |
||
204 | } |
||
205 | |||
206 | /** @var PhotoSize $photo */ |
||
207 | $photo = $message->getPhoto()[0]; |
||
208 | $notes['photo_id'] = $photo->getFileId(); |
||
209 | |||
210 | // no break |
||
211 | case 6: |
||
212 | View Code Duplication | if ($message->getContact() === null) { |
|
213 | $notes['state'] = 6; |
||
214 | $this->conversation->update(); |
||
215 | |||
216 | $data['reply_markup'] = (new Keyboard( |
||
217 | (new KeyboardButton('Share Contact'))->setRequestContact(true) |
||
218 | )) |
||
219 | ->setOneTimeKeyboard(true) |
||
220 | ->setResizeKeyboard(true) |
||
221 | ->setSelective(true); |
||
222 | |||
223 | $data['text'] = 'Share your contact information:'; |
||
224 | |||
225 | $result = Request::sendMessage($data); |
||
226 | break; |
||
227 | } |
||
228 | |||
229 | $notes['phone_number'] = $message->getContact()->getPhoneNumber(); |
||
230 | |||
231 | // no break |
||
232 | case 7: |
||
233 | $this->conversation->update(); |
||
234 | $out_text = '/Survey result:' . PHP_EOL; |
||
235 | unset($notes['state']); |
||
236 | foreach ($notes as $k => $v) { |
||
237 | $out_text .= PHP_EOL . ucfirst($k) . ': ' . $v; |
||
238 | } |
||
239 | |||
240 | $data['photo'] = $notes['photo_id']; |
||
241 | $data['reply_markup'] = Keyboard::remove(['selective' => true]); |
||
242 | $data['caption'] = $out_text; |
||
243 | $this->conversation->stop(); |
||
244 | |||
245 | $result = Request::sendPhoto($data); |
||
246 | break; |
||
247 | } |
||
248 | |||
249 | return $result; |
||
250 | } |
||
251 | } |
||
252 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.