| Conditions | 11 |
| Paths | 7 |
| Total Lines | 70 |
| Code Lines | 42 |
| Lines | 3 |
| Ratio | 4.29 % |
| 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 show($userId) |
||
| 32 | { |
||
| 33 | $cfg = $this->application->configs; |
||
|
|
|||
| 34 | View Code Duplication | if (!(bool)$cfg['guestView'] && !App::$User->isAuth()) { |
|
| 35 | throw new ForbiddenException(__('You must login to view other profile')); |
||
| 36 | } |
||
| 37 | |||
| 38 | // check if target exists |
||
| 39 | if (!App::$User->isExist($userId)) { |
||
| 40 | throw new NotFoundException(__('This profile is not exist')); |
||
| 41 | } |
||
| 42 | |||
| 43 | $targetPersone = App::$User->identity($userId); // target user object instance of Apps\ActiveRecord\User |
||
| 44 | $viewerPersone = App::$User->identity(); // current user object(viewer) instance of Apps\ActiveRecord\User |
||
| 45 | |||
| 46 | $wallModel = null; |
||
| 47 | // if current user is auth - allow to post messages on wall current user |
||
| 48 | if (App::$User->isAuth() && $viewerPersone->role->can('global/write')) { |
||
| 49 | $wallModel = new FormWallPost(); |
||
| 50 | // check if request post is done and rules validated |
||
| 51 | if ($wallModel->send() && $wallModel->validate()) { |
||
| 52 | // maybe in blacklist? |
||
| 53 | if (!Blacklist::check($viewerPersone->getId(), $targetPersone->getId())) { |
||
| 54 | App::$Session->getFlashBag()->add('error', __('This user are in your black list or you are in blacklist!')); |
||
| 55 | } else { |
||
| 56 | // check if message added |
||
| 57 | if ($wallModel->makePost($targetPersone, $viewerPersone, (int)$cfg['delayBetweenPost'])) { |
||
| 58 | App::$Session->getFlashBag()->add('success', __('The message was successful posted!')); |
||
| 59 | } else { |
||
| 60 | App::$Session->getFlashBag()->add('warning', __('Posting message was failed! Please, wait few seconds')); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | // pagination and query params |
||
| 67 | $wallPage = (int)$this->request->query->get('page'); |
||
| 68 | $wallItems = (int)$cfg['wallPostOnPage']; |
||
| 69 | $wallOffset = $wallPage * $wallItems; |
||
| 70 | |||
| 71 | // get wall posts by target user_id |
||
| 72 | $wallQuery = WallPost::where('target_id', $targetPersone->getId()); |
||
| 73 | |||
| 74 | // build pagination |
||
| 75 | $wallPagination = new SimplePagination([ |
||
| 76 | 'url' => ['profile/show', $userId, null], |
||
| 77 | 'page' => $wallPage, |
||
| 78 | 'step' => $wallItems, |
||
| 79 | 'total' => $wallQuery->count() |
||
| 80 | ]); |
||
| 81 | |||
| 82 | // get wall messages as object |
||
| 83 | $wallRecords = $wallQuery->with(['senderUser', 'senderUser.profile', 'senderUser.role']) |
||
| 84 | ->orderBy('id', 'desc') |
||
| 85 | ->skip($wallOffset) |
||
| 86 | ->take($wallItems) |
||
| 87 | ->get(); |
||
| 88 | |||
| 89 | // render output view |
||
| 90 | return $this->view->render('show', [ |
||
| 91 | 'user' => $targetPersone, |
||
| 92 | 'viewer' => $viewerPersone, |
||
| 93 | 'isSelf' => ($viewerPersone !== null && $viewerPersone->id === $targetPersone->id), |
||
| 94 | 'wall' => $wallModel, |
||
| 95 | 'notify' => App::$Session->getFlashBag()->all(), |
||
| 96 | 'wallRecords' => $wallRecords, |
||
| 97 | 'pagination' => $wallPagination, |
||
| 98 | 'ratingOn' => (int)$cfg['rating'] === 1 |
||
| 99 | ]); |
||
| 100 | } |
||
| 101 | } |
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: