| Conditions | 8 |
| Paths | 11 |
| Total Lines | 69 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 87 | public function output( $args = array(), $widget_args = array(), $content = '' ) { |
||
| 88 | |||
| 89 | // Do we have a payment form? |
||
| 90 | if ( empty( $args['form'] ) && empty( $args['item'] ) ) { |
||
| 91 | return aui()->alert( |
||
|
|
|||
| 92 | array( |
||
| 93 | 'type' => 'warning', |
||
| 94 | 'content' => __( 'No payment form or item selected', 'invoicing' ), |
||
| 95 | ) |
||
| 96 | ); |
||
| 97 | |||
| 98 | } |
||
| 99 | |||
| 100 | $defaults = array( |
||
| 101 | 'item' => '', |
||
| 102 | 'button' => __( 'Buy Now', 'invoicing' ), |
||
| 103 | 'form' => '', |
||
| 104 | ); |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Parse incoming $args into an array and merge it with $defaults |
||
| 108 | */ |
||
| 109 | $args = wp_parse_args( $args, $defaults ); |
||
| 110 | |||
| 111 | // Payment form? |
||
| 112 | if ( ! empty( $args['form'] ) ) { |
||
| 113 | |||
| 114 | // Ensure that it is published. |
||
| 115 | if ( 'publish' != get_post_status( $args['form'] ) ) { |
||
| 116 | return aui()->alert( |
||
| 117 | array( |
||
| 118 | 'type' => 'warning', |
||
| 119 | 'content' => __( 'This payment form is no longer active', 'invoicing' ), |
||
| 120 | ) |
||
| 121 | ); |
||
| 122 | } |
||
| 123 | |||
| 124 | $attrs = array( |
||
| 125 | 'form' => $args['form'] |
||
| 126 | ); |
||
| 127 | |||
| 128 | } else { |
||
| 129 | |||
| 130 | // Ensure that it is published. |
||
| 131 | if ( 'publish' != get_post_status( $args['item'] ) ) { |
||
| 132 | return aui()->alert( |
||
| 133 | array( |
||
| 134 | 'type' => 'warning', |
||
| 135 | 'content' => __( 'This item is no longer active', 'invoicing' ), |
||
| 136 | ) |
||
| 137 | ); |
||
| 138 | } |
||
| 139 | |||
| 140 | $attrs = array( |
||
| 141 | 'item' => $args['item'] |
||
| 142 | ); |
||
| 143 | |||
| 144 | } |
||
| 145 | |||
| 146 | $label = ! empty( $args['button'] ) ? sanitize_text_field( $args['button'] ) : __( 'Buy Now', 'invoicing' ); |
||
| 147 | $attr = ''; |
||
| 148 | |||
| 149 | foreach( $attrs as $key => $val ) { |
||
| 150 | $key = sanitize_text_field( $key ); |
||
| 151 | $val = esc_attr( $val ); |
||
| 152 | $attr .= " $key='$val' "; |
||
| 153 | } |
||
| 154 | |||
| 155 | return "<button class='btn btn-primary getpaid-payment-button' type='button' $attr>$label</button>"; |
||
| 156 | |||
| 160 |