Conditions | 18 |
Paths | 3245 |
Total Lines | 85 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | 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 | $jsRequirements .= HTML::createTag('script', $htmlAttributes); |
||
200 | $jsRequirements .= "\n"; |
||
201 | } |
||
202 | |||
203 | // Add all inline JavaScript *after* including external files they might rely on |
||
204 | foreach ($this->getCustomScripts() as $script) { |
||
205 | $jsRequirements .= HTML::createTag( |
||
206 | 'script', |
||
207 | [ |
||
208 | 'type' => 'application/javascript', |
||
209 | 'nonce' => $nonce, |
||
210 | ], |
||
211 | "//<![CDATA[\n{$script}\n//]]>" |
||
212 | ); |
||
213 | $jsRequirements .= "\n"; |
||
214 | } |
||
215 | |||
216 | // Custom head tags (comes first) |
||
217 | foreach ($this->getCustomHeadTags() as $customHeadTag) { |
||
218 | $requirements .= "{$customHeadTag}\n"; |
||
219 | } |
||
220 | |||
221 | // CSS file links |
||
222 | foreach ($this->getCSS() as $file => $params) { |
||
223 | $htmlAttributes = [ |
||
224 | 'rel' => 'stylesheet', |
||
225 | 'type' => 'text/css', |
||
226 | 'href' => $this->pathForFile($file), |
||
227 | ]; |
||
228 | if (!empty($params['media'])) { |
||
229 | $htmlAttributes['media'] = $params['media']; |
||
230 | } |
||
231 | $requirements .= HTML::createTag('link', $htmlAttributes); |
||
232 | $requirements .= "\n"; |
||
233 | } |
||
234 | |||
235 | // Literal custom CSS content |
||
236 | foreach ($this->getCustomCSS() as $css) { |
||
237 | $requirements .= HTML::createTag('style', ['type' => 'text/css'], "\n{$css}\n"); |
||
238 | $requirements .= "\n"; |
||
239 | } |
||
240 | |||
241 | // Inject CSS into body |
||
242 | $content = $this->insertTagsIntoHead($requirements, $content); |
||
243 | |||
244 | // Inject scripts |
||
245 | if ($this->getForceJSToBottom()) { |
||
246 | $content = $this->insertScriptsAtBottom($jsRequirements, $content); |
||
247 | } elseif ($this->getWriteJavascriptToBody()) { |
||
248 | $content = $this->insertScriptsIntoBody($jsRequirements, $content); |
||
249 | } else { |
||
250 | $content = $this->insertTagsIntoHead($jsRequirements, $content); |
||
251 | } |
||
252 | return $content; |
||
253 | } |
||
255 |
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.