Conditions | 14 |
Paths | 44 |
Total Lines | 50 |
Code Lines | 29 |
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 |
||
121 | public static function findConfigKeyValuePairAsTokens($tokens, $label, $setting) |
||
122 | { |
||
123 | //$climate = Command::getClimate(); |
||
124 | $depth = 0; |
||
125 | $returnTokens = []; |
||
126 | foreach ($tokens as $key=>$token) { |
||
127 | if ($token == '[') { |
||
128 | $depth++; |
||
129 | continue; |
||
130 | } |
||
131 | // find config label |
||
132 | if ($depth === 1 && $token[0]==323) { // T_CONSTANT_ENCAPSED_STRING |
||
133 | $strValue = substr($token[1], 1, -1); |
||
134 | if ($strValue == $label) { |
||
135 | $labelLine = $token[2]; // remember line # where the config we care about starts |
||
136 | $nextKey = true; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | // find key value pair |
||
141 | if ($labelLine) { |
||
142 | if ($depth === 2 && $token[0]==323) { |
||
143 | if ($nextKey) { |
||
144 | $nextKey = false; |
||
145 | // found key for setting |
||
146 | $strValue = substr($token[1], 1, -1); |
||
147 | if ($strValue === $setting) { |
||
148 | $returnTokens['key'] = ['token'=>$token,'index'=>$key]; |
||
149 | //$climate->out(sprintf('Found key <yellow>%s</yellow> at line <green>%s</green>',$token[1],$token[2])); // make verbose mode |
||
150 | continue; |
||
151 | } |
||
152 | } else { |
||
153 | // next value once setting is found is the value we want |
||
154 | if ($returnTokens['key']) { |
||
155 | $strValue = substr($token[1], 1, -1); |
||
156 | //$climate->out(sprintf('Found value <yellow>%s</yellow> at line <green>%s</green>',$token[1],$token[2])); // make verbose mode |
||
157 | $returnTokens['value'] = ['token'=>$token,'index'=>$key]; |
||
158 | return $returnTokens; |
||
159 | } |
||
160 | $nextKey = true; |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | |||
165 | // keep track of closing brackets so we can ignore things |
||
166 | if ($token == ']') { |
||
167 | $depth--; |
||
168 | |||
169 | if ($depth === 1) { |
||
170 | unset($labelLine); // clear line number once we detect the end |
||
171 | } |
||
176 |