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