| Conditions | 28 |
| Paths | > 20000 |
| Total Lines | 122 |
| Code Lines | 68 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| 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 |
||
| 188 | public function includeInHTML($content) |
||
| 189 | { |
||
| 190 | // Get our CSP nonce, it's always good to have even if we don't use it :-) |
||
| 191 | $nonce = CspProvider::getCspNonce(); |
||
| 192 | |||
| 193 | // Skip if content isn't injectable, or there is nothing to inject |
||
| 194 | $tagsAvailable = preg_match('#</head\b#', $content); |
||
| 195 | $hasFiles = $this->css || $this->javascript || $this->customCSS || $this->customScript || $this->customHeadTags; |
||
| 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 | if (!empty($attributes['defer'])) { |
||
| 217 | $htmlAttributes['defer'] = 'defer'; |
||
| 218 | } |
||
| 219 | if (!empty($attributes['integrity'])) { |
||
| 220 | $htmlAttributes['integrity'] = $attributes['integrity']; |
||
| 221 | } |
||
| 222 | if (!empty($attributes['crossorigin'])) { |
||
| 223 | $htmlAttributes['crossorigin'] = $attributes['crossorigin']; |
||
| 224 | } |
||
| 225 | if (!empty($attributes['cookie-consent'])) { |
||
| 226 | $htmlAttributes['cookie-consent'] = $attributes['cookie-consent']; |
||
| 227 | } |
||
| 228 | $jsRequirements .= HTML::createTag('script', $htmlAttributes); |
||
| 229 | $jsRequirements .= "\n"; |
||
| 230 | } |
||
| 231 | |||
| 232 | // Add all inline JavaScript *after* including external files they might rely on |
||
| 233 | foreach ($this->getCustomScripts() as $scriptId => $script) { |
||
| 234 | $type = self::config()->enable_js_modules ? 'module' : 'application/javascript'; |
||
| 235 | $attributes = [ |
||
| 236 | 'type' => $type, |
||
| 237 | 'nonce' => $nonce, |
||
| 238 | ]; |
||
| 239 | // For cookie-consent, since the Requirements API does not support passing variables |
||
| 240 | // we rely on last part of uniqueness id |
||
| 241 | if ($scriptId) { |
||
| 242 | $parts = explode("_", $scriptId); |
||
| 243 | $lastPart = array_pop($parts); |
||
| 244 | if (in_array($lastPart, self::listCookieTypes())) { |
||
| 245 | $attributes['type'] = 'text/plain'; |
||
| 246 | $attributes['cookie-consent'] = $lastPart; |
||
| 247 | } |
||
| 248 | if (in_array($lastPart, self::listScriptTypes())) { |
||
| 249 | $attributes['type'] = $lastPart; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | // Wrap script in a DOMContentLoaded |
||
| 254 | // Make sure we don't add the eventListener twice (this will only work for simple scripts) |
||
| 255 | // Make sure we don't wrap scripts concerned by security policies |
||
| 256 | // Js modules are deferred by default, even if they are inlined, so not wrapping needed |
||
| 257 | // @link https://stackoverflow.com/questions/41394983/how-to-defer-inline-javascript |
||
| 258 | if (empty($attributes['cookie-consent']) && strpos($script, 'window.addEventListener') === false && !self::config()->enable_js_modules) { |
||
| 259 | $script = "window.addEventListener('DOMContentLoaded', function() { $script });"; |
||
| 260 | } |
||
| 261 | |||
| 262 | // Remove comments if any |
||
| 263 | $script = preg_replace('/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\")\/\/.*))/', '', $script); |
||
| 264 | |||
| 265 | $jsRequirements .= HTML::createTag( |
||
| 266 | 'script', |
||
| 267 | $attributes, |
||
| 268 | "//<![CDATA[\n{$script}\n//]]>" |
||
| 269 | ); |
||
| 270 | $jsRequirements .= "\n"; |
||
| 271 | } |
||
| 272 | |||
| 273 | // Custom head tags (comes first) |
||
| 274 | foreach ($this->getCustomHeadTags() as $customHeadTag) { |
||
| 275 | $requirements .= "{$customHeadTag}\n"; |
||
| 276 | } |
||
| 277 | |||
| 278 | // CSS file links |
||
| 279 | foreach ($this->getCSS() as $file => $params) { |
||
| 280 | $htmlAttributes = [ |
||
| 281 | 'rel' => 'stylesheet', |
||
| 282 | 'type' => 'text/css', |
||
| 283 | 'href' => $this->pathForFile($file), |
||
| 284 | ]; |
||
| 285 | if (!empty($params['media'])) { |
||
| 286 | $htmlAttributes['media'] = $params['media']; |
||
| 287 | } |
||
| 288 | $requirements .= HTML::createTag('link', $htmlAttributes); |
||
| 289 | $requirements .= "\n"; |
||
| 290 | } |
||
| 291 | |||
| 292 | // Literal custom CSS content |
||
| 293 | foreach ($this->getCustomCSS() as $css) { |
||
| 294 | $requirements .= HTML::createTag('style', ['type' => 'text/css'], "\n{$css}\n"); |
||
| 295 | $requirements .= "\n"; |
||
| 296 | } |
||
| 297 | |||
| 298 | // Inject CSS into body |
||
| 299 | $content = $this->insertTagsIntoHead($requirements, $content); |
||
| 300 | |||
| 301 | // Inject scripts |
||
| 302 | if ($this->getForceJSToBottom()) { |
||
| 303 | $content = $this->insertScriptsAtBottom($jsRequirements, $content); |
||
| 304 | } elseif ($this->getWriteJavascriptToBody()) { |
||
| 305 | $content = $this->insertScriptsIntoBody($jsRequirements, $content); |
||
| 306 | } else { |
||
| 307 | $content = $this->insertTagsIntoHead($jsRequirements, $content); |
||
| 308 | } |
||
| 309 | return $content; |
||
| 310 | } |
||
| 312 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.