Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ConversationsController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ConversationsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class ConversationsController extends AppController |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Initialization hook method. |
||
| 17 | * |
||
| 18 | * @return void |
||
| 19 | */ |
||
| 20 | public function initialize() |
||
| 21 | { |
||
| 22 | parent::initialize(); |
||
| 23 | |||
| 24 | $this->loadComponent('RequestHandler'); |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * BeforeFilter handle. |
||
| 29 | * |
||
| 30 | * @param Event $event The beforeFilter event that was fired. |
||
| 31 | * |
||
| 32 | * @return void |
||
| 33 | */ |
||
| 34 | public function beforeFilter(Event $event) |
||
| 35 | { |
||
| 36 | parent::beforeFilter($event); |
||
| 37 | |||
| 38 | $this->Auth->deny(); |
||
| 39 | |||
| 40 | if (Configure::read('Conversations.enabled') === false && $this->request->action != 'maintenance') { |
||
| 41 | $this->redirect(['action' => 'maintenance']); |
||
| 42 | } elseif (Configure::read('Conversations.enabled') === true && $this->request->action == 'maintenance') { |
||
| 43 | $this->redirect(['action' => 'index']); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Display all conversations for the user. |
||
| 49 | * |
||
| 50 | * @return void |
||
| 51 | */ |
||
| 52 | public function index() |
||
| 53 | { |
||
| 54 | $this->ConversationsUsers = $this->loadModel('ConversationsUsers'); |
||
|
|
|||
| 55 | |||
| 56 | $this->paginate = [ |
||
| 57 | 'maxLimit' => Configure::read('Conversations.conversations_per_page') |
||
| 58 | ]; |
||
| 59 | $conversations = $this->ConversationsUsers |
||
| 60 | ->find() |
||
| 61 | ->contain([ |
||
| 62 | 'Users', |
||
| 63 | 'Conversations', |
||
| 64 | 'Conversations.Users' => function ($q) { |
||
| 65 | return $q->find('medium'); |
||
| 66 | }, |
||
| 67 | 'Conversations.LastMessage', |
||
| 68 | 'Conversations.LastMessageUser' |
||
| 69 | ]) |
||
| 70 | ->where([ |
||
| 71 | 'ConversationsUsers.user_id' => $this->Auth->user('id'), |
||
| 72 | 'Conversations.conversation_open <>' => 2 |
||
| 73 | ]) |
||
| 74 | ->order([ |
||
| 75 | 'ConversationsUsers.is_read' => 'ASC', |
||
| 76 | 'ConversationsUsers.is_star' => 'DESC', |
||
| 77 | 'Conversations.last_message_date' => 'DESC', |
||
| 78 | ]); |
||
| 79 | |||
| 80 | $conversations = $this->paginate($conversations); |
||
| 81 | |||
| 82 | $this->set(compact('conversations')); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Function to do an action when a user select a/many conversations. |
||
| 87 | * |
||
| 88 | * Action list : |
||
| 89 | * star : Conversations Important |
||
| 90 | * normal : Make normal conversation |
||
| 91 | * exit : Exit conversations |
||
| 92 | * |
||
| 93 | * @return void |
||
| 94 | * |
||
| 95 | * @throws NotFoundException |
||
| 96 | */ |
||
| 97 | public function action() |
||
| 98 | { |
||
| 99 | if (!$this->request->is('ajax')) { |
||
| 100 | throw new NotFoundException(); |
||
| 101 | } |
||
| 102 | |||
| 103 | $actionAllowed = ['star', 'normal', 'exit']; |
||
| 104 | |||
| 105 | View Code Duplication | if (!array_key_exists('action', $this->request->getParsedBody()) || !in_array($this->request->getData('action'), $actionAllowed)) { |
|
| 106 | $json = []; |
||
| 107 | $json['error'] = '1'; |
||
| 108 | $json['message'] = __d('conversations', 'Action unknown.'); |
||
| 109 | |||
| 110 | $this->set(compact('json')); |
||
| 111 | $this->set('_serialize', 'json'); |
||
| 112 | |||
| 113 | return; |
||
| 114 | } |
||
| 115 | |||
| 116 | View Code Duplication | if (!array_key_exists('conversations', $this->request->getParsedBody())) { |
|
| 117 | $json = []; |
||
| 118 | $json['error'] = '1'; |
||
| 119 | $json['message'] = __d('conversations', 'You have not chosen any conversations.'); |
||
| 120 | |||
| 121 | $this->set(compact('json')); |
||
| 122 | $this->set('_serialize', 'json'); |
||
| 123 | |||
| 124 | return; |
||
| 125 | } |
||
| 126 | |||
| 127 | $this->loadModel('ConversationsUsers'); |
||
| 128 | |||
| 129 | $action = $this->request->getData('action'); |
||
| 130 | $array = $this->request->getData('conversations'); |
||
| 131 | |||
| 132 | switch ($action) { |
||
| 133 | View Code Duplication | case "star": |
|
| 134 | foreach ($array as $conversationId) { |
||
| 135 | $this->ConversationsUsers->updateAll( |
||
| 136 | ['is_star' => 1], |
||
| 137 | [ |
||
| 138 | 'conversation_id' => $conversationId, |
||
| 139 | 'user_id' => $this->Auth->user('id') |
||
| 140 | ] |
||
| 141 | ); |
||
| 142 | } |
||
| 143 | |||
| 144 | $json['message'] = __d('conversations', 'Your conversation(s) has been Stared.'); |
||
| 145 | $json['error'] = '0'; |
||
| 146 | $json['redirect'] = Router::url(['action' => 'index']); |
||
| 147 | |||
| 148 | $this->set(compact('json')); |
||
| 149 | |||
| 150 | break; |
||
| 151 | |||
| 152 | View Code Duplication | case "normal": |
|
| 153 | foreach ($array as $conversationId) { |
||
| 154 | $this->ConversationsUsers->updateAll( |
||
| 155 | ['is_star' => 0], |
||
| 156 | [ |
||
| 157 | 'conversation_id' => $conversationId, |
||
| 158 | 'user_id' => $this->Auth->user('id') |
||
| 159 | ] |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | |||
| 163 | $json['message'] = __d('conversations', 'Your conversation(s) has been set normal.'); |
||
| 164 | $json['error'] = '0'; |
||
| 165 | $json['redirect'] = Router::url(['action' => 'index']); |
||
| 166 | |||
| 167 | $this->set(compact('json')); |
||
| 168 | break; |
||
| 169 | |||
| 170 | case "exit": |
||
| 171 | foreach ($array as $conversationId) { |
||
| 172 | $user = $this->ConversationsUsers |
||
| 173 | ->find() |
||
| 174 | ->contain([ |
||
| 175 | 'Conversations' |
||
| 176 | ]) |
||
| 177 | ->where([ |
||
| 178 | 'ConversationsUsers.user_id' => $this->Auth->user('id'), |
||
| 179 | 'ConversationsUsers.conversation_id' => $conversationId |
||
| 180 | ]) |
||
| 181 | ->first(); |
||
| 182 | |||
| 183 | //Check if the user is the owner of the conversation. |
||
| 184 | if ($user->conversation->user_id == $this->Auth->user('id')) { |
||
| 185 | $conversation = $this->Conversations->get($conversationId); |
||
| 186 | $conversation->conversation_open = 2; |
||
| 187 | $this->Conversations->save($conversation); |
||
| 188 | } else { |
||
| 189 | $this->ConversationsUsers->delete($user); |
||
| 190 | |||
| 191 | $conversation = $this->Conversations->get($conversationId); |
||
| 192 | $conversation->recipient_count = $user->conversation->recipient_count - 1; |
||
| 193 | $this->Conversations->save($conversation); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | $json['message'] = __d('conversations', 'You have left your conversation(s) successfully.'); |
||
| 198 | $json['error'] = '0'; |
||
| 199 | $json['redirect'] = Router::url(['action' => 'index']); |
||
| 200 | |||
| 201 | $this->set(compact('json')); |
||
| 202 | break; |
||
| 203 | } |
||
| 204 | |||
| 205 | $this->set('_serialize', 'json'); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Create a new conversation. |
||
| 210 | * |
||
| 211 | * @return void |
||
| 212 | */ |
||
| 213 | public function create() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Display a conversation. |
||
| 357 | * |
||
| 358 | * @return void|\Cake\Network\Response |
||
| 359 | */ |
||
| 360 | public function view() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Action to search some users when adding an user in a conversation. |
||
| 445 | * |
||
| 446 | * @return void |
||
| 447 | * |
||
| 448 | * @throws \Cake\Network\Exception\NotFoundException |
||
| 449 | */ |
||
| 450 | public function inviteMember() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Quote a post. |
||
| 477 | * |
||
| 478 | * @throws \Cake\Network\Exception\NotFoundException |
||
| 479 | * |
||
| 480 | * @return mixed |
||
| 481 | */ |
||
| 482 | public function quote() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Get the form to edit a message. |
||
| 540 | * |
||
| 541 | * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request. |
||
| 542 | * |
||
| 543 | * @return void |
||
| 544 | */ |
||
| 545 | public function getEditMessage() |
||
| 546 | { |
||
| 547 | if (!$this->request->is('ajax')) { |
||
| 548 | throw new NotFoundException(); |
||
| 549 | } |
||
| 550 | |||
| 551 | $this->loadModel('ConversationsMessages'); |
||
| 552 | $this->viewBuilder()->layout(false); |
||
| 553 | |||
| 554 | $message = $this->ConversationsMessages |
||
| 555 | ->find() |
||
| 556 | ->where([ |
||
| 557 | 'ConversationsMessages.id' => $this->request->getData('id') |
||
| 558 | ]) |
||
| 559 | ->first(); |
||
| 560 | |||
| 561 | $json = [ |
||
| 562 | 'error' => false, |
||
| 563 | 'errorMessage' => '' |
||
| 564 | ]; |
||
| 565 | |||
| 566 | View Code Duplication | if (is_null($message)) { |
|
| 567 | $json['error'] = true; |
||
| 568 | $json['errorMessage'] = __d('conversations', "This message doesn't exist or has been deleted !"); |
||
| 569 | |||
| 570 | $this->set(compact('json')); |
||
| 571 | |||
| 572 | return; |
||
| 573 | } |
||
| 574 | |||
| 575 | //Current user. |
||
| 576 | $this->loadModel('Users'); |
||
| 577 | $currentUser = $this->Users |
||
| 578 | ->find() |
||
| 579 | ->contain([ |
||
| 580 | 'Groups' => function ($q) { |
||
| 581 | return $q->select(['id', 'is_staff']); |
||
| 582 | } |
||
| 583 | ]) |
||
| 584 | ->where([ |
||
| 585 | 'Users.id' => $this->Auth->user('id') |
||
| 586 | ]) |
||
| 587 | ->select(['id', 'group_id']) |
||
| 588 | ->first(); |
||
| 589 | |||
| 590 | if ($message->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) { |
||
| 591 | $json['error'] = true; |
||
| 592 | $json['errorMessage'] = __d('conversations', "You don't have the authorization to edit this message !"); |
||
| 593 | |||
| 594 | $this->set(compact('json')); |
||
| 595 | |||
| 596 | return; |
||
| 597 | } |
||
| 598 | |||
| 599 | $this->set(compact('json', 'message')); |
||
| 600 | } |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Edit a message. |
||
| 604 | * |
||
| 605 | * @param int $id Id of the message. |
||
| 606 | * |
||
| 607 | * @return \Cake\Network\Response |
||
| 608 | */ |
||
| 609 | public function messageEdit($id = null) |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Redirect an user to a conversation, page and message. |
||
| 665 | * |
||
| 666 | * @param int $messageId Id of the message. |
||
| 667 | * |
||
| 668 | * @return \Cake\Network\Response |
||
| 669 | */ |
||
| 670 | View Code Duplication | public function go($messageId = null) |
|
| 671 | { |
||
| 672 | $this->loadModel('ConversationsMessages'); |
||
| 673 | |||
| 674 | $message = $this->ConversationsMessages |
||
| 675 | ->find() |
||
| 676 | ->contain([ |
||
| 677 | 'Conversations' |
||
| 678 | ]) |
||
| 679 | ->where([ |
||
| 680 | 'ConversationsMessages.id' => $messageId |
||
| 681 | ]) |
||
| 682 | ->first(); |
||
| 683 | |||
| 684 | if (is_null($message)) { |
||
| 685 | $this->Flash->error(__d('conversations', "This message doesn't exist or has been deleted.")); |
||
| 686 | |||
| 687 | return $this->redirect(['controller' => 'conversations', 'action' => 'index']); |
||
| 688 | } |
||
| 689 | |||
| 690 | $message->toArray(); |
||
| 691 | |||
| 692 | //Count the number of messages before this message. |
||
| 693 | $messagesBefore = $this->ConversationsMessages |
||
| 694 | ->find() |
||
| 695 | ->where([ |
||
| 696 | 'ConversationsMessages.conversation_id' => $message->conversation_id, |
||
| 697 | 'ConversationsMessages.created <' => $message->created |
||
| 698 | ]) |
||
| 699 | ->count(); |
||
| 700 | |||
| 701 | //Get the number of messages per page. |
||
| 702 | $messagesPerPage = Configure::read('Conversations.messages_per_page'); |
||
| 703 | |||
| 704 | //Calculate the page. |
||
| 705 | $page = ceil($messagesBefore / $messagesPerPage); |
||
| 706 | |||
| 707 | $page = ($page > 1) ? $page : 1; |
||
| 708 | |||
| 709 | //Redirect the user. |
||
| 710 | return $this->redirect([ |
||
| 711 | '_name' => 'conversations-view', |
||
| 712 | 'slug' => $message->conversation->title, |
||
| 713 | 'id' => $message->conversation->id, |
||
| 714 | '?' => ['page' => $page], |
||
| 715 | '#' => 'message-' . $messageId |
||
| 716 | ]); |
||
| 717 | } |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Function to kick an user from a conversation. |
||
| 721 | * |
||
| 722 | * @return void |
||
| 723 | */ |
||
| 724 | public function kick() |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Reply to a conversation. |
||
| 809 | * |
||
| 810 | * @return void|\Cake\Network\Response |
||
| 811 | */ |
||
| 812 | public function reply() |
||
| 913 | |||
| 914 | /** |
||
| 915 | * Edit a conversation. |
||
| 916 | * |
||
| 917 | * @return \Cake\Network\Response |
||
| 918 | */ |
||
| 919 | public function edit() |
||
| 968 | |||
| 969 | /** |
||
| 970 | * Function to invite user(s) in a conversation. |
||
| 971 | * |
||
| 972 | * @return void|\Cake\Network\Response |
||
| 973 | */ |
||
| 974 | public function invite() |
||
| 1069 | |||
| 1070 | /** |
||
| 1071 | * Function to leave a conversation. |
||
| 1072 | * |
||
| 1073 | * @return void|\Cake\Network\Response |
||
| 1074 | */ |
||
| 1075 | public function leave() |
||
| 1122 | |||
| 1123 | /** |
||
| 1124 | * Search conversations. |
||
| 1125 | * |
||
| 1126 | * @return void |
||
| 1127 | */ |
||
| 1128 | public function search() |
||
| 1178 | |||
| 1179 | /** |
||
| 1180 | * Action to render the maintenance page. |
||
| 1181 | * |
||
| 1182 | * @return void |
||
| 1183 | */ |
||
| 1184 | public function maintenance() |
||
| 1187 | } |
||
| 1188 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write 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.