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