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 WC_API_Coupons 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 WC_API_Coupons, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class WC_API_Coupons extends WC_API_Resource { |
||
| 19 | |||
| 20 | /** @var string $base the route base */ |
||
| 21 | protected $base = '/coupons'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Register the routes for this class |
||
| 25 | * |
||
| 26 | * GET /coupons |
||
| 27 | * GET /coupons/count |
||
| 28 | * GET /coupons/<id> |
||
| 29 | * |
||
| 30 | * @since 2.1 |
||
| 31 | * @param array $routes |
||
| 32 | * @return array |
||
| 33 | */ |
||
| 34 | public function register_routes( $routes ) { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Get all coupons |
||
| 61 | * |
||
| 62 | * @since 2.1 |
||
| 63 | * @param string $fields |
||
| 64 | * @param array $filter |
||
| 65 | * @param int $page |
||
| 66 | * @return array |
||
| 67 | */ |
||
| 68 | public function get_coupons( $fields = null, $filter = array(), $page = 1 ) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get the coupon for the given ID |
||
| 91 | * |
||
| 92 | * @since 2.1 |
||
| 93 | * @param int $id the coupon ID |
||
| 94 | * @param string $fields fields to include in response |
||
| 95 | * @return array|WP_Error |
||
| 96 | */ |
||
| 97 | public function get_coupon( $id, $fields = null ) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get the total number of coupons |
||
| 143 | * |
||
| 144 | * @since 2.1 |
||
| 145 | * @param array $filter |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | View Code Duplication | public function get_coupons_count( $filter = array() ) { |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Get the coupon for the given code |
||
| 160 | * |
||
| 161 | * @since 2.1 |
||
| 162 | * @param string $code the coupon code |
||
| 163 | * @param string $fields fields to include in response |
||
| 164 | * @return int|WP_Error |
||
| 165 | */ |
||
| 166 | View Code Duplication | public function get_coupon_by_code( $code, $fields = null ) { |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Create a coupon |
||
| 179 | * |
||
| 180 | * @TODO implement in 2.2 |
||
| 181 | * @param array $data |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public function create_coupon( $data ) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Edit a coupon |
||
| 191 | * |
||
| 192 | * @TODO implement in 2.2 |
||
| 193 | * @param int $id the coupon ID |
||
| 194 | * @param array $data |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | public function edit_coupon( $id, $data ) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Delete a coupon |
||
| 209 | * |
||
| 210 | * @TODO enable along with PUT/POST in 2.2 |
||
| 211 | * @param int $id the coupon ID |
||
| 212 | * @param bool $force true to permanently delete coupon, false to move to trash |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | public function delete_coupon( $id, $force = false ) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Helper method to get coupon post objects |
||
| 227 | * |
||
| 228 | * @since 2.1 |
||
| 229 | * @param array $args request arguments for filtering query |
||
| 230 | * @return WP_Query |
||
| 231 | */ |
||
| 232 | private function query_coupons( $args ) { |
||
| 245 | |||
| 246 | } |
||
| 247 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.