| Conditions | 1 |
| Paths | 1 |
| Total Lines | 85 |
| Code Lines | 76 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 65 | function execute() { |
||
| 66 | echo "<script src='https://cdn.pagantis.com/js/pg-v2/sdk.js'></script>"; |
||
| 67 | echo '<script>'; |
||
| 68 | echo ' function findPriceSelector()'; |
||
| 69 | echo ' { '; |
||
| 70 | echo ' var priceDOM = document.getElementById("our_price_display");'; |
||
| 71 | echo ' if (priceDOM != null) {'; |
||
| 72 | echo ' return \'#our_price_display\';'; |
||
| 73 | echo ' } else { '; |
||
| 74 | echo ' priceDOM = document.querySelector(".current-price span[itemprop=price]");'; |
||
| 75 | echo ' if (priceDOM != null) { '; |
||
| 76 | echo ' return ".current-price span[itemprop=price]"; '; |
||
| 77 | echo ' }'; |
||
| 78 | echo ' }'; |
||
| 79 | echo ' return \'default\';'; |
||
| 80 | echo ' }'; |
||
| 81 | |||
| 82 | echo ' function findQuantitySelector()'; |
||
| 83 | echo ' {'; |
||
| 84 | echo ' var quantityDOM = document.getElementById("quantity_wanted");'; |
||
| 85 | echo ' if (quantityDOM != null) {'; |
||
| 86 | echo ' return \'#quantity_wanted\';'; |
||
| 87 | echo ' }'; |
||
| 88 | echo ' return \'default\';'; |
||
| 89 | echo ' }'; |
||
| 90 | |||
| 91 | echo ' function loadSimulator()'; |
||
| 92 | echo ' {'; |
||
| 93 | echo ' if (typeof pgSDK != \'undefined\') {'; |
||
| 94 | echo ' var price = null;'; |
||
| 95 | echo ' var quantity = null;'; |
||
| 96 | echo ' var positionSelector = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR']. '\';'; |
||
| 97 | echo ' var priceSelector = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR']. '\';'; |
||
| 98 | echo ' var quantitySelector = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR']. '\';'; |
||
| 99 | |||
| 100 | echo ' if (positionSelector === \'default\') {'; |
||
| 101 | echo ' positionSelector = \'.pagantisSimulator\''; |
||
| 102 | echo ' }'; |
||
| 103 | |||
| 104 | echo ' if (priceSelector === \'default\') {'; |
||
| 105 | echo ' priceSelector = findPriceSelector();'; |
||
| 106 | echo ' }'; |
||
| 107 | |||
| 108 | echo ' if (quantitySelector === \'default\') {'; |
||
| 109 | echo ' quantitySelector = findQuantitySelector();'; |
||
| 110 | echo ' if (quantitySelector === \'default\') {'; |
||
| 111 | echo ' quantity = \'1\';'; |
||
| 112 | echo ' }'; |
||
| 113 | echo ' }'; |
||
| 114 | |||
| 115 | echo ' pgSDK.product_simulator = {};'; |
||
| 116 | echo ' pgSDK.product_simulator.id = \'product-simulator\';'; |
||
| 117 | echo ' pgSDK.product_simulator.publicKey = \'' . $this->pk . '\';'; |
||
| 118 | echo ' pgSDK.product_simulator.selector = positionSelector;'; |
||
| 119 | echo ' pgSDK.product_simulator.numInstalments = \'' . $this->extraConfig['PAGANTIS_SIMULATOR_START_INSTALLMENTS'] . '\';'; |
||
| 120 | echo ' pgSDK.product_simulator.type = ' . $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE'] . ';'; |
||
| 121 | echo ' pgSDK.product_simulator.skin = ' . $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_SKIN'] . ';'; |
||
| 122 | echo ' pgSDK.product_simulator.position = ' . $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'] . ';'; |
||
| 123 | |||
| 124 | echo ' if (priceSelector !== \'default\') {'; |
||
| 125 | echo ' pgSDK.product_simulator.itemAmountSelector = priceSelector;'; |
||
| 126 | echo ' }'; |
||
| 127 | echo ' if (quantitySelector !== \'default\' && quantitySelector !== \'none\') {'; |
||
| 128 | echo ' pgSDK.product_simulator.itemQuantitySelector = quantitySelector;'; |
||
| 129 | echo ' }'; |
||
| 130 | echo ' if (price != null) {'; |
||
| 131 | echo ' pgSDK.product_simulator.itemAmount = price;'; |
||
| 132 | echo ' }'; |
||
| 133 | echo ' if (quantity != null) {'; |
||
| 134 | echo ' pgSDK.product_simulator.itemQuantity = quantity;'; |
||
| 135 | echo ' }'; |
||
| 136 | |||
| 137 | echo ' pgSDK.simulator.init(pgSDK.product_simulator);'; |
||
| 138 | echo ' clearInterval(window.PSSimulatorId);'; |
||
| 139 | echo ' return true;'; |
||
| 140 | echo ' }'; |
||
| 141 | echo ' return false;'; |
||
| 142 | echo ' }'; |
||
| 143 | echo ' if (!loadSimulator()) {'; |
||
| 144 | echo ' window.PSSimulatorId = setInterval(function () {'; |
||
| 145 | echo ' loadSimulator();'; |
||
| 146 | echo ' }, 2000);'; |
||
| 147 | echo ' }'; |
||
| 148 | echo '</script>'; |
||
| 149 | echo '<div class="pagantisSimulator"></div>'; |
||
| 150 | } |
||
| 174 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.