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:
1 | <?php |
||
41 | class AppController extends CakeController |
||
42 | { |
||
43 | |||
44 | /** |
||
45 | * Hold CMS object. |
||
46 | * |
||
47 | * @var Cms |
||
48 | */ |
||
49 | public $cms; |
||
50 | |||
51 | /** |
||
52 | * Initialization hook method. |
||
53 | * |
||
54 | * @return void |
||
55 | * |
||
56 | * @throws \JBZoo\Utils\Exception |
||
57 | */ |
||
58 | View Code Duplication | public function initialize() |
|
69 | |||
70 | /** |
||
71 | * Called after the controller action is run, but before the view is rendered. You can use this method |
||
72 | * to perform logic or set view variables that are required on every request. |
||
73 | * |
||
74 | * @param \Cake\Event\Event $event The beforeRender event. |
||
75 | * @return void |
||
76 | * |
||
77 | * @throws \JBZoo\Utils\Exception |
||
78 | */ |
||
79 | public function beforeRender(Event $event) |
||
92 | |||
93 | /** |
||
94 | * Called before the controller action. You can use this method to configure and customize components |
||
95 | * or perform logic that needs to happen before each controller action. |
||
96 | * |
||
97 | * @param Event $event |
||
98 | * @return void |
||
99 | * |
||
100 | * @throws \JBZoo\Utils\Exception |
||
101 | */ |
||
102 | View Code Duplication | public function beforeFilter(Event $event) |
|
111 | |||
112 | /** |
||
113 | * The beforeRedirect method is invoked when the controller's redirect method is called but before any |
||
114 | * further action. |
||
115 | * |
||
116 | * @param Event $event |
||
117 | * @param array|string $url |
||
118 | * @param Response $response |
||
119 | * @return void |
||
120 | * |
||
121 | * @throws \JBZoo\Utils\Exception |
||
122 | */ |
||
123 | View Code Duplication | public function beforeRedirect(Event $event, $url, Response $response) |
|
130 | |||
131 | /** |
||
132 | * Called after the controller action is run and rendered. |
||
133 | * |
||
134 | * @param Event $event |
||
135 | * @return void |
||
136 | * |
||
137 | * @throws \JBZoo\Utils\Exception |
||
138 | */ |
||
139 | public function afterFilter(Event $event) |
||
146 | |||
147 | /** |
||
148 | * Setup application theme. |
||
149 | * |
||
150 | * @return void |
||
151 | */ |
||
152 | protected function _setTheme() |
||
159 | } |
||
160 |
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.