| Conditions | 8 |
| Paths | 6 |
| Total Lines | 62 |
| Code Lines | 38 |
| 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 |
||
| 25 | public function vote() |
||
| 26 | { |
||
| 27 | if (!$this->request->is('post') || !isset($this->request->data['answer_id'])) { |
||
| 28 | throw new NotFoundException(); |
||
| 29 | } |
||
| 30 | |||
| 31 | $this->loadModel('PollsAnswers'); |
||
| 32 | $answer = $this->PollsAnswers |
||
|
|
|||
| 33 | ->find() |
||
| 34 | ->contain([ |
||
| 35 | 'Polls' |
||
| 36 | ]) |
||
| 37 | ->where([ |
||
| 38 | 'PollsAnswers.id' => $this->request->data['answer_id'], |
||
| 39 | 'Polls.article_id' => $this->request->id |
||
| 40 | ]) |
||
| 41 | ->first(); |
||
| 42 | |||
| 43 | if (is_null($answer)) { |
||
| 44 | $this->Flash->error(__('This answer doesn\'t exist.')); |
||
| 45 | |||
| 46 | return $this->redirect($this->referer()); |
||
| 47 | } |
||
| 48 | |||
| 49 | if ($answer->poll->is_timed == true && $answer->poll->end_date < new Time()) { |
||
| 50 | $this->Flash->error(__('This poll is expired, you can not vote anymore.')); |
||
| 51 | |||
| 52 | return $this->redirect($this->referer()); |
||
| 53 | } |
||
| 54 | |||
| 55 | $this->loadModel('PollsUsers'); |
||
| 56 | $hasVoted = $this->PollsUsers |
||
| 57 | ->find() |
||
| 58 | ->contain([ |
||
| 59 | 'PollsAnswers' |
||
| 60 | ]) |
||
| 61 | ->where([ |
||
| 62 | 'PollsUsers.poll_id' => $answer->poll->id, |
||
| 63 | 'PollsUsers.user_id' => $this->Auth->user('id') |
||
| 64 | ]) |
||
| 65 | ->first(); |
||
| 66 | |||
| 67 | if (!is_null($hasVoted)) { |
||
| 68 | $this->Flash->error(__('You have already voted for this poll ! (You voted <strong>{0}</strong>)', h($hasVoted->polls_answer->response))); |
||
| 69 | |||
| 70 | return $this->redirect($this->referer()); |
||
| 71 | } |
||
| 72 | |||
| 73 | $this->request->data['poll_id'] = $answer->poll->id; |
||
| 74 | $this->request->data['user_id'] = $this->Auth->user('id'); |
||
| 75 | $user = $this->PollsUsers->newEntity($this->request->data); |
||
| 76 | |||
| 77 | if ($this->PollsUsers->save($user)) { |
||
| 78 | $this->Flash->success(__('Your have successfully voted for this poll ! (You voted <strong>{0}</strong>)', h($answer->response))); |
||
| 79 | |||
| 80 | return $this->redirect($this->referer()); |
||
| 81 | } |
||
| 82 | |||
| 83 | $this->Flash->error(__('An error occured while saving the vote !')); |
||
| 84 | |||
| 85 | return $this->redirect($this->referer()); |
||
| 86 | } |
||
| 87 | } |
||
| 88 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.