| Conditions | 14 |
| Paths | 4096 |
| Total Lines | 157 |
| Lines | 22 |
| Ratio | 14.01 % |
| 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 |
||
| 180 | public function create_checkout( $ebi ) { |
||
| 181 | $countries = EDUAPI()->OData->Countries->Search()['value']; |
||
| 182 | |||
| 183 | $selectedCountry = 'SE'; |
||
| 184 | $selectedLocale = 'sv-SE'; |
||
| 185 | |||
| 186 | $invoiceCountry = $ebi->Customer['BillingInfo']['Country']; |
||
| 187 | if ( empty( $invoiceCountry ) ) { |
||
| 188 | $invoiceCountry = $ebi->Customer['Country']; |
||
| 189 | } |
||
| 190 | |||
| 191 | foreach ( $countries as $country ) { |
||
| 192 | if ( $invoiceCountry == $country['CountryName'] ) { |
||
| 193 | $selectedCountry = $country['CountryCode']; |
||
| 194 | if ( ! empty( $country['CultureName'] ) ) { |
||
| 195 | $selectedLocale = $country['CultureName']; |
||
| 196 | } |
||
| 197 | break; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | $booking_id = 0; |
||
| 202 | $programme_booking_id = 0; |
||
| 203 | |||
| 204 | $reference_id = 0; |
||
| 205 | |||
| 206 | $_event = null; |
||
| 207 | |||
| 208 | $eventName = ''; |
||
| 209 | |||
| 210 | View Code Duplication | if ( ! empty( $ebi->EventBooking['BookingId'] ) ) { |
|
| 211 | $booking_id = intval( $ebi->EventBooking['BookingId'] ); |
||
| 212 | $reference_id = $booking_id; |
||
| 213 | |||
| 214 | $_event = EDUAPI()->OData->Events->GetItem( $ebi->EventBooking['EventId'], null ); |
||
| 215 | |||
| 216 | $eventName = $_event['EventName']; |
||
| 217 | } |
||
| 218 | |||
| 219 | View Code Duplication | if ( ! empty( $ebi->EventBooking['ProgrammeBookingId'] ) ) { |
|
| 220 | $programme_booking_id = intval( $ebi->EventBooking['ProgrammeBookingId'] ); |
||
| 221 | $reference_id = $programme_booking_id; |
||
| 222 | |||
| 223 | $_event = EDUAPI()->OData->ProgrammeStarts->GetItem( $ebi->EventBooking['ProgrammeStartId'] ); |
||
| 224 | |||
| 225 | $eventName = $_event['ProgrammeStartName']; |
||
| 226 | } |
||
| 227 | |||
| 228 | $currency = EDU()->get_option( 'eduadmin-currency', 'SEK' ); |
||
| 229 | |||
| 230 | View Code Duplication | if ( 'no' !== $this->get_option( 'testrun', 'no' ) ) { |
|
| 231 | $wpConfig = new EduSveaWebPayTestConfig( $this ); |
||
| 232 | } else { |
||
| 233 | $wpConfig = new EduSveaWebPayProductionConfig( $this ); |
||
| 234 | } |
||
| 235 | |||
| 236 | $wpOrder = WebPay::checkout( $wpConfig ); |
||
| 237 | |||
| 238 | $orderRow = WebPayItem::orderRow(); |
||
| 239 | $orderRow->setName( $eventName ); |
||
| 240 | $orderRow->setQuantity( 1 ); |
||
| 241 | |||
| 242 | $vatPercent = ( $ebi->EventBooking['VatSum'] / $ebi->EventBooking['TotalPriceExVat'] ) * 100; |
||
| 243 | $orderRow->setVatPercent( $vatPercent ); |
||
| 244 | $orderRow->setAmountIncVat( (float) $ebi->EventBooking['TotalPriceIncVat'] ); |
||
| 245 | |||
| 246 | $customer = WebPayItem::companyCustomer(); |
||
| 247 | |||
| 248 | $customerName = ! empty( $ebi->Customer['BillingInfo']['InvoiceName'] ) ? $ebi->Customer['BillingInfo']['InvoiceName'] : $ebi->Customer['CustomerName']; |
||
| 249 | $streetAddress = ! empty( $ebi->Customer['BillingInfo']['Address'] ) ? $ebi->Customer['BillingInfo']['Address'] : $ebi->Customer['Address']; |
||
| 250 | $zipCode = ! empty( $ebi->Customer['BillingInfo']['Zip'] ) ? $ebi->Customer['BillingInfo']['Zip'] : $ebi->Customer['Zip']; |
||
| 251 | $city = $ebi->Customer['BillingInfo']['City'] ? $ebi->Customer['BillingInfo']['City'] : $ebi->Customer['City']; |
||
| 252 | $phone = $ebi->Customer['Phone']; |
||
| 253 | $email = ! empty( $ebi->Customer['BillingInfo']['Email'] ) ? $ebi->Customer['BillingInfo']['Email'] : $ebi->Customer['Email']; |
||
| 254 | |||
| 255 | $customer->setCompanyName( $customerName ); |
||
| 256 | $customer->setStreetAddress( $streetAddress ); |
||
| 257 | $customer->setZipCode( $zipCode ); |
||
| 258 | $customer->setLocality( $city ); |
||
| 259 | |||
| 260 | if ( ! empty( $phone ) ) { |
||
| 261 | $customer->setPhoneNumber( $phone ); |
||
| 262 | $phonePreset = WebPayItem::presetValue() |
||
| 263 | ->setTypeName( \Svea\WebPay\Checkout\Model\PresetValue::PHONE_NUMBER ) |
||
| 264 | ->setValue( $phone ) |
||
| 265 | ->setIsReadonly( false ); |
||
| 266 | $wpOrder->addPresetValue( $phonePreset ); |
||
| 267 | } |
||
| 268 | $customer->setEmail( $email ); |
||
| 269 | |||
| 270 | $zipPreset = WebPayItem::presetValue() |
||
| 271 | ->setTypeName( \Svea\WebPay\Checkout\Model\PresetValue::POSTAL_CODE ) |
||
| 272 | ->setValue( $zipCode ) |
||
| 273 | ->setIsReadonly( false ); |
||
| 274 | $wpOrder->addPresetValue( $zipPreset ); |
||
| 275 | |||
| 276 | $emailPreset = WebPayItem::presetValue() |
||
| 277 | ->setTypeName( \Svea\WebPay\Checkout\Model\PresetValue::EMAIL_ADDRESS ) |
||
| 278 | ->setValue( $email ) |
||
| 279 | ->setIsReadonly( false ); |
||
| 280 | $wpOrder->addPresetValue( $emailPreset ); |
||
| 281 | |||
| 282 | $current_url = esc_url( "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}" ); |
||
| 283 | |||
| 284 | $defaultThankYou = add_query_arg( |
||
| 285 | array( |
||
| 286 | 'edu-thankyou' => $reference_id, |
||
| 287 | 'svea' => '1', |
||
| 288 | 'booking_id' => $booking_id, |
||
| 289 | 'programme_booking_id' => $programme_booking_id, |
||
| 290 | 'edu-valid-form' => wp_create_nonce( 'edu-booking-confirm' ), |
||
| 291 | 'act' => 'paymentCompleted', |
||
| 292 | ), |
||
| 293 | @get_page_link( get_option( 'eduadmin-thankYouPage', '/' ) ) |
||
| 294 | ); |
||
| 295 | |||
| 296 | $defaultCancel = add_query_arg( |
||
| 297 | array( |
||
| 298 | 'edu-thankyou' => $reference_id, |
||
| 299 | 'svea' => '1', |
||
| 300 | 'booking_id' => $booking_id, |
||
| 301 | 'programme_booking_id' => $programme_booking_id, |
||
| 302 | 'status' => 'cancel' |
||
| 303 | ), |
||
| 304 | $current_url |
||
| 305 | ); |
||
| 306 | |||
| 307 | $defaultPushUrl = add_query_arg( |
||
| 308 | array( |
||
| 309 | 'edu-thankyou' => $reference_id, |
||
| 310 | 'svea' => '1', |
||
| 311 | 'booking_id' => $booking_id, |
||
| 312 | 'programme_booking_id' => $programme_booking_id, |
||
| 313 | 'svea_order_id' => '{checkout.order.uri}', |
||
| 314 | 'status' => 'push' |
||
| 315 | ), |
||
| 316 | $current_url |
||
| 317 | ); |
||
| 318 | |||
| 319 | $defaultTermsUrl = get_option( 'eduadmin-bookingTermsLink' ); |
||
| 320 | |||
| 321 | $wpBuild = $wpOrder |
||
| 322 | ->setCurrency( $currency ) |
||
| 323 | ->setCountryCode( $selectedCountry ) |
||
| 324 | ->setClientOrderNumber( $reference_id ) |
||
| 325 | ->addOrderRow( $orderRow ) |
||
| 326 | ->setLocale( $selectedLocale ) |
||
| 327 | ->setTermsUri( $defaultTermsUrl ) |
||
| 328 | ->setConfirmationUri( $defaultThankYou ) |
||
| 329 | ->setPushUri( $defaultPushUrl ) |
||
| 330 | ->setCheckoutUri( $defaultCancel ); // We have no "checkout"-url.. So we just cancel the booking instead. |
||
| 331 | $wpForm = $wpBuild->createOrder(); |
||
| 332 | |||
| 333 | EDU()->session['svea-order-id'] = $wpForm['OrderId']; |
||
| 334 | |||
| 335 | return $wpForm; |
||
| 336 | } |
||
| 337 | |||
| 437 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: