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 | /** |
||
40 | * Register hook on save method. |
||
41 | * |
||
42 | * @codeCoverageIgnore |
||
43 | * |
||
44 | * @return \Closure |
||
45 | */ |
||
46 | public function save() |
||
54 | |||
55 | /** |
||
56 | * Register hook on isset call. |
||
57 | * |
||
58 | * @codeCoverageIgnore |
||
59 | * |
||
60 | * @return \Closure |
||
61 | */ |
||
62 | View Code Duplication | public function __issetHook() |
|
74 | |||
75 | /** |
||
76 | * Register hook on unset call. |
||
77 | * |
||
78 | * @codeCoverageIgnore |
||
79 | * |
||
80 | * @return \Closure |
||
81 | */ |
||
82 | public function __unsetHook() |
||
94 | |||
95 | /** |
||
96 | * Register hook on getAttribute method. |
||
97 | * |
||
98 | * @codeCoverageIgnore |
||
99 | * |
||
100 | * @return \Closure |
||
101 | */ |
||
102 | View Code Duplication | public function getAttribute() |
|
114 | |||
115 | /** |
||
116 | * Register hook on setAttribute method. |
||
117 | * |
||
118 | * @codeCoverageIgnore |
||
119 | * |
||
120 | * @return \Closure |
||
121 | */ |
||
122 | public function setAttribute() |
||
134 | |||
135 | public function __call($method, $params) |
||
143 | } |
||
144 |
If you implement
__call
and 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
__call
is implemented by a parent class and only the child class knows which methods exist: