| Conditions | 22 |
| Paths | 420 |
| Total Lines | 108 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 79 | private function tokenizeSource($source) |
||
| 80 | { |
||
| 81 | $tokens = token_get_all($source); |
||
| 82 | |||
| 83 | $builtNamespace = ''; |
||
| 84 | $buildingNamespace = false; |
||
| 85 | $matchedNamespace = false; |
||
| 86 | |||
| 87 | $useStatements = []; |
||
| 88 | $record = false; |
||
| 89 | $currentUse = [ |
||
| 90 | 'class' => '', |
||
| 91 | 'as' => '' |
||
| 92 | ]; |
||
| 93 | |||
| 94 | foreach ($tokens as $token) { |
||
| 95 | |||
| 96 | if ($token[0] === T_NAMESPACE) { |
||
| 97 | $buildingNamespace = true; |
||
| 98 | |||
| 99 | if ($matchedNamespace) { |
||
| 100 | break; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | if ($buildingNamespace) { |
||
| 105 | |||
| 106 | if ($token === ';') { |
||
| 107 | $buildingNamespace = false; |
||
| 108 | continue; |
||
| 109 | } |
||
| 110 | |||
| 111 | switch ($token[0]) { |
||
| 112 | |||
| 113 | case T_STRING: |
||
| 114 | case T_NS_SEPARATOR: |
||
| 115 | $builtNamespace .= $token[1]; |
||
| 116 | break; |
||
| 117 | } |
||
| 118 | |||
| 119 | continue; |
||
| 120 | } |
||
| 121 | |||
| 122 | if ($token === ';' || !is_array($token)) { |
||
| 123 | |||
| 124 | if ($record) { |
||
|
|
|||
| 125 | $useStatements[] = $currentUse; |
||
| 126 | $record = false; |
||
| 127 | $currentUse = [ |
||
| 128 | 'class' => '', |
||
| 129 | 'as' => '' |
||
| 130 | ]; |
||
| 131 | } |
||
| 132 | |||
| 133 | continue; |
||
| 134 | } |
||
| 135 | |||
| 136 | if ($token[0] === T_CLASS) { |
||
| 137 | break; |
||
| 138 | } |
||
| 139 | |||
| 140 | if (strcasecmp($builtNamespace, $this->getNamespaceName()) === 0) { |
||
| 141 | $matchedNamespace = true; |
||
| 142 | } |
||
| 143 | |||
| 144 | if ($matchedNamespace) { |
||
| 145 | |||
| 146 | if ($token[0] === T_USE) { |
||
| 147 | $record = 'class'; |
||
| 148 | } |
||
| 149 | |||
| 150 | if ($token[0] === T_AS) { |
||
| 151 | $record = 'as'; |
||
| 152 | } |
||
| 153 | |||
| 154 | if ($record) { |
||
| 155 | switch ($token[0]) { |
||
| 156 | |||
| 157 | case T_STRING: |
||
| 158 | case T_NS_SEPARATOR: |
||
| 159 | |||
| 160 | if ($record) { |
||
| 161 | $currentUse[$record] .= $token[1]; |
||
| 162 | } |
||
| 163 | |||
| 164 | break; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | if ($token[2] >= $this->getStartLine()) { |
||
| 170 | break; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | // Make sure the as key has the name of the class even |
||
| 175 | // if there is no alias in the use statement. |
||
| 176 | foreach ($useStatements as $k => $useStatement) { |
||
| 177 | |||
| 178 | $reflection = new \ReflectionClass($useStatement['class']); |
||
| 179 | |||
| 180 | $useStatements[$reflection->getShortName()] = $useStatement['class']; |
||
| 181 | |||
| 182 | unset($useStatements[$k]); |
||
| 183 | } |
||
| 184 | |||
| 185 | return $useStatements; |
||
| 186 | } |
||
| 187 | |||
| 217 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: