| Conditions | 12 |
| Paths | 55 |
| Total Lines | 51 |
| Lines | 22 |
| Ratio | 43.14 % |
| 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 |
||
| 84 | public function getBracketSpan($string, $bracketType, $offset = 0) |
||
| 85 | { |
||
| 86 | // prepare opening and closing bracket according to bracket type |
||
| 87 | View Code Duplication | switch ($bracketType) { |
|
| 88 | case '(': |
||
| 89 | case ')': |
||
| 90 | $openingBracket = '('; |
||
| 91 | $closingBracket = ')'; |
||
| 92 | break; |
||
| 93 | |||
| 94 | case '{': |
||
| 95 | case '}': |
||
| 96 | $openingBracket = '{'; |
||
| 97 | $closingBracket = '}'; |
||
| 98 | break; |
||
| 99 | |||
| 100 | case '[': |
||
| 101 | case ']': |
||
| 102 | $openingBracket = '['; |
||
| 103 | $closingBracket = ']'; |
||
| 104 | break; |
||
| 105 | |||
| 106 | default: |
||
| 107 | throw new \Exception(sprintf('Unrecognized bracket type %s', $bracketType)); |
||
| 108 | } |
||
| 109 | |||
| 110 | // split up the string and analyse it character for character |
||
| 111 | $bracketCounter = null; |
||
| 112 | $stringArray = str_split($string); |
||
| 113 | $strlen = strlen($string); |
||
| 114 | $firstBracket = 0; |
||
| 115 | for ($i = $offset; $i < $strlen; $i++) { |
||
| 116 | // count different bracket types by de- and increasing the counter |
||
| 117 | if ($stringArray[$i] === $openingBracket) { |
||
| 118 | if (is_null($bracketCounter)) { |
||
| 119 | $firstBracket = $i; |
||
| 120 | } |
||
| 121 | $bracketCounter = (int) $bracketCounter + 1; |
||
| 122 | |||
|
|
|||
| 123 | } elseif ($stringArray[$i] === $closingBracket) { |
||
| 124 | $bracketCounter = (int) $bracketCounter - 1; |
||
| 125 | } |
||
| 126 | |||
| 127 | // if we reach 0 again we have a completely enclosed string |
||
| 128 | if ($bracketCounter === 0) { |
||
| 129 | return $i + 1 - $firstBracket; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | return 0; |
||
| 134 | } |
||
| 135 | |||
| 160 |