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 FrmFormApi 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 FrmFormApi, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class FrmFormApi { |
||
| 4 | |||
| 5 | protected $license = ''; |
||
| 6 | protected $cache_key = ''; |
||
| 7 | protected $cache_timeout = '+6 hours'; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * @since 3.06 |
||
| 11 | */ |
||
| 12 | public function __construct( $license = null ) { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @since 3.06 |
||
| 19 | */ |
||
| 20 | private function set_license( $license ) { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @since 3.06 |
||
| 32 | * @return string |
||
| 33 | */ |
||
| 34 | public function get_license() { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @since 3.06 |
||
| 40 | */ |
||
| 41 | protected function set_cache_key() { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @since 3.06 |
||
| 47 | * @return string |
||
| 48 | */ |
||
| 49 | public function get_cache_key() { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @since 3.06 |
||
| 55 | * @return array |
||
| 56 | */ |
||
| 57 | public function get_api_info() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @since 3.06 |
||
| 97 | */ |
||
| 98 | protected function api_url() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @since 3.06 |
||
| 104 | */ |
||
| 105 | protected function skip_categories() { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @since 3.06 |
||
| 111 | * |
||
| 112 | * @param object $license_plugin The FrmAddon object |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | public function get_addon_for_license( $license_plugin, $addons = array() ) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @since 3.06 |
||
| 137 | */ |
||
| 138 | public function get_pro_updater() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @since 3.06 |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | protected function get_cached() { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @since 3.06 |
||
| 172 | */ |
||
| 173 | protected function set_cached( $addons ) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @since 3.06 |
||
| 185 | */ |
||
| 186 | public function reset_cached() { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @since 3.06 |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | public function error_for_license() { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @since 3.06 |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | public function get_error_from_response( $addons = array() ) { |
||
| 219 | } |
||
| 220 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.