| Conditions | 29 |
| Paths | > 20000 |
| Total Lines | 131 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 9 | ||
| Bugs | 3 | 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 |
||
| 183 | public function includeInHTML($content) |
||
| 184 | { |
||
| 185 | // Get our CSP nonce, it's always good to have even if we don't use it :-) |
||
| 186 | $nonce = CspProvider::getCspNonce(); |
||
| 187 | |||
| 188 | // Skip if content isn't injectable, or there is nothing to inject |
||
| 189 | $tagsAvailable = preg_match('#</head\b#', $content); |
||
| 190 | $hasFiles = !empty($this->css) |
||
| 191 | || !empty($this->javascript) |
||
| 192 | || !empty($this->customCSS) |
||
| 193 | || !empty($this->customScript) |
||
| 194 | || !empty($this->customHeadTags); |
||
| 195 | |||
| 196 | if (!$tagsAvailable || !$hasFiles) { |
||
| 197 | return $content; |
||
| 198 | } |
||
| 199 | $requirements = ''; |
||
| 200 | $jsRequirements = ''; |
||
| 201 | |||
| 202 | // Combine files - updates $this->javascript and $this->css |
||
| 203 | $this->processCombinedFiles(); |
||
| 204 | |||
| 205 | // Script tags for js links |
||
| 206 | foreach ($this->getJavascript() as $file => $attributes) { |
||
| 207 | // Build html attributes |
||
| 208 | $htmlAttributes = [ |
||
| 209 | 'type' => isset($attributes['type']) ? $attributes['type'] : "application/javascript", |
||
| 210 | 'src' => $this->pathForFile($file), |
||
| 211 | 'nonce' => $nonce, |
||
| 212 | ]; |
||
| 213 | if (!empty($attributes['async'])) { |
||
| 214 | $htmlAttributes['async'] = 'async'; |
||
| 215 | } |
||
| 216 | // defer is not allowed for module, ignore it as it does the same anyway |
||
| 217 | if (!empty($attributes['defer']) && $htmlAttributes['type'] !== 'module') { |
||
| 218 | $htmlAttributes['defer'] = 'defer'; |
||
| 219 | } |
||
| 220 | if (!empty($attributes['integrity'])) { |
||
| 221 | $htmlAttributes['integrity'] = $attributes['integrity']; |
||
| 222 | } |
||
| 223 | if (!empty($attributes['crossorigin'])) { |
||
| 224 | $htmlAttributes['crossorigin'] = $attributes['crossorigin']; |
||
| 225 | } |
||
| 226 | if (!empty($attributes['cookie-consent'])) { |
||
| 227 | $htmlAttributes['cookie-consent'] = $attributes['cookie-consent']; |
||
| 228 | } |
||
| 229 | $jsRequirements .= str_replace(' />', '>', HTML::createTag('script', $htmlAttributes)); |
||
| 230 | $jsRequirements .= "\n"; |
||
| 231 | } |
||
| 232 | |||
| 233 | // Add all inline JavaScript *after* including external files they might rely on |
||
| 234 | foreach ($this->getCustomScripts() as $scriptId => $script) { |
||
| 235 | $type = self::config()->enable_js_modules ? 'module' : 'application/javascript'; |
||
| 236 | $attributes = [ |
||
| 237 | 'type' => $type, |
||
| 238 | 'nonce' => $nonce, |
||
| 239 | ]; |
||
| 240 | |||
| 241 | // since the Requirements API does not support passing variables, we use naming conventions |
||
| 242 | if ($scriptId) { |
||
| 243 | // Check for jsmodule in the name, since we have no other way to pass arguments |
||
| 244 | if (strpos($scriptId, "jsmodule") !== false) { |
||
| 245 | $attributes['type'] = 'module'; |
||
| 246 | } |
||
| 247 | |||
| 248 | // For cookie-consent, we rely on last part of uniquness id |
||
| 249 | $parts = explode("-", $scriptId); |
||
| 250 | $lastPart = array_pop($parts); |
||
| 251 | if (in_array($lastPart, self::listCookieTypes())) { |
||
| 252 | $attributes['type'] = 'text/plain'; |
||
| 253 | $attributes['cookie-consent'] = $lastPart; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | // Wrap script in a DOMContentLoaded |
||
| 258 | // Make sure we don't add the eventListener twice (this will only work for simple scripts) |
||
| 259 | // Make sure we don't wrap scripts concerned by security policies |
||
| 260 | // Js modules are deferred by default, even if they are inlined, so not wrapping needed |
||
| 261 | // @link https://stackoverflow.com/questions/41394983/how-to-defer-inline-javascript |
||
| 262 | if (empty($attributes['cookie-consent']) && strpos($script, 'window.addEventListener') === false && $attributes['type'] !== 'module') { |
||
| 263 | $script = "window.addEventListener('DOMContentLoaded', function() { $script });"; |
||
| 264 | } |
||
| 265 | |||
| 266 | // Remove comments if any |
||
| 267 | $script = preg_replace('/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\")\/\/.*))/', '', $script); |
||
| 268 | |||
| 269 | $jsRequirements .= HTML::createTag( |
||
| 270 | 'script', |
||
| 271 | $attributes, |
||
| 272 | "//<![CDATA[\n{$script}\n//]]>" |
||
| 273 | ); |
||
| 274 | $jsRequirements .= "\n"; |
||
| 275 | } |
||
| 276 | |||
| 277 | // Custom head tags (comes first) |
||
| 278 | foreach ($this->getCustomHeadTags() as $customHeadTag) { |
||
| 279 | $requirements .= "{$customHeadTag}\n"; |
||
| 280 | } |
||
| 281 | |||
| 282 | // CSS file links |
||
| 283 | foreach ($this->getCSS() as $file => $params) { |
||
| 284 | $htmlAttributes = [ |
||
| 285 | 'rel' => 'stylesheet', |
||
| 286 | 'type' => 'text/css', |
||
| 287 | 'href' => $this->pathForFile($file), |
||
| 288 | ]; |
||
| 289 | if (!empty($params['media'])) { |
||
| 290 | $htmlAttributes['media'] = $params['media']; |
||
| 291 | } |
||
| 292 | $requirements .= str_replace(' />', '>', HTML::createTag('link', $htmlAttributes)); |
||
| 293 | $requirements .= "\n"; |
||
| 294 | } |
||
| 295 | |||
| 296 | // Literal custom CSS content |
||
| 297 | foreach ($this->getCustomCSS() as $css) { |
||
| 298 | $requirements .= HTML::createTag('style', ['type' => 'text/css'], "\n{$css}\n"); |
||
| 299 | $requirements .= "\n"; |
||
| 300 | } |
||
| 301 | |||
| 302 | // Inject CSS into body |
||
| 303 | $content = $this->insertTagsIntoHead($requirements, $content); |
||
| 304 | |||
| 305 | // Inject scripts |
||
| 306 | if ($this->getForceJSToBottom()) { |
||
| 307 | $content = $this->insertScriptsAtBottom($jsRequirements, $content); |
||
| 308 | } elseif ($this->getWriteJavascriptToBody()) { |
||
| 309 | $content = $this->insertScriptsIntoBody($jsRequirements, $content); |
||
| 310 | } else { |
||
| 311 | $content = $this->insertTagsIntoHead($jsRequirements, $content); |
||
| 312 | } |
||
| 313 | return $content; |
||
| 314 | } |
||
| 316 |