| Conditions | 12 |
| Paths | 2 |
| Total Lines | 54 |
| Code Lines | 23 |
| 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 |
||
| 104 | public function parse($data) { |
||
| 105 | $info = array(); |
||
| 106 | |||
| 107 | if (preg_match_all(' |
||
| 108 | @^\s* # Start at the beginning of a line, ignoring leading whitespace |
||
| 109 | ((?: |
||
| 110 | [^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets, |
||
| 111 | \[[^\[\]]*\] # unless they are balanced and not nested |
||
| 112 | )+?) |
||
| 113 | \s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space) |
||
| 114 | (?: |
||
| 115 | ("(?:[^"]|(?<=\\\\)")*")| # Double-quoted string, which may contain slash-escaped quotes/slashes |
||
| 116 | (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes |
||
| 117 | ([^\r\n]*?) # Non-quoted string |
||
| 118 | )\s*$ # Stop at the next end of a line, ignoring trailing whitespace |
||
| 119 | @msx', $data, $matches, PREG_SET_ORDER)) { |
||
| 120 | foreach ($matches as $match) { |
||
| 121 | // Fetch the key and value string. |
||
| 122 | $i = 0; |
||
| 123 | foreach (['key', 'value1', 'value2', 'value3'] as $var) { |
||
| 124 | $$var = isset($match[++$i]) ? $match[$i] : ''; |
||
| 125 | } |
||
| 126 | $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3; |
||
| 127 | |||
| 128 | // Parse array syntax. |
||
| 129 | $keys = preg_split('/\]?\[/', rtrim($key, ']')); |
||
| 130 | $last = array_pop($keys); |
||
| 131 | $parent = &$info; |
||
| 132 | |||
| 133 | // Create nested arrays. |
||
| 134 | foreach ($keys as $key) { |
||
| 135 | if ($key == '') { |
||
| 136 | $key = count($parent); |
||
| 137 | } |
||
| 138 | if (!isset($parent[$key]) || !is_array($parent[$key])) { |
||
| 139 | $parent[$key] = []; |
||
| 140 | } |
||
| 141 | $parent = &$parent[$key]; |
||
| 142 | } |
||
| 143 | |||
| 144 | // Handle PHP constants. |
||
| 145 | if (preg_match('/^\w+$/i', $value) && defined($value)) { |
||
| 146 | $value = constant($value); |
||
| 147 | } |
||
| 148 | |||
| 149 | // Insert actual value. |
||
| 150 | if ($last == '') { |
||
| 151 | $last = count($parent); |
||
| 152 | } |
||
| 153 | $parent[$last] = $value; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | return $info; |
||
| 158 | } |
||
| 160 | } |