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 customWhere method. |
||
| 15 | * |
||
| 16 | * @codeCoverageIgnore |
||
| 17 | * |
||
| 18 | * @return \Closure |
||
| 19 | */ |
||
| 20 | public function queryHook() |
||
| 38 | |||
| 39 | public function isDirty() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Register hook on save method. |
||
| 53 | * |
||
| 54 | * @codeCoverageIgnore |
||
| 55 | * |
||
| 56 | * @return \Closure |
||
| 57 | */ |
||
| 58 | public function save() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Register hook on isset call. |
||
| 69 | * |
||
| 70 | * @codeCoverageIgnore |
||
| 71 | * |
||
| 72 | * @return \Closure |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function __issetHook() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Register hook on unset call. |
||
| 89 | * |
||
| 90 | * @codeCoverageIgnore |
||
| 91 | * |
||
| 92 | * @return \Closure |
||
| 93 | */ |
||
| 94 | public function __unsetHook() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Register hook on getAttribute method. |
||
| 109 | * |
||
| 110 | * @codeCoverageIgnore |
||
| 111 | * |
||
| 112 | * @return \Closure |
||
| 113 | */ |
||
| 114 | View Code Duplication | public function getAttribute() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Register hook on setAttribute method. |
||
| 129 | * |
||
| 130 | * @codeCoverageIgnore |
||
| 131 | * |
||
| 132 | * @return \Closure |
||
| 133 | */ |
||
| 134 | public function setAttribute() |
||
| 146 | |||
| 147 | View Code Duplication | public function __call($method, $params) |
|
| 155 | } |
||
| 156 |
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: