| Conditions | 32 |
| Paths | 122 |
| Total Lines | 128 |
| Code Lines | 88 |
| Lines | 24 |
| Ratio | 18.75 % |
| Changes | 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 |
||
| 135 | private function readFile(&$contents, $file, $ext) |
||
| 136 | { |
||
| 137 | if (!file_exists($file)) { |
||
| 138 | return; |
||
| 139 | } |
||
| 140 | $source = fopen($file, 'r'); |
||
| 141 | if (!$source) { |
||
| 142 | return; |
||
| 143 | } |
||
| 144 | |||
| 145 | $contents[] = "/* --- Source file: {$file} */\n\n"; |
||
| 146 | $inMultilineComment = false; |
||
| 147 | $importsAllowd = true; |
||
| 148 | $wasCommentHack = false; |
||
| 149 | while (!feof($source)) { |
||
| 150 | if ($ext == 'css') { |
||
| 151 | $line = fgets($source, 4096); |
||
| 152 | $lineParse = trim($line); |
||
| 153 | $lineParse_length = mb_strlen($lineParse, 'UTF-8'); |
||
| 154 | $newLine = ''; |
||
| 155 | // parse line char by char |
||
| 156 | for ($i = 0; $i < $lineParse_length; $i++) { |
||
| 157 | $char = $lineParse[$i]; |
||
| 158 | $nextchar = $i < ($lineParse_length - 1) ? $lineParse[$i + 1] : ''; |
||
| 159 | if (!$inMultilineComment && $char == '/' && $nextchar == '*') { |
||
| 160 | // a multiline comment starts here |
||
| 161 | $inMultilineComment = true; |
||
| 162 | $wasCommentHack = false; |
||
| 163 | $newLine .= $char . $nextchar; |
||
| 164 | $i++; |
||
| 165 | } elseif ($inMultilineComment && $char == '*' && $nextchar == '/') { |
||
| 166 | // a multiline comment stops here |
||
| 167 | $inMultilineComment = false; |
||
| 168 | $newLine .= $char . $nextchar; |
||
| 169 | if (substr($lineParse, $i - 3, 8) == '/*\*//*/') { |
||
| 170 | $wasCommentHack = true; |
||
| 171 | $i += 3; // move to end of hack process hack as it where |
||
| 172 | $newLine .= '/*/'; // fix hack comment because we lost some chars with $i += 3 |
||
| 173 | } |
||
| 174 | $i++; |
||
| 175 | } elseif ($importsAllowd && $char == '@' && substr($lineParse, $i, 7) == '@import') { |
||
| 176 | // an @import starts here |
||
| 177 | $lineParseRest = trim(substr($lineParse, $i + 7)); |
||
| 178 | if (strtolower(substr($lineParseRest, 0, 3)) == 'url') { |
||
| 179 | // the @import uses url to specify the path |
||
| 180 | $posEnd = strpos($lineParse, ';', $i); |
||
| 181 | $charsEnd = substr($lineParse, $posEnd - 1, 2); |
||
| 182 | if ($charsEnd == ');') { |
||
| 183 | // used url() without media |
||
| 184 | $start = strpos($lineParseRest, '(') + 1; |
||
| 185 | $end = strpos($lineParseRest, ')'); |
||
| 186 | $url = substr($lineParseRest, $start, $end - $start); |
||
| 187 | View Code Duplication | if ($url[0] == '"' | $url[0] == "'") { |
|
| 188 | $url = substr($url, 1, strlen($url) - 2); |
||
| 189 | } |
||
| 190 | // fix url |
||
| 191 | $url = dirname($file) . '/' . $url; |
||
| 192 | View Code Duplication | if (!$wasCommentHack) { |
|
| 193 | // clear buffer |
||
| 194 | $contents[] = $newLine; |
||
| 195 | $newLine = ''; |
||
| 196 | // process include |
||
| 197 | $this->readFile($contents, $url, $ext); |
||
| 198 | } else { |
||
| 199 | $newLine .= '@import url("' . $url . '");'; |
||
| 200 | } |
||
| 201 | // skip @import statement |
||
| 202 | $i += $posEnd - $i; |
||
| 203 | } else { |
||
| 204 | // @import contains media type so we can't include its contents. |
||
| 205 | // We need to fix the url instead. |
||
| 206 | $start = strpos($lineParseRest, '(') + 1; |
||
| 207 | $end = strpos($lineParseRest, ')'); |
||
| 208 | $url = substr($lineParseRest, $start, $end - $start); |
||
| 209 | View Code Duplication | if ($url[0] == '"' | $url[0] == "'") { |
|
| 210 | $url = substr($url, 1, strlen($url) - 2); |
||
| 211 | } |
||
| 212 | // fix url |
||
| 213 | $url = dirname($file) . '/' . $url; |
||
| 214 | // readd @import with fixed url |
||
| 215 | $newLine .= '@import url("' . $url . '")' . substr($lineParseRest, $end + 1, strpos($lineParseRest, ';') - $end - 1) . ';'; |
||
| 216 | // skip @import statement |
||
| 217 | $i += $posEnd - $i; |
||
| 218 | } |
||
| 219 | } elseif (substr($lineParseRest, 0, 1) == '"' || substr($lineParseRest, 0, 1) == '\'') { |
||
| 220 | // the @import uses an normal string to specify the path |
||
| 221 | $posEnd = strpos($lineParseRest, ';'); |
||
| 222 | $url = substr($lineParseRest, 1, $posEnd - 2); |
||
| 223 | $posEnd = strpos($lineParse, ';', $i); |
||
| 224 | // fix url |
||
| 225 | $url = dirname($file) . '/' . $url; |
||
| 226 | View Code Duplication | if (!$wasCommentHack) { |
|
| 227 | // clear buffer |
||
| 228 | $contents[] = $newLine; |
||
| 229 | $newLine = ''; |
||
| 230 | // process include |
||
| 231 | self::readFile($contents, $url, $ext); |
||
| 232 | } else { |
||
| 233 | $newLine .= '@import url("' . $url . '");'; |
||
| 234 | } |
||
| 235 | // skip @import statement |
||
| 236 | $i += $posEnd - $i; |
||
| 237 | } |
||
| 238 | } elseif (!$inMultilineComment && $char != ' ' && $char != "\n" && $char != "\r\n" && $char != "\r") { |
||
| 239 | // css rule found -> stop processing of @import statements |
||
| 240 | $importsAllowd = false; |
||
| 241 | $newLine .= $char; |
||
| 242 | } else { |
||
| 243 | $newLine .= $char; |
||
| 244 | } |
||
| 245 | } |
||
| 246 | // fix other paths after @import processing |
||
| 247 | if (!$importsAllowd) { |
||
| 248 | $relativePath = str_replace(realpath($this->rootDir), '', $file); |
||
| 249 | $newLine = self::cssFixPath($newLine, explode('/', dirname($relativePath))); |
||
| 250 | } |
||
| 251 | $contents[] = $newLine; |
||
| 252 | } else { |
||
| 253 | $contents[] = fgets($source, 4096); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | fclose($source); |
||
| 257 | if ($ext == 'js') { |
||
| 258 | $contents[] = "\n;\n"; |
||
| 259 | } else { |
||
| 260 | $contents[] = "\n\n"; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 308 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.