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 |
||
| 7 | class CommentController implements \Anax\DI\IInjectionAware |
||
| 8 | { |
||
| 9 | use \Anax\DI\TInjectable, |
||
| 10 | \Anax\MVC\TRedirectHelpers; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Initialize the controller. |
||
| 14 | * |
||
| 15 | * @return void |
||
| 16 | */ |
||
| 17 | public function initialize() |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Setup initial table for users. |
||
| 27 | * |
||
| 28 | * @return void |
||
| 29 | */ |
||
| 30 | public function setupAction() |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Add new comment. |
||
| 38 | * |
||
| 39 | * @param integer $id of question or answer |
||
| 40 | * @param string $q_or_a 'q' or 'a' for question or answer comment |
||
| 41 | * |
||
| 42 | * @return void |
||
| 43 | */ |
||
| 44 | View Code Duplication | public function addAction($id, $q_or_a) |
|
| 86 | /** |
||
| 87 | * Callback for submit-button. |
||
| 88 | * |
||
| 89 | */ |
||
| 90 | View Code Duplication | public function callbackSubmitAddComment($form) |
|
| 113 | /** |
||
| 114 | * Callback for submit-button. |
||
| 115 | * |
||
| 116 | */ |
||
| 117 | public function callbackSubmitFailAddComment($form) |
||
| 123 | /** |
||
| 124 | * Callback What to do if the form was submitted? |
||
| 125 | * |
||
| 126 | */ |
||
| 127 | public function callbackSuccess($form) |
||
| 133 | /** |
||
| 134 | * Callback What to do when form could not be processed? |
||
| 135 | * |
||
| 136 | */ |
||
| 137 | public function callbackFail($form) |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Delete a comment. |
||
| 147 | * |
||
| 148 | * @return void |
||
| 149 | */ |
||
| 150 | // public function deleteAction() |
||
| 151 | // { |
||
| 152 | // $id = $this->request->getGet('id'); |
||
| 153 | // if (!isset($id)) { |
||
| 154 | // die("Missing id"); |
||
| 155 | // } |
||
| 156 | // |
||
| 157 | // $res = $this->comments->delete($id); |
||
| 158 | // $this->redirectTo($_SERVER['HTTP_REFERER']); |
||
| 159 | // } |
||
| 160 | } |
||
| 161 |
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: