Conditions | 29 |
Paths | > 20000 |
Total Lines | 126 |
Code Lines | 68 |
Lines | 0 |
Ratio | 0 % |
Changes | 8 | ||
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 | |||
233 | // since the Requirements API does not support passing variables, we use naming conventions |
||
234 | if ($scriptId) { |
||
235 | // Check for jsmodule in the name, since we have no other way to pass arguments |
||
236 | if (strpos($scriptId, "jsmodule") !== false) { |
||
237 | $attributes['type'] = 'module'; |
||
238 | } |
||
239 | |||
240 | // For cookie-consent, we rely on last part of uniquness id |
||
241 | $parts = explode("-", $scriptId); |
||
242 | $lastPart = array_pop($parts); |
||
243 | if (in_array($lastPart, self::listCookieTypes())) { |
||
244 | $attributes['type'] = 'text/plain'; |
||
245 | $attributes['cookie-consent'] = $lastPart; |
||
246 | } |
||
247 | } |
||
248 | |||
249 | // Wrap script in a DOMContentLoaded |
||
250 | // Make sure we don't add the eventListener twice (this will only work for simple scripts) |
||
251 | // Make sure we don't wrap scripts concerned by security policies |
||
252 | // Js modules are deferred by default, even if they are inlined, so not wrapping needed |
||
253 | // @link https://stackoverflow.com/questions/41394983/how-to-defer-inline-javascript |
||
254 | if (empty($attributes['cookie-consent']) && strpos($script, 'window.addEventListener') === false && $attributes['type'] !== 'module') { |
||
255 | $script = "window.addEventListener('DOMContentLoaded', function() { $script });"; |
||
256 | } |
||
257 | |||
258 | // Remove comments if any |
||
259 | $script = preg_replace('/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\")\/\/.*))/', '', $script); |
||
260 | |||
261 | $jsRequirements .= HTML::createTag( |
||
262 | 'script', |
||
263 | $attributes, |
||
264 | "//<![CDATA[\n{$script}\n//]]>" |
||
265 | ); |
||
266 | $jsRequirements .= "\n"; |
||
267 | } |
||
268 | |||
269 | // Custom head tags (comes first) |
||
270 | foreach ($this->getCustomHeadTags() as $customHeadTag) { |
||
271 | $requirements .= "{$customHeadTag}\n"; |
||
272 | } |
||
273 | |||
274 | // CSS file links |
||
275 | foreach ($this->getCSS() as $file => $params) { |
||
276 | $htmlAttributes = [ |
||
277 | 'rel' => 'stylesheet', |
||
278 | 'type' => 'text/css', |
||
279 | 'href' => $this->pathForFile($file), |
||
280 | ]; |
||
281 | if (!empty($params['media'])) { |
||
282 | $htmlAttributes['media'] = $params['media']; |
||
283 | } |
||
284 | $requirements .= str_replace(' />', '>', HTML::createTag('link', $htmlAttributes)); |
||
285 | $requirements .= "\n"; |
||
286 | } |
||
287 | |||
288 | // Literal custom CSS content |
||
289 | foreach ($this->getCustomCSS() as $css) { |
||
290 | $requirements .= HTML::createTag('style', ['type' => 'text/css'], "\n{$css}\n"); |
||
291 | $requirements .= "\n"; |
||
292 | } |
||
293 | |||
294 | // Inject CSS into body |
||
295 | $content = $this->insertTagsIntoHead($requirements, $content); |
||
296 | |||
297 | // Inject scripts |
||
298 | if ($this->getForceJSToBottom()) { |
||
299 | $content = $this->insertScriptsAtBottom($jsRequirements, $content); |
||
300 | } elseif ($this->getWriteJavascriptToBody()) { |
||
301 | $content = $this->insertScriptsIntoBody($jsRequirements, $content); |
||
302 | } else { |
||
303 | $content = $this->insertTagsIntoHead($jsRequirements, $content); |
||
304 | } |
||
305 | return $content; |
||
306 | } |
||
308 |
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.