Conditions | 8 |
Paths | 8 |
Total Lines | 63 |
Code Lines | 35 |
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 |
||
152 | public function get_subscription() { |
||
153 | $test_subscription = filter_input( INPUT_POST, 'pronamic_pay_test_subscription', FILTER_VALIDATE_BOOLEAN ); |
||
154 | |||
155 | if ( ! $test_subscription ) { |
||
156 | return false; |
||
157 | } |
||
158 | |||
159 | $interval = filter_input( INPUT_POST, 'pronamic_pay_test_repeat_interval', FILTER_VALIDATE_INT ); |
||
160 | |||
161 | if ( empty( $interval ) ) { |
||
162 | return false; |
||
163 | } |
||
164 | |||
165 | $interval_period = filter_input( INPUT_POST, 'pronamic_pay_test_repeat_frequency', FILTER_SANITIZE_STRING ); |
||
166 | |||
167 | if ( empty( $interval_period ) ) { |
||
168 | return false; |
||
169 | } |
||
170 | |||
171 | // Ends on. |
||
172 | $ends_on = filter_input( INPUT_POST, 'pronamic_pay_ends_on', FILTER_SANITIZE_STRING ); |
||
173 | |||
174 | $times = null; |
||
175 | |||
176 | switch ( $ends_on ) { |
||
177 | case 'count': |
||
178 | $count = filter_input( INPUT_POST, 'pronamic_pay_ends_on_count', FILTER_VALIDATE_INT ); |
||
179 | |||
180 | if ( ! empty( $count ) ) { |
||
181 | $times = $count; |
||
182 | } |
||
183 | |||
184 | break; |
||
185 | case 'date': |
||
186 | $end_date = filter_input( INPUT_POST, 'pronamic_pay_ends_on_date', FILTER_SANITIZE_STRING ); |
||
187 | |||
188 | if ( ! empty( $end_date ) ) { |
||
189 | /* translators: 1: interval, 2: interval period */ |
||
190 | $interval_spec = sprintf( 'P%1$s%2$s', $interval, Core_Util::to_period( $interval_period ) ); |
||
191 | |||
192 | $period = new DatePeriod( |
||
193 | new DateTime(), |
||
194 | new DateInterval( $interval_spec ), |
||
195 | new DateTime( $end_date ) |
||
196 | ); |
||
197 | |||
198 | $times = iterator_count( $period ); |
||
199 | } |
||
200 | |||
201 | break; |
||
202 | } |
||
203 | |||
204 | // Subscription. |
||
205 | $subscription = new Subscription(); |
||
206 | |||
207 | $subscription->currency = $this->get_currency(); |
||
208 | $subscription->description = $this->get_description(); |
||
209 | $subscription->amount = $this->get_amount(); |
||
210 | $subscription->frequency = $times; |
||
211 | $subscription->interval = $interval; |
||
212 | $subscription->interval_period = Core_Util::to_period( $interval_period ); |
||
|
|||
213 | |||
214 | return $subscription; |
||
215 | } |
||
243 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.