| Conditions | 16 |
| Paths | 6144 |
| Total Lines | 167 |
| Lines | 6 |
| Ratio | 3.59 % |
| 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 |
||
| 182 | public function create_checkout( $ebi ) { |
||
| 183 | $countries = EDUAPI()->OData->Countries->Search()['value']; |
||
| 184 | |||
| 185 | $selectedCountry = 'SE'; |
||
| 186 | $selectedLocale = 'sv-SE'; |
||
| 187 | |||
| 188 | $invoiceCountry = $ebi->Customer['BillingInfo']['Country']; |
||
| 189 | if ( empty( $invoiceCountry ) ) { |
||
| 190 | $invoiceCountry = $ebi->Customer['Country']; |
||
| 191 | } |
||
| 192 | |||
| 193 | foreach ( $countries as $country ) { |
||
| 194 | if ( $invoiceCountry == $country['CountryName'] ) { |
||
| 195 | $selectedCountry = $country['CountryCode']; |
||
| 196 | if ( ! empty( $country['CultureName'] ) ) { |
||
| 197 | $selectedLocale = $country['CultureName']; |
||
| 198 | } |
||
| 199 | break; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | $booking_id = 0; |
||
| 204 | $programme_booking_id = 0; |
||
| 205 | |||
| 206 | $reference_id = 0; |
||
| 207 | |||
| 208 | $_event = null; |
||
| 209 | |||
| 210 | $eventName = ''; |
||
| 211 | |||
| 212 | $locationAddress = ''; |
||
| 213 | $locationCountry = ''; |
||
| 214 | $locationPostalCode = ''; |
||
| 215 | |||
| 216 | if ( ! empty( $ebi->EventBooking['BookingId'] ) ) { |
||
| 217 | $booking_id = intval( $ebi->EventBooking['BookingId'] ); |
||
| 218 | $reference_id = $booking_id; |
||
| 219 | |||
| 220 | $_event = EDUAPI()->OData->Events->GetItem( $ebi->EventBooking['EventId'], null, "LocationAddress" ); |
||
| 221 | |||
| 222 | $eventName = $_event['EventName']; |
||
| 223 | |||
| 224 | if ( ! empty( $_event['LocationAddress'] ) && $_event['LocationAdress'] != null ) { |
||
| 225 | $locationAddress = $_event['LocationAddress']['Address']; |
||
| 226 | $locationCountry = $_event['LocationAddress']['Country']; |
||
| 227 | $locationPostalCode = $_event['LocationAddress']['AddressZip']; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | if ( ! empty( $ebi->EventBooking['ProgrammeBookingId'] ) ) { |
||
| 232 | $programme_booking_id = intval( $ebi->EventBooking['ProgrammeBookingId'] ); |
||
| 233 | $reference_id = $programme_booking_id; |
||
| 234 | |||
| 235 | $_event = EDUAPI()->OData->ProgrammeStarts->GetItem( $ebi->EventBooking['ProgrammeStartId'] ); |
||
| 236 | |||
| 237 | $eventName = $_event['ProgrammeStartName']; |
||
| 238 | } |
||
| 239 | |||
| 240 | $currency = EDU()->get_option( 'eduadmin-currency', 'SEK' ); |
||
| 241 | |||
| 242 | View Code Duplication | if ( 'no' !== $this->get_option( 'testrun', 'no' ) ) { |
|
| 243 | $wpConfig = new EduSveaWebPayTestConfig( $this ); |
||
| 244 | } else { |
||
| 245 | $wpConfig = new EduSveaWebPayProductionConfig( $this ); |
||
| 246 | } |
||
| 247 | |||
| 248 | $wpOrder = WebPay::checkout( $wpConfig ); |
||
| 249 | |||
| 250 | $orderRow = WebPayItem::orderRow(); |
||
| 251 | $orderRow->setName( $eventName ); |
||
| 252 | $orderRow->setQuantity( 1 ); |
||
| 253 | |||
| 254 | $vatPercent = ( $ebi->EventBooking['VatSum'] / $ebi->EventBooking['TotalPriceExVat'] ) * 100; |
||
| 255 | $orderRow->setVatPercent( $vatPercent ); |
||
| 256 | $orderRow->setAmountIncVat( (float) $ebi->EventBooking['TotalPriceIncVat'] ); |
||
| 257 | |||
| 258 | $customer = WebPayItem::companyCustomer(); |
||
| 259 | |||
| 260 | $customerName = ! empty( $ebi->Customer['BillingInfo']['InvoiceName'] ) ? $ebi->Customer['BillingInfo']['InvoiceName'] : $ebi->Customer['CustomerName']; |
||
| 261 | $streetAddress = ! empty( $ebi->Customer['BillingInfo']['Address'] ) ? $ebi->Customer['BillingInfo']['Address'] : $ebi->Customer['Address']; |
||
| 262 | $zipCode = ! empty( $ebi->Customer['BillingInfo']['Zip'] ) ? $ebi->Customer['BillingInfo']['Zip'] : $ebi->Customer['Zip']; |
||
| 263 | $city = $ebi->Customer['BillingInfo']['City'] ? $ebi->Customer['BillingInfo']['City'] : $ebi->Customer['City']; |
||
| 264 | $phone = $ebi->Customer['Phone']; |
||
| 265 | $email = ! empty( $ebi->Customer['BillingInfo']['Email'] ) ? $ebi->Customer['BillingInfo']['Email'] : $ebi->Customer['Email']; |
||
| 266 | |||
| 267 | $customer->setCompanyName( $customerName ); |
||
| 268 | $customer->setStreetAddress( $streetAddress ); |
||
| 269 | $customer->setZipCode( $zipCode ); |
||
| 270 | $customer->setLocality( $city ); |
||
| 271 | |||
| 272 | if ( ! empty( $phone ) ) { |
||
| 273 | $customer->setPhoneNumber( $phone ); |
||
| 274 | $phonePreset = WebPayItem::presetValue() |
||
| 275 | ->setTypeName( \Svea\WebPay\Checkout\Model\PresetValue::PHONE_NUMBER ) |
||
| 276 | ->setValue( $phone ) |
||
| 277 | ->setIsReadonly( false ); |
||
| 278 | $wpOrder->addPresetValue( $phonePreset ); |
||
| 279 | } |
||
| 280 | $customer->setEmail( $email ); |
||
| 281 | |||
| 282 | $zipPreset = WebPayItem::presetValue() |
||
| 283 | ->setTypeName( \Svea\WebPay\Checkout\Model\PresetValue::POSTAL_CODE ) |
||
| 284 | ->setValue( $zipCode ) |
||
| 285 | ->setIsReadonly( false ); |
||
| 286 | $wpOrder->addPresetValue( $zipPreset ); |
||
| 287 | |||
| 288 | $emailPreset = WebPayItem::presetValue() |
||
| 289 | ->setTypeName( \Svea\WebPay\Checkout\Model\PresetValue::EMAIL_ADDRESS ) |
||
| 290 | ->setValue( $email ) |
||
| 291 | ->setIsReadonly( false ); |
||
| 292 | $wpOrder->addPresetValue( $emailPreset ); |
||
| 293 | |||
| 294 | $current_url = esc_url( "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}" ); |
||
| 295 | |||
| 296 | $defaultThankYou = 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 | 'edu-valid-form' => wp_create_nonce( 'edu-booking-confirm' ), |
||
| 303 | 'act' => 'paymentCompleted', |
||
| 304 | ), |
||
| 305 | @get_page_link( get_option( 'eduadmin-thankYouPage', '/' ) ) |
||
| 306 | ); |
||
| 307 | |||
| 308 | $defaultCancel = add_query_arg( |
||
| 309 | array( |
||
| 310 | 'edu-thankyou' => $reference_id, |
||
| 311 | 'svea' => '1', |
||
| 312 | 'booking_id' => $booking_id, |
||
| 313 | 'programme_booking_id' => $programme_booking_id, |
||
| 314 | 'status' => 'cancel' |
||
| 315 | ), |
||
| 316 | $current_url |
||
| 317 | ); |
||
| 318 | |||
| 319 | $defaultPushUrl = add_query_arg( |
||
| 320 | array( |
||
| 321 | 'edu-thankyou' => $reference_id, |
||
| 322 | 'svea' => '1', |
||
| 323 | 'booking_id' => $booking_id, |
||
| 324 | 'programme_booking_id' => $programme_booking_id, |
||
| 325 | 'svea_order_id' => '{checkout.order.uri}', |
||
| 326 | 'status' => 'push' |
||
| 327 | ), |
||
| 328 | $current_url |
||
| 329 | ); |
||
| 330 | |||
| 331 | $defaultTermsUrl = get_option( 'eduadmin-bookingTermsLink' ); |
||
| 332 | |||
| 333 | $wpBuild = $wpOrder |
||
| 334 | ->setCurrency( $currency ) |
||
| 335 | ->setCountryCode( $selectedCountry ) |
||
| 336 | ->setClientOrderNumber( $reference_id ) |
||
| 337 | ->addOrderRow( $orderRow ) |
||
| 338 | ->setLocale( $selectedLocale ) |
||
| 339 | ->setTermsUri( $defaultTermsUrl ) |
||
| 340 | ->setConfirmationUri( $defaultThankYou ) |
||
| 341 | ->setPushUri( $defaultPushUrl ) |
||
| 342 | ->setCheckoutUri( $defaultCancel ); // We have no "checkout"-url.. So we just cancel the booking instead. |
||
| 343 | $wpForm = $wpBuild->createOrder(); |
||
| 344 | |||
| 345 | EDU()->session['svea-order-id'] = $wpForm['OrderId']; |
||
| 346 | |||
| 347 | return $wpForm; |
||
| 348 | } |
||
| 349 | |||
| 444 |
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: