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:
| 1 | <?php |
||
| 6 | class Pods_CLI_Command extends WP_CLI_Command { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Add a pod item. |
||
| 10 | * |
||
| 11 | * ## OPTIONS |
||
| 12 | * |
||
| 13 | * --pod=<pod> |
||
| 14 | * : The pod name. |
||
| 15 | * |
||
| 16 | * --<field>=<value> |
||
| 17 | * : The field => value pair(s) to save. |
||
| 18 | * |
||
| 19 | * ## EXAMPLES |
||
| 20 | * |
||
| 21 | * wp pods add --pod=my_pod --my_field_name1=Value --my_field_name2="Another Value" |
||
| 22 | */ |
||
| 23 | public function add( $args, $assoc_args ) { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Save a pod item. |
||
| 58 | * |
||
| 59 | * ## OPTIONS |
||
| 60 | * |
||
| 61 | * --pod=<pod> |
||
| 62 | * : The pod name. |
||
| 63 | * |
||
| 64 | * [--item=<item>] |
||
| 65 | * : The item to save for, it is not used for a settings pod. |
||
| 66 | * |
||
| 67 | * --<field>=<value> |
||
| 68 | * : The field => value pair(s) to save. |
||
| 69 | * |
||
| 70 | * ## EXAMPLES |
||
| 71 | * |
||
| 72 | * wp pods save --pod=my_pod --item=123 --my_field_name1=Value2 --my_field_name2="Another Value2" |
||
| 73 | * wp pods save --pod=my_settings_pod --my_option_field_name1=Value --my_option_field_name2="Another Value2" |
||
| 74 | */ |
||
| 75 | public function save( $args, $assoc_args ) { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Duplicate a pod item. |
||
| 119 | * |
||
| 120 | * ## OPTIONS |
||
| 121 | * |
||
| 122 | * --pod=<pod> |
||
| 123 | * : The pod name. |
||
| 124 | * |
||
| 125 | * --item=<item> |
||
| 126 | * : The pod item to delete. |
||
| 127 | * |
||
| 128 | * ## EXAMPLES |
||
| 129 | * |
||
| 130 | * wp pods duplicate --pod=my_pod --item=123 |
||
| 131 | */ |
||
| 132 | public function duplicate( $args, $assoc_args ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Delete a pod item. |
||
| 163 | * |
||
| 164 | * ## OPTIONS |
||
| 165 | * |
||
| 166 | * --pod=<pod> |
||
| 167 | * : The pod name. |
||
| 168 | * |
||
| 169 | * --item=<item> |
||
| 170 | * : The pod item to delete. |
||
| 171 | * |
||
| 172 | * ## EXAMPLES |
||
| 173 | * |
||
| 174 | * wp pods delete --pod=my_pod --item=123 |
||
| 175 | */ |
||
| 176 | public function delete( $args, $assoc_args ) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Export a single pod item to a file. |
||
| 206 | * |
||
| 207 | * ## OPTIONS |
||
| 208 | * |
||
| 209 | * --pod=<pod> |
||
| 210 | * : The pod name. |
||
| 211 | * |
||
| 212 | * --file=<file> |
||
| 213 | * : The file to save to including path (defaults to current path). |
||
| 214 | * |
||
| 215 | * [--item=<item>] |
||
| 216 | * : The item to save for, it is not used for a settings pod. |
||
| 217 | * |
||
| 218 | * [--fields=<fields>] |
||
| 219 | * : The comma-separated list of fields to export (defaults to all fields). |
||
| 220 | * |
||
| 221 | * [--depth=<depth>] |
||
| 222 | * : The depth of related objects to recursively export (default is 1 level deep, only returns IDs for related objects). |
||
| 223 | * |
||
| 224 | * ## EXAMPLES |
||
| 225 | * |
||
| 226 | * wp pods export-item --pod=my_pod --item=123 --file="item-data.json" |
||
| 227 | * wp pods export-item --pod=my_pod --item=123 --file="/path/to/item-data.json" |
||
| 228 | * wp pods export-item --pod=my_pod --item=123 --file="item-data.json" --fields="ID,post_title,post_content,my_field_name1,my_field_name2" |
||
| 229 | * wp pods export-item --pod=my_pod --item=123 --file="item-data.json" --depth=2 |
||
| 230 | * |
||
| 231 | * @subcommand export-item |
||
| 232 | */ |
||
| 233 | public function export_item( $args, $assoc_args ) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Export all pod items to a file. |
||
| 288 | * |
||
| 289 | * ## OPTIONS |
||
| 290 | * |
||
| 291 | * --pod=<pod> |
||
| 292 | * : The pod name. |
||
| 293 | * |
||
| 294 | * --file=<file> |
||
| 295 | * : The file to save to including path (defaults to current path). |
||
| 296 | * |
||
| 297 | * [--fields=<fields>] |
||
| 298 | * : The comma-separated list of fields to export (defaults to all fields). |
||
| 299 | * |
||
| 300 | * [--depth=<depth>] |
||
| 301 | * : The depth of related objects to recursively export (default is 1 level deep, only returns IDs for related objects). |
||
| 302 | * |
||
| 303 | * [--params=<params>] |
||
| 304 | * : The params to pass into the Pods::find() call, provided in arg1=A&arg2=B or JSON format (default is limit=-1). |
||
| 305 | * |
||
| 306 | * ## EXAMPLES |
||
| 307 | * |
||
| 308 | * wp pods export --pod=my_pod --file="items.json" |
||
| 309 | * wp pods export --pod=my_pod --file="/path/to/items.json" |
||
| 310 | * wp pods export --pod=my_pod --file="items.json" --fields="ID,post_title,post_content,my_field_name1,my_field_name2" |
||
| 311 | * wp pods export --pod=my_pod --file="items.json" --depth=2 |
||
| 312 | * wp pods export --pod=my_pod --file="items.json" --params="{\"limit\":10,\"orderby\":\"t.ID DESC\"}" |
||
| 313 | * wp pods export --pod=my_pod --file="items.json" --params="limit=10&orderby=t.ID DESC" |
||
| 314 | */ |
||
| 315 | public function export( $args, $assoc_args ) { |
||
| 373 | |||
| 374 | } |
||
| 375 | |||
| 377 |
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.