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 PodsAPI_CLI_Command 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 PodsAPI_CLI_Command, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class PodsAPI_CLI_Command extends WP_CLI_Command { |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Add a pod. |
||
| 9 | * |
||
| 10 | * ## OPTIONS |
||
| 11 | * |
||
| 12 | * --name=<name> |
||
| 13 | * : The pod name, the default type is post_type. |
||
| 14 | * |
||
| 15 | * [--<field>=<value>] |
||
| 16 | * : The field => value pair(s) to save. |
||
| 17 | * |
||
| 18 | * ## EXAMPLES |
||
| 19 | * |
||
| 20 | * wp pods-api add-pod --name=book |
||
| 21 | * wp pods-api add-pod --name=book --type=post_type |
||
| 22 | * wp pods-api add-pod --name=book --type=post_type --label=Books --singular_label=Book |
||
| 23 | * wp pods-api add-pod --name=genre --type=taxonomy --label=Genres --singular_label=Genre |
||
| 24 | * |
||
| 25 | * @subcommand add-pod |
||
| 26 | */ |
||
| 27 | public function add_pod( $args, $assoc_args ) { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Save a pod. |
||
| 55 | * |
||
| 56 | * ## OPTIONS |
||
| 57 | * |
||
| 58 | * --name=<name> |
||
| 59 | * : The pod name. |
||
| 60 | * |
||
| 61 | * [--<field>=<value>] |
||
| 62 | * : The field => value pair(s) to save. |
||
| 63 | * |
||
| 64 | * ## EXAMPLES |
||
| 65 | * |
||
| 66 | * wp pods-api save-pod --name=book --type=post_type |
||
| 67 | * wp pods-api save-pod --name=book --type=post_type --label=Books --singular_label=Book |
||
| 68 | * wp pods-api save-pod --name=genre --type=taxonomy --label=Genres --singular_label=Genre |
||
| 69 | * |
||
| 70 | * @subcommand save-pod |
||
| 71 | */ |
||
| 72 | View Code Duplication | public function save_pod( $args, $assoc_args ) { |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Duplicate a pod. |
||
| 106 | * |
||
| 107 | * ## OPTIONS |
||
| 108 | * |
||
| 109 | * --name=<name> |
||
| 110 | * : The pod name. |
||
| 111 | * |
||
| 112 | * [--new_name=<new_name>] |
||
| 113 | * : The new pod name (defaults to a unique non-conflicting name). |
||
| 114 | * |
||
| 115 | * [--<field>=<value>] |
||
| 116 | * : The field => value pair(s) to save. |
||
| 117 | * |
||
| 118 | * ## EXAMPLES |
||
| 119 | * |
||
| 120 | * wp pods-api duplicate-pod --name=book |
||
| 121 | * wp pods-api duplicate-pod --name=book --new_name=book2 |
||
| 122 | * wp pods-api duplicate-pod --name=book --new_name=book2 --label="Books Two" --singular_label="Book Two" |
||
| 123 | * |
||
| 124 | * @subcommand duplicate-pod |
||
| 125 | */ |
||
| 126 | View Code Duplication | public function duplicate_pod( $args, $assoc_args ) { |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Reset a pod which will delete all pod items. |
||
| 160 | * |
||
| 161 | * ## OPTIONS |
||
| 162 | * |
||
| 163 | * --name=<name> |
||
| 164 | * : The pod name. |
||
| 165 | * |
||
| 166 | * ## EXAMPLES |
||
| 167 | * |
||
| 168 | * wp pods-api reset-pod --name=book |
||
| 169 | * |
||
| 170 | * @subcommand reset-pod |
||
| 171 | */ |
||
| 172 | public function reset_pod( $args, $assoc_args ) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Delete a pod, which will NOT delete all pod items by default. |
||
| 200 | * |
||
| 201 | * ## OPTIONS |
||
| 202 | * |
||
| 203 | * --name=<name> |
||
| 204 | * : The pod name. |
||
| 205 | * |
||
| 206 | * [--delete-all] |
||
| 207 | * : Delete all pod content for the pod. |
||
| 208 | * |
||
| 209 | * ## EXAMPLES |
||
| 210 | * |
||
| 211 | * wp pods-api delete-pod --name=book |
||
| 212 | * wp pods-api delete-pod --name=book --delete_all |
||
| 213 | * |
||
| 214 | * @subcommand delete-pod |
||
| 215 | */ |
||
| 216 | public function delete_pod( $args, $assoc_args ) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Activate a component. |
||
| 251 | * |
||
| 252 | * ## OPTIONS |
||
| 253 | * |
||
| 254 | * --component=<component> |
||
| 255 | * : The component identifier. |
||
| 256 | * |
||
| 257 | * ## EXAMPLES |
||
| 258 | * |
||
| 259 | * wp pods-api activate-component --component=templates |
||
| 260 | * |
||
| 261 | * @subcommand activate-component |
||
| 262 | */ |
||
| 263 | View Code Duplication | public function activate_component( $args, $assoc_args ) { |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Deactivate a component. |
||
| 287 | * |
||
| 288 | * ## OPTIONS |
||
| 289 | * |
||
| 290 | * --component=<component> |
||
| 291 | * : The component identifier. |
||
| 292 | * |
||
| 293 | * ## EXAMPLES |
||
| 294 | * |
||
| 295 | * wp pods-api deactivate-component --component=templates |
||
| 296 | * |
||
| 297 | * @subcommand deactivate-component |
||
| 298 | */ |
||
| 299 | View Code Duplication | public function deactivate_component( $args, $assoc_args ) { |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Clear the Pods cache. |
||
| 323 | * |
||
| 324 | * ## EXAMPLES |
||
| 325 | * |
||
| 326 | * wp pods-api clear-cache |
||
| 327 | * |
||
| 328 | * @subcommand clear-cache |
||
| 329 | */ |
||
| 330 | public function cache_clear() { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Export a Pods Package to a file. |
||
| 340 | * |
||
| 341 | * ## OPTIONS |
||
| 342 | * |
||
| 343 | * --file=<file> |
||
| 344 | * : The file to save to including path (defaults to current path). |
||
| 345 | * |
||
| 346 | * [--pods=<pods>] |
||
| 347 | * : A comma-separated list of Pods IDs to export (default is all Pods). |
||
| 348 | * |
||
| 349 | * [--templates=<templates>] |
||
| 350 | * : A comma-separated list of Pod Template IDs to export (default is all Templates). |
||
| 351 | * |
||
| 352 | * [--pages=<pages>] |
||
| 353 | * : A comma-separated list of Pod Page IDs to export (default is all Pod Pages). |
||
| 354 | * |
||
| 355 | * ## EXAMPLES |
||
| 356 | * |
||
| 357 | * wp pods-api export-pod --file="pods-package.json" |
||
| 358 | * wp pods-api export-pod --file="pods-package.json" --pods="book,genre" |
||
| 359 | * wp pods-api export-pod --file="/path/to/pods-package.json" --pods="book,genre" |
||
| 360 | * wp pods-api export-pod --templates="book-single,book-list" --file="pods-package.json" |
||
| 361 | * wp pods-api export-pod --pod-pages="books,books/*" --file="pods-package.json" |
||
| 362 | * wp pods-api export-pod --pods="book,genre" --templates="book-single,book-list" --pod-pages="books,books/*" --file="pods-package.json" |
||
| 363 | * |
||
| 364 | * @subcommand export-pod |
||
| 365 | */ |
||
| 366 | public function export_pod( $args, $assoc_args ) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Import a Pods Package from a file. |
||
| 422 | * |
||
| 423 | * ## OPTIONS |
||
| 424 | * |
||
| 425 | * --file=<file> |
||
| 426 | * : The file to save to including path (defaults to current path). |
||
| 427 | * |
||
| 428 | * [--replace] |
||
| 429 | * : Overwrite imported items if they already exist (defaults to false). |
||
| 430 | * |
||
| 431 | * ## EXAMPLES |
||
| 432 | * |
||
| 433 | * wp pods-api import-pod --file="pods-package.json" |
||
| 434 | * wp pods-api import-pod --file="/path/to/pods-package.json" |
||
| 435 | * wp pods-api import-pod --file="pods-package.json" --replace |
||
| 436 | * |
||
| 437 | * @subcommand import-pod |
||
| 438 | */ |
||
| 439 | function import_pod( $args, $assoc_args ) { |
||
| 482 | |||
| 483 | } |
||
| 484 | |||
| 486 |
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.