| Conditions | 12 | 
| Paths | 10 | 
| Total Lines | 61 | 
| Code Lines | 40 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 5 | ||
| Bugs | 0 | Features | 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 | ||
| 24 | public function onAfterInit() | ||
| 25 |     { | ||
| 26 | $owner = $this->owner; | ||
| 27 |         if ($owner->EnableGoogleAnalytics && $owner->currentOrder && (Director::isLive() || isset($_GET['testanalytics']))) { | ||
| 28 | $var = EcommerceConfig::get(CheckoutPageDataExtension::class, 'google_analytics_variable'); | ||
| 29 |             if ($var) { | ||
| 30 | $currencyUsedObject = $owner->currentOrder->CurrencyUsed(); | ||
| 31 |                 if ($currencyUsedObject) { | ||
| 32 | $currencyUsedString = $currencyUsedObject->Code; | ||
| 33 | } | ||
| 34 |                 if (empty($currencyUsedString)) { | ||
| 35 | $currencyUsedString = EcommerceCurrency::default_currency_code(); | ||
| 36 | } | ||
| 37 | $orderItems = $owner->currentOrder->OrderItems(); | ||
| 38 | $items = ''; | ||
| 39 |                 if (Config::inst()->get(CheckoutPageExtensionController::class, 'include_product_items')) { | ||
| 40 |                     foreach ($orderItems as $orderItem) { | ||
| 41 | /** @var Product $product */ | ||
| 42 | $product = Product::get_by_id($orderItem->BuyableID); | ||
| 43 | $sku = $product->InternalItemID ?: $product->ID; | ||
| 44 | $category = 'Unknown'; | ||
| 45 | $topParent = $product->TopParentGroup(); | ||
| 46 |                         if ($topParent) { | ||
| 47 | $category = $topParent->Title; | ||
| 48 | } | ||
| 49 |                         $orderItemName = preg_replace("#\r|\n#", '', (string) $orderItem->TableTitle()); | ||
| 50 |                         $category = preg_replace("#\r|\n#", '', (string) $category); | ||
| 51 | $items .= | ||
| 52 | 'ga( | ||
| 53 | \'ecommerce:addItem\', | ||
| 54 |                                 { | ||
| 55 | \'id\': \'' . $owner->currentOrder->ID . '\', | ||
| 56 | \'name\': \'' . $orderItemName . '\', | ||
| 57 | \'sku\': \'' . $sku . '\', | ||
| 58 | \'category\': \'' . $category . '\', | ||
| 59 | \'price\': \'' . $orderItem->CalculatedTotal . '\', | ||
| 60 | \'quantity\': \'' . $orderItem->Quantity . '\', | ||
| 61 | } | ||
| 62 | );'; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | $js = ' | ||
| 66 |                 const ecommerceFormForGA = document.getElementById("OrderForm_OrderForm"); | ||
| 67 | ecommerceFormForGA.addEventListener( | ||
| 68 | "submit", | ||
| 69 |                     function(e){ | ||
| 70 | console.log(e); | ||
| 71 | ' . $var . '(\'require\', \'ecommerce\'); | ||
| 72 | ' . $var . '( | ||
| 73 | \'ecommerce:addTransaction\', | ||
| 74 |                             { | ||
| 75 | \'id\': \'' . $owner->currentOrder->ID . '\', | ||
| 76 | \'revenue\': \'' . $owner->currentOrder->getSubTotal() . '\', | ||
| 77 | \'currency\': \'' . $currencyUsedString . '\' | ||
| 78 | } | ||
| 79 | ); | ||
| 80 | ' . $items . ' | ||
| 81 | ' . $var . '(\'ecommerce:send\'); | ||
| 82 | } | ||
| 83 | );'; | ||
| 84 | Requirements::customScript($js, 'GoogleAnalyticsEcommerce'); | ||
| 85 | } | ||
| 89 |