Conditions | 2 |
Paths | 1 |
Total Lines | 58 |
Code Lines | 39 |
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 |
||
14 | public function sidebar() |
||
15 | { |
||
16 | $this->loadModel('BlogCategories'); |
||
17 | $this->loadModel('BlogArticles'); |
||
18 | |||
19 | $articleSearch = $this->BlogArticles->newEntity($this->request->data); |
||
20 | |||
21 | //Select all Categories. |
||
22 | $categories = $this->BlogCategories |
||
23 | ->find() |
||
24 | ->select([ |
||
25 | 'id', |
||
26 | 'title', |
||
27 | 'article_count' |
||
28 | ]) |
||
29 | ->all(); |
||
30 | |||
31 | //Select featured article. |
||
32 | $featured = $this->BlogArticles |
||
33 | ->find() |
||
34 | ->select([ |
||
35 | 'id', |
||
36 | 'title', |
||
37 | 'created', |
||
38 | 'comment_count' |
||
39 | ]) |
||
40 | ->contain([ |
||
41 | 'Users' => function ($q) { |
||
42 | return $q->find('short'); |
||
43 | } |
||
44 | ]) |
||
45 | ->where([ |
||
46 | 'BlogArticles.id !=' => ($this->request->id) ? $this->request->id : 0, |
||
47 | 'BlogArticles.is_display' => 1 |
||
48 | ]) |
||
49 | ->order([ |
||
50 | 'BlogArticles.created' => 'desc' |
||
51 | ]) |
||
52 | ->first(); |
||
53 | |||
54 | //Select all articles and group them by monthly. |
||
55 | $archives = $this->BlogArticles |
||
56 | ->find('all') |
||
57 | ->select([ |
||
58 | 'date' => 'DATE(created)', |
||
59 | 'count' => 'COUNT(*)' |
||
60 | ]) |
||
61 | ->group('SUBSTR(DATE(created), 1, 7)') |
||
62 | ->order([ |
||
63 | 'date' => 'desc' |
||
64 | ]) |
||
65 | ->where([ |
||
66 | 'is_display' => 1 |
||
67 | ]) |
||
68 | ->toArray(); |
||
69 | |||
70 | $this->set(compact('categories', 'featured', 'archives', 'articleSearch')); |
||
71 | } |
||
72 | } |
||
73 |
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: