| Conditions | 5 |
| Paths | 5 |
| Total Lines | 52 |
| Code Lines | 34 |
| 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 |
||
| 50 | public function registerBadge(Event $event) |
||
| 51 | { |
||
| 52 | $this->Badges = TableRegistry::get('Badges'); |
||
| 53 | $user = $event->getData('user'); |
||
| 54 | |||
| 55 | if (!$user instanceof User) { |
||
| 56 | return false; |
||
| 57 | } |
||
| 58 | |||
| 59 | $badges = $this->Badges |
||
| 60 | ->find('all') |
||
| 61 | ->select([ |
||
| 62 | 'id', |
||
| 63 | 'name', |
||
| 64 | 'picture', |
||
| 65 | 'rule' |
||
| 66 | ]) |
||
| 67 | ->where([ |
||
| 68 | 'type' => 'registration' |
||
| 69 | ]) |
||
| 70 | ->hydrate(false) |
||
| 71 | ->toArray(); |
||
| 72 | |||
| 73 | if (empty($badges)) { |
||
| 74 | return true; |
||
| 75 | } |
||
| 76 | |||
| 77 | $this->Users = TableRegistry::get('Users'); |
||
| 78 | |||
| 79 | $userId = $event->getData('user')->id; |
||
| 80 | $user = $this->Users |
||
| 81 | ->find() |
||
| 82 | ->where([ |
||
| 83 | 'id' => $userId |
||
| 84 | ]) |
||
| 85 | ->select([ |
||
| 86 | 'created' |
||
| 87 | ]) |
||
| 88 | ->first(); |
||
| 89 | |||
| 90 | $today = new Time(); |
||
| 91 | $created = $user->created; |
||
| 92 | $diff = $today->diff($created)->y; |
||
| 93 | |||
| 94 | foreach ($badges as $badge) { |
||
| 95 | if ($diff >= $badge['rule']) { |
||
| 96 | $this->_unlockBadge($badge, $userId); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | return true; |
||
| 101 | } |
||
| 102 | |||
| 208 |
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: