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