Conditions | 7 |
Paths | 10 |
Total Lines | 55 |
Code Lines | 30 |
Lines | 7 |
Ratio | 12.73 % |
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 |
||
31 | public function ignore() |
||
32 | { |
||
33 | // check if not auth |
||
34 | if (!App::$User->isAuth()) { |
||
35 | throw new ForbiddenException(); |
||
36 | } |
||
37 | |||
38 | // get user object and init model of it |
||
39 | $user = App::$User->identity(); |
||
40 | $model = new FormIgnoreAdd($user); |
||
41 | |||
42 | // set user id from ?id= get param if form not sended |
||
43 | if (!$model->send()) { |
||
44 | $uid = (int)$this->request->query->get('id'); |
||
45 | if ($uid > 0) { |
||
46 | $model->id = $uid; |
||
47 | } |
||
48 | } |
||
49 | |||
50 | // sended new block add? |
||
51 | View Code Duplication | if ($model->send() && $model->validate()) { |
|
|
|||
52 | if ($model->save()) { |
||
53 | App::$Session->getFlashBag()->add('success', __('User was successful blocked!')); |
||
54 | } else { |
||
55 | App::$Session->getFlashBag()->add('error', __('This user is always in ignore list')); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | // get blocked users |
||
60 | $query = Blacklist::where('user_id', '=', $user->getId()); |
||
61 | |||
62 | $page = (int)$this->request->query->get('page'); |
||
63 | $offset = $page * $this->_blockPerPage; |
||
64 | |||
65 | // build pagination |
||
66 | $pagination = new SimplePagination([ |
||
67 | 'url' => ['profile/ignore'], |
||
68 | 'page' => $page, |
||
69 | 'step' => $this->_blockPerPage, |
||
70 | 'total' => $query->count() |
||
71 | ]); |
||
72 | |||
73 | // get records as object |
||
74 | $records = $query->with(['targetUser', 'targetUser.profile']) |
||
75 | ->skip($offset) |
||
76 | ->take($this->_blockPerPage) |
||
77 | ->get(); |
||
78 | |||
79 | // render output view |
||
80 | return $this->view->render('ignore', [ |
||
81 | 'records' => $records, |
||
82 | 'model' => $model, |
||
83 | 'pagination' => $pagination |
||
84 | ]); |
||
85 | } |
||
86 | } |
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.