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 |
||
| 10 | class PagesController extends AppController |
||
| 11 | { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Initialization hook method. |
||
| 15 | * |
||
| 16 | * @return void |
||
| 17 | */ |
||
| 18 | public function initialize() |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Beforefilter. |
||
| 27 | * |
||
| 28 | * @param Event $event The beforeFilter event that was fired. |
||
| 29 | * |
||
| 30 | * @return void |
||
| 31 | */ |
||
| 32 | public function beforeFilter(Event $event) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Home page. |
||
| 41 | * |
||
| 42 | * @return void |
||
| 43 | */ |
||
| 44 | public function home() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Build the statistics for the home page. |
||
| 96 | * |
||
| 97 | * @param array $array The array of statistics to build. |
||
| 98 | * |
||
| 99 | * @return array |
||
| 100 | */ |
||
| 101 | View Code Duplication | protected function _buildStats(array $array) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * The user accept the use of cookies. |
||
| 120 | * |
||
| 121 | * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request. |
||
| 122 | * |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | public function acceptCookie() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Redirect to the referer. |
||
| 146 | * |
||
| 147 | * @return void |
||
| 148 | */ |
||
| 149 | public function lang() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Display the maintenance page or a 404 if the site isn't in maintenance. |
||
| 156 | * |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | public function maintenance() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Display the Terms page. |
||
| 168 | * |
||
| 169 | * @return void |
||
| 170 | */ |
||
| 171 | public function terms() |
||
| 174 | } |
||
| 175 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read 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.