Conditions | 21 |
Paths | > 20000 |
Total Lines | 94 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 |
||
168 | public function includeInHTML($content) |
||
169 | { |
||
170 | // Get our CSP nonce, it's always good to have even if we don't use it :-) |
||
171 | $nonce = CspProvider::getCspNonce(); |
||
172 | |||
173 | // Skip if content isn't injectable, or there is nothing to inject |
||
174 | $tagsAvailable = preg_match('#</head\b#', $content); |
||
175 | $hasFiles = $this->css || $this->javascript || $this->customCSS || $this->customScript || $this->customHeadTags; |
||
176 | if (!$tagsAvailable || !$hasFiles) { |
||
177 | return $content; |
||
178 | } |
||
179 | $requirements = ''; |
||
180 | $jsRequirements = ''; |
||
181 | |||
182 | // Combine files - updates $this->javascript and $this->css |
||
183 | $this->processCombinedFiles(); |
||
184 | |||
185 | // Script tags for js links |
||
186 | foreach ($this->getJavascript() as $file => $attributes) { |
||
187 | // Build html attributes |
||
188 | $htmlAttributes = [ |
||
189 | 'type' => isset($attributes['type']) ? $attributes['type'] : "application/javascript", |
||
190 | 'src' => $this->pathForFile($file), |
||
191 | 'nonce' => $nonce, |
||
192 | ]; |
||
193 | if (!empty($attributes['async'])) { |
||
194 | $htmlAttributes['async'] = 'async'; |
||
195 | } |
||
196 | if (!empty($attributes['defer'])) { |
||
197 | $htmlAttributes['defer'] = 'defer'; |
||
198 | } |
||
199 | if (!empty($attributes['integrity'])) { |
||
200 | $htmlAttributes['integrity'] = $attributes['integrity']; |
||
201 | } |
||
202 | if (!empty($attributes['crossorigin'])) { |
||
203 | $htmlAttributes['crossorigin'] = $attributes['crossorigin']; |
||
204 | } |
||
205 | if (!empty($attributes['cookie-consent'])) { |
||
206 | $htmlAttributes['cookie-consent'] = $attributes['cookie-consent']; |
||
207 | } |
||
208 | $jsRequirements .= HTML::createTag('script', $attributes); |
||
209 | $jsRequirements .= "\n"; |
||
210 | } |
||
211 | |||
212 | // Add all inline JavaScript *after* including external files they might rely on |
||
213 | foreach ($this->getCustomScripts() as $script) { |
||
214 | $jsRequirements .= HTML::createTag( |
||
215 | 'script', |
||
216 | [ |
||
217 | 'type' => 'application/javascript', |
||
218 | 'nonce' => $nonce, |
||
219 | ], |
||
220 | "//<![CDATA[\n{$script}\n//]]>" |
||
221 | ); |
||
222 | $jsRequirements .= "\n"; |
||
223 | } |
||
224 | |||
225 | // Custom head tags (comes first) |
||
226 | foreach ($this->getCustomHeadTags() as $customHeadTag) { |
||
227 | $requirements .= "{$customHeadTag}\n"; |
||
228 | } |
||
229 | |||
230 | // CSS file links |
||
231 | foreach ($this->getCSS() as $file => $params) { |
||
232 | $htmlAttributes = [ |
||
233 | 'rel' => 'stylesheet', |
||
234 | 'type' => 'text/css', |
||
235 | 'href' => $this->pathForFile($file), |
||
236 | ]; |
||
237 | if (!empty($params['media'])) { |
||
238 | $htmlAttributes['media'] = $params['media']; |
||
239 | } |
||
240 | $requirements .= HTML::createTag('link', $htmlAttributes); |
||
241 | $requirements .= "\n"; |
||
242 | } |
||
243 | |||
244 | // Literal custom CSS content |
||
245 | foreach ($this->getCustomCSS() as $css) { |
||
246 | $requirements .= HTML::createTag('style', ['type' => 'text/css'], "\n{$css}\n"); |
||
247 | $requirements .= "\n"; |
||
248 | } |
||
249 | |||
250 | // Inject CSS into body |
||
251 | $content = $this->insertTagsIntoHead($requirements, $content); |
||
252 | |||
253 | // Inject scripts |
||
254 | if ($this->getForceJSToBottom()) { |
||
255 | $content = $this->insertScriptsAtBottom($jsRequirements, $content); |
||
256 | } elseif ($this->getWriteJavascriptToBody()) { |
||
257 | $content = $this->insertScriptsIntoBody($jsRequirements, $content); |
||
258 | } else { |
||
259 | $content = $this->insertTagsIntoHead($jsRequirements, $content); |
||
260 | } |
||
261 | return $content; |
||
262 | } |
||
264 |
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.