Conditions | 15 |
Paths | 4 |
Total Lines | 52 |
Code Lines | 28 |
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 |
||
34 | private function getClassNameFromFile($file) |
||
35 | { |
||
36 | $fp = fopen($file, 'r'); |
||
37 | |||
38 | $class = $namespace = $buffer = ''; |
||
39 | $i = 0; |
||
40 | |||
41 | while (!$class) { |
||
42 | if (feof($fp)) { |
||
43 | break; |
||
44 | } |
||
45 | |||
46 | // Read entire lines to prevent keyword truncation |
||
47 | for ($line = 0; $line <= 20; $line++) { |
||
48 | $buffer .= fgets($fp); |
||
49 | } |
||
50 | $tokens = @token_get_all($buffer); |
||
51 | |||
52 | if (strpos($buffer, '{') === false) { |
||
53 | continue; |
||
54 | } |
||
55 | |||
56 | for (; $i < count($tokens); $i++) { |
||
57 | if ($tokens[$i][0] === T_NAMESPACE) { |
||
58 | for ($j = $i + 1; $j < count($tokens); $j++) { |
||
59 | if ($tokens[$j][0] === T_STRING) { |
||
60 | $namespace .= '\\' . $tokens[$j][1]; |
||
61 | } elseif ($tokens[$j] === '{' || $tokens[$j] === ';') { |
||
62 | break; |
||
63 | } |
||
64 | } |
||
65 | } |
||
66 | |||
67 | if ($tokens[$i][0] === T_CLASS) { |
||
68 | for ($j = $i + 1; $j < count($tokens); $j++) { |
||
69 | if ($tokens[$j][0] === T_STRING) { |
||
70 | $class = $tokens[$i + 2][1]; |
||
71 | break 2; |
||
72 | } |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | |||
78 | if (!trim($class)) { |
||
79 | return; |
||
80 | } |
||
81 | |||
82 | fclose($fp); |
||
83 | |||
84 | return ltrim($namespace . '\\' . $class, '\\'); |
||
85 | } |
||
86 | } |
||
87 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: