| 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 |
||
| 120 | public function includeInHTML($content) |
||
| 121 | { |
||
| 122 | // Get our CSP nonce, it's always good to have even if we don't use it :-) |
||
| 123 | $nonce = CspProvider::getCspNonce(); |
||
| 124 | |||
| 125 | // Skip if content isn't injectable, or there is nothing to inject |
||
| 126 | $tagsAvailable = preg_match('#</head\b#', $content); |
||
| 127 | $hasFiles = $this->css || $this->javascript || $this->customCSS || $this->customScript || $this->customHeadTags; |
||
| 128 | if (!$tagsAvailable || !$hasFiles) { |
||
| 129 | return $content; |
||
| 130 | } |
||
| 131 | $requirements = ''; |
||
| 132 | $jsRequirements = ''; |
||
| 133 | |||
| 134 | // Combine files - updates $this->javascript and $this->css |
||
| 135 | $this->processCombinedFiles(); |
||
| 136 | |||
| 137 | // Script tags for js links |
||
| 138 | foreach ($this->getJavascript() as $file => $attributes) { |
||
| 139 | // Build html attributes |
||
| 140 | $htmlAttributes = [ |
||
| 141 | 'type' => isset($attributes['type']) ? $attributes['type'] : "application/javascript", |
||
| 142 | 'src' => $this->pathForFile($file), |
||
| 143 | 'nonce' => $nonce, |
||
| 144 | ]; |
||
| 145 | if (!empty($attributes['async'])) { |
||
| 146 | $htmlAttributes['async'] = 'async'; |
||
| 147 | } |
||
| 148 | if (!empty($attributes['defer'])) { |
||
| 149 | $htmlAttributes['defer'] = 'defer'; |
||
| 150 | } |
||
| 151 | $jsRequirements .= HTML::createTag('script', $htmlAttributes); |
||
| 152 | $jsRequirements .= "\n"; |
||
| 153 | } |
||
| 154 | |||
| 155 | // Add all inline JavaScript *after* including external files they might rely on |
||
| 156 | foreach ($this->getCustomScripts() as $script) { |
||
| 157 | $jsRequirements .= HTML::createTag( |
||
| 158 | 'script', |
||
| 159 | [ |
||
| 160 | 'type' => 'application/javascript', |
||
| 161 | 'nonce' => $nonce, |
||
| 162 | ], |
||
| 163 | "//<![CDATA[\n{$script}\n//]]>" |
||
| 164 | ); |
||
| 165 | $jsRequirements .= "\n"; |
||
| 166 | } |
||
| 167 | |||
| 168 | // Custom head tags (comes first) |
||
| 169 | foreach ($this->getCustomHeadTags() as $customHeadTag) { |
||
| 170 | $requirements .= "{$customHeadTag}\n"; |
||
| 171 | } |
||
| 172 | |||
| 173 | // CSS file links |
||
| 174 | foreach ($this->getCSS() as $file => $params) { |
||
| 175 | $htmlAttributes = [ |
||
| 176 | 'rel' => 'stylesheet', |
||
| 177 | 'type' => 'text/css', |
||
| 178 | 'href' => $this->pathForFile($file), |
||
| 179 | ]; |
||
| 180 | if (!empty($params['media'])) { |
||
| 181 | $htmlAttributes['media'] = $params['media']; |
||
| 182 | } |
||
| 183 | $requirements .= HTML::createTag('link', $htmlAttributes); |
||
| 184 | $requirements .= "\n"; |
||
| 185 | } |
||
| 186 | |||
| 187 | // Literal custom CSS content |
||
| 188 | foreach ($this->getCustomCSS() as $css) { |
||
| 189 | $requirements .= HTML::createTag('style', ['type' => 'text/css'], "\n{$css}\n"); |
||
| 190 | $requirements .= "\n"; |
||
| 191 | } |
||
| 192 | |||
| 193 | // Inject CSS into body |
||
| 194 | $content = $this->insertTagsIntoHead($requirements, $content); |
||
| 195 | |||
| 196 | // Inject scripts |
||
| 197 | if ($this->getForceJSToBottom()) { |
||
| 198 | $content = $this->insertScriptsAtBottom($jsRequirements, $content); |
||
| 199 | } elseif ($this->getWriteJavascriptToBody()) { |
||
| 200 | $content = $this->insertScriptsIntoBody($jsRequirements, $content); |
||
| 201 | } else { |
||
| 202 | $content = $this->insertTagsIntoHead($jsRequirements, $content); |
||
| 203 | } |
||
| 204 | return $content; |
||
| 205 | } |
||
| 207 |
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.