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 |
||
| 11 | class Hooks |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Register hook on getAttribute method. |
||
| 15 | * |
||
| 16 | * @return \Closure |
||
| 17 | */ |
||
| 18 | public function getAttribute() |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Register hook on setAttribute method. |
||
| 33 | * |
||
| 34 | * @return \Closure |
||
| 35 | */ |
||
| 36 | public function setAttribute() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Register hook on toArray method. |
||
| 51 | * |
||
| 52 | * @return \Closure |
||
| 53 | */ |
||
| 54 | public function toArray() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Register hook on replicate method. |
||
| 67 | * |
||
| 68 | * @return \Closure |
||
| 69 | */ |
||
| 70 | public function replicate() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Register hook on save method. |
||
| 85 | * |
||
| 86 | * @return \Closure |
||
| 87 | */ |
||
| 88 | public function save() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Register hook on isset call. |
||
| 99 | * |
||
| 100 | * @return \Closure |
||
| 101 | */ |
||
| 102 | public function __issetHook() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Register hook on unset call. |
||
| 117 | * |
||
| 118 | * @return \Closure |
||
| 119 | */ |
||
| 120 | public function __unsetHook() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Register hook on queryHook method. |
||
| 135 | * |
||
| 136 | * @return \Closure |
||
| 137 | */ |
||
| 138 | public function queryHook() |
||
| 156 | |||
| 157 | View Code Duplication | public function __call($method, $params) |
|
| 165 | } |
||
| 166 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: