| Conditions | 7 |
| Paths | 6 |
| Total Lines | 71 |
| Code Lines | 43 |
| Lines | 12 |
| Ratio | 16.9 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 27 | function wpbo_aw_settings( $settings ) { |
||
| 28 | |||
| 29 | require( WPBO_PATH . 'includes/providers/aweber/class-titan-aweber.php' ); |
||
| 30 | |||
| 31 | /* Avoid calling the Aweber API when not on its settings page */ |
||
| 32 | if ( ! isset( $_GET['tab'] ) || 'aweber' != $_GET['tab'] ) { |
||
| 33 | $lists = array(); |
||
| 34 | } else { |
||
| 35 | $lists = wpbo_aw_get_lists(); |
||
| 36 | } |
||
| 37 | |||
| 38 | if( !is_array( $lists ) || empty( $lists ) ) { |
||
| 39 | |||
| 40 | $list_id = array( |
||
| 41 | 'name' => __( 'List ID', 'wpbo' ), |
||
| 42 | 'id' => 'aw_list_id', |
||
| 43 | 'type' => 'text', |
||
| 44 | 'default' => '', |
||
| 45 | 'desc' => __( 'Input your authorization code and save if you want to see a dropdown of all your lists. If you already entered your authorization code and the dropdown did not appear pleas refresh the page.', 'wpbo' ) |
||
| 46 | ); |
||
| 47 | |||
| 48 | } else { |
||
| 49 | |||
| 50 | if( is_array( $lists ) ) { |
||
| 51 | |||
| 52 | $opts[''] = __( 'Please select...', 'wpbo' ); |
||
|
|
|||
| 53 | |||
| 54 | foreach( $lists as $key => $list ) { |
||
| 55 | $opts[$list['id']] = $list['name']; |
||
| 56 | } |
||
| 57 | |||
| 58 | $list_id = array( |
||
| 59 | 'name' => __( 'List ID', 'wpbo' ), |
||
| 60 | 'id' => 'aw_list_id', |
||
| 61 | 'type' => 'select', |
||
| 62 | 'options' => $opts, |
||
| 63 | 'default' => '' |
||
| 64 | ); |
||
| 65 | |||
| 66 | View Code Duplication | } else { |
|
| 67 | |||
| 68 | $list_id = array( |
||
| 69 | 'name' => __( 'List ID', 'wpbo' ), |
||
| 70 | 'id' => 'aw_list_id', |
||
| 71 | 'type' => 'text', |
||
| 72 | 'default' => '', |
||
| 73 | 'desc' => __( 'Input your API key and save if you want to see a dropdown of all your lists.', 'wpbo' ), |
||
| 74 | 'default' => '' |
||
| 75 | ); |
||
| 76 | |||
| 77 | } |
||
| 78 | |||
| 79 | } |
||
| 80 | |||
| 81 | $settings['aweber'] = array( |
||
| 82 | 'name' => __( 'AWeber', 'wpbo' ), |
||
| 83 | 'options' => array( |
||
| 84 | array( |
||
| 85 | 'name' => __( 'Authorization Code', 'wpbo' ), |
||
| 86 | 'id' => 'aw_auth_code', |
||
| 87 | 'type' => 'aweber', |
||
| 88 | 'desc' => sprintf( __( 'AWeber requires you to authenticate this plugin with your account for security reasons. If you are not sure how to do it, please <a href="#" target="_blank">watch our video tutorial</a>.', '' ), esc_url( 'http://youtu.be/c448gBbWlkg' ) ), |
||
| 89 | 'default' => '' |
||
| 90 | ), |
||
| 91 | $list_id |
||
| 92 | ) |
||
| 93 | ); |
||
| 94 | |||
| 95 | return $settings; |
||
| 96 | |||
| 97 | } |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.