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