| Conditions | 10 |
| Paths | 32 |
| Total Lines | 66 |
| Code Lines | 41 |
| 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 |
||
| 61 | public function execute(Http $http) |
||
| 62 | { |
||
| 63 | $scripts = []; |
||
| 64 | $html = $http->getBody(); |
||
| 65 | |||
| 66 | $jsons = []; |
||
| 67 | $scriptStart = '<script'; |
||
| 68 | $scriptEnd = '</script>'; |
||
| 69 | |||
| 70 | $start = 0; |
||
| 71 | $i = 0; |
||
| 72 | while (($start = stripos($html, $scriptStart, $start)) !== false) { |
||
| 73 | $end = stripos($html, $scriptEnd, $start); |
||
| 74 | |||
| 75 | if ($end === false) { |
||
| 76 | break; |
||
| 77 | } |
||
| 78 | |||
| 79 | $len = $end + strlen($scriptEnd) - $start; |
||
| 80 | $script = substr($html, $start, $len); |
||
| 81 | |||
| 82 | if ( |
||
| 83 | $this->config->isOptimizeXMagentoInitScripts() && |
||
| 84 | strpos($script, 'text/x-magento-init') !== false |
||
| 85 | ) { |
||
| 86 | $jsons[] = strip_tags($script); |
||
| 87 | $html = str_replace($script, '', $html); |
||
| 88 | continue; |
||
| 89 | } |
||
| 90 | |||
| 91 | if ($this->validateSkipper->execute($script, $http)) { |
||
| 92 | $start++; |
||
| 93 | continue; |
||
| 94 | } |
||
| 95 | |||
| 96 | $html = str_replace($script, '', $html); |
||
| 97 | $scripts[] = $script; |
||
| 98 | |||
| 99 | $i++; |
||
| 100 | } |
||
| 101 | |||
| 102 | if ($this->config->isMinifyBodyScript()) { |
||
| 103 | $scripts = $this->minifyJs->execute($scripts); |
||
| 104 | } |
||
| 105 | |||
| 106 | $merged = []; |
||
| 107 | foreach ($jsons as $json) { |
||
| 108 | $json = Json::decode($json); |
||
| 109 | $merged = ArrayHelper::merge($merged, $json); |
||
| 110 | } |
||
| 111 | |||
| 112 | if (count($merged) > 0) { |
||
| 113 | $merged = '<script type=text/x-magento-init>' . Json::encode($merged) . '</script>'; |
||
| 114 | } else { |
||
| 115 | $merged = ''; |
||
| 116 | } |
||
| 117 | |||
| 118 | $scripts = implode('', $scripts); |
||
| 119 | |||
| 120 | if ($endBody = stripos($html, '</body>')) { |
||
| 121 | $html = substr($html, 0, $endBody) . $merged . $scripts . substr($html, $endBody); |
||
| 122 | } else { |
||
| 123 | $html .= $merged . $scripts; |
||
| 124 | } |
||
| 125 | |||
| 126 | $http->setBody($html); |
||
| 127 | } |
||
| 128 | } |