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:
Complex classes like FrmInbox often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FrmInbox, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class FrmInbox extends FrmFormApi { |
||
| 6 | |||
| 7 | protected $cache_key; |
||
| 8 | |||
| 9 | private $option = 'frm_inbox'; |
||
| 10 | |||
| 11 | private $messages = false; |
||
| 12 | |||
| 13 | public function __construct( $for_parent = null ) { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @since 4.05 |
||
| 20 | */ |
||
| 21 | protected function set_cache_key() { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @since 4.05 |
||
| 27 | */ |
||
| 28 | protected function api_url() { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @since 4.05 |
||
| 34 | */ |
||
| 35 | public function get_messages() { |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @since 4.05 |
||
| 41 | */ |
||
| 42 | public function set_messages() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @since 4.05 |
||
| 58 | */ |
||
| 59 | private function add_api_messages() { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param array $message |
||
| 72 | */ |
||
| 73 | public function add_message( $message ) { |
||
| 98 | |||
| 99 | private function fill_message( $message ) { |
||
| 111 | |||
| 112 | private function clean_messages() { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $key |
||
| 131 | */ |
||
| 132 | View Code Duplication | public function mark_read( $key ) { |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $key |
||
| 147 | * |
||
| 148 | * @since 4.05.02 |
||
| 149 | */ |
||
| 150 | public function mark_unread( $key ) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $key |
||
| 160 | */ |
||
| 161 | View Code Duplication | public function dismiss( $key ) { |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @since 4.06 |
||
| 181 | */ |
||
| 182 | private function dismiss_all() { |
||
| 195 | |||
| 196 | public function unread() { |
||
| 206 | |||
| 207 | public function unread_html() { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @since 4.05.02 |
||
| 218 | */ |
||
| 219 | public function remove( $key ) { |
||
| 225 | |||
| 226 | private function update_list() { |
||
| 229 | } |
||
| 230 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..