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