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 ) { |
||
| 14 | $this->set_cache_key(); |
||
| 15 | $this->set_messages(); |
||
| 16 | } |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @since 4.05 |
||
| 20 | */ |
||
| 21 | protected function set_cache_key() { |
||
| 22 | $this->cache_key = 'frm_inbox_cache'; |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @since 4.05 |
||
| 27 | */ |
||
| 28 | protected function api_url() { |
||
| 29 | return 'https://formidableforms.com/wp-json/inbox/v1/message/'; |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @since 4.05 |
||
| 34 | */ |
||
| 35 | public function get_messages( $filter = false ) { |
||
| 36 | $messages = $this->messages; |
||
| 37 | if ( $filter === 'filter' ) { |
||
| 38 | $this->filter_messages( $messages ); |
||
| 39 | } |
||
| 40 | return $messages; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @since 4.05 |
||
| 45 | */ |
||
| 46 | public function set_messages() { |
||
| 47 | $this->messages = get_option( $this->option ); |
||
| 48 | if ( empty( $this->messages ) ) { |
||
| 49 | $this->messages = array(); |
||
|
|
|||
| 50 | } |
||
| 51 | |||
| 52 | $this->add_api_messages(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Messages are in an array. |
||
| 56 | */ |
||
| 57 | $this->messages = apply_filters( 'frm_inbox', $this->messages ); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @since 4.05 |
||
| 62 | */ |
||
| 63 | private function add_api_messages() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param array $message |
||
| 76 | */ |
||
| 77 | public function add_message( $message ) { |
||
| 78 | if ( isset( $this->messages[ $message['key'] ] ) && ! isset( $message['force'] ) ) { |
||
| 103 | |||
| 104 | private function fill_message( $message ) { |
||
| 117 | |||
| 118 | private function clean_messages() { |
||
| 134 | |||
| 135 | private function filter_messages( &$messages ) { |
||
| 146 | |||
| 147 | private function is_expired( $message ) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Show different messages for different accounts. |
||
| 153 | */ |
||
| 154 | private function is_for_user( $message ) { |
||
| 161 | |||
| 162 | private function get_user_type() { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $key |
||
| 172 | */ |
||
| 173 | View Code Duplication | public function mark_read( $key ) { |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @param string $key |
||
| 188 | * |
||
| 189 | * @since 4.05.02 |
||
| 190 | */ |
||
| 191 | public function mark_unread( $key ) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param string $key |
||
| 201 | */ |
||
| 202 | View Code Duplication | public function dismiss( $key ) { |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @since 4.06 |
||
| 222 | */ |
||
| 223 | private function dismiss_all() { |
||
| 236 | |||
| 237 | public function unread() { |
||
| 247 | |||
| 248 | public function unread_html() { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @since 4.05.02 |
||
| 259 | */ |
||
| 260 | public function remove( $key ) { |
||
| 266 | |||
| 267 | private function update_list() { |
||
| 270 | } |
||
| 271 |
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..