Conditions | 19 |
Paths | 42 |
Total Lines | 78 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
122 | public function process_item( $item, $selected_items, $submission ) { |
||
123 | |||
124 | if ( $item->has_variable_pricing() ) { |
||
125 | |||
126 | $selected_items = array_filter( |
||
127 | $selected_items, |
||
128 | function ( $selected_item ) use ( $item ) { |
||
129 | return (int) $selected_item['item_id'] === (int) $item->get_id(); |
||
130 | } |
||
131 | ); |
||
132 | |||
133 | if ( ! $item->is_required() && ! count( $selected_items ) ) { |
||
134 | return; |
||
135 | } |
||
136 | |||
137 | $price_options = $item->get_variable_prices(); |
||
138 | $price = current( $selected_items ); |
||
139 | |||
140 | $item->set_price_id( $price['price_id'] ); |
||
141 | $item->set_quantity( $price['quantity'] ); |
||
142 | |||
143 | $price = isset( $price_options[ $price['price_id'] ] ) ? $price_options[ $price['price_id'] ] : $price; |
||
144 | $item->set_price( (float) $price['amount'] ); |
||
145 | |||
146 | if ( isset( $price['is-recurring'] ) && 'yes' === $price['is-recurring'] ) { |
||
147 | if ( isset( $price['trial-interval'], $price['trial-period'] ) && $price['trial-interval'] > 0 ) { |
||
148 | $trial_interval = (int) $price['trial-interval']; |
||
149 | $trial_period = $price['trial-period']; |
||
150 | |||
151 | $item->set_is_free_trial( 1 ); |
||
152 | $item->set_trial_interval( $trial_interval ); |
||
153 | $item->set_trial_period( $trial_period ); |
||
154 | } |
||
155 | |||
156 | if ( isset( $price['recurring-interval'], $price['recurring-period'] ) && $price['recurring-interval'] > 0 ) { |
||
157 | $recurring_interval = (int) $price['recurring-interval']; |
||
158 | $recurring_period = $price['recurring-period']; |
||
159 | $recurring_limit = isset( $price['recurring-limit'] ) ? (int) $price['recurring-limit'] : 0; |
||
160 | |||
161 | $item->set_is_recurring( 1 ); |
||
162 | $item->set_recurring_interval( $recurring_interval ); |
||
163 | $item->set_recurring_period( $recurring_period ); |
||
164 | $item->set_recurring_limit( $recurring_limit ); |
||
165 | } |
||
166 | } |
||
167 | } else { |
||
168 | // Abort if this is an optional item and it has not been selected. |
||
169 | if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) { |
||
170 | return; |
||
171 | } |
||
172 | |||
173 | // (maybe) let customers change the quantities and prices. |
||
174 | if ( isset( $selected_items[ $item->get_id() ] ) ) { |
||
175 | |||
176 | // Maybe change the quantities. |
||
177 | if ( $item->allows_quantities() ) { |
||
178 | $item->set_quantity( (float) $selected_items[ $item->get_id() ]['quantity'] ); |
||
179 | } |
||
180 | |||
181 | // Maybe change the price. |
||
182 | if ( $item->user_can_set_their_price() ) { |
||
183 | $price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] ); |
||
184 | |||
185 | if ( $item->get_minimum_price() > $price ) { |
||
186 | throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $item->get_minimum_price() ) ) ); |
||
187 | } |
||
188 | |||
189 | $item->set_price( $price ); |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | |||
194 | if ( 0 == $item->get_quantity() ) { |
||
195 | return; |
||
196 | } |
||
197 | |||
198 | // Save the item. |
||
199 | $this->items[] = apply_filters( 'getpaid_payment_form_submission_processed_item', $item, $submission ); |
||
200 | } |
||
202 |