| Conditions | 12 |
| Paths | 2 |
| Total Lines | 62 |
| Code Lines | 28 |
| 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 |
||
| 68 | protected function parseContent($data) |
||
| 69 | { |
||
| 70 | $info = array(); |
||
| 71 | |||
| 72 | if (preg_match_all( |
||
| 73 | ' |
||
| 74 | @^\s* # Start at the beginning of a line, ignoring leading whitespace |
||
| 75 | ((?: |
||
| 76 | [^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets, |
||
| 77 | \[[^\[\]]*\] # unless they are balanced and not nested |
||
| 78 | )+?) |
||
| 79 | \s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space) |
||
| 80 | (?: |
||
| 81 | ("(?:[^"]|(?<=\\\\)")*")| # Double-quoted string, which may contain slash-escaped quotes/slashes |
||
| 82 | (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes |
||
| 83 | ([^\r\n]*?) # Non-quoted string |
||
| 84 | )\s*$ # Stop at the next end of a line, ignoring trailing whitespace |
||
| 85 | @msx', |
||
| 86 | $data, |
||
| 87 | $matches, |
||
| 88 | PREG_SET_ORDER |
||
| 89 | )) { |
||
| 90 | foreach ($matches as $match) { |
||
| 91 | // Fetch the key and value string. |
||
| 92 | $i = 0; |
||
| 93 | $key = $value1 = $value2 = $value3 = ''; |
||
| 94 | foreach (array('key', 'value1', 'value2', 'value3') as $var) { |
||
| 95 | $$var = isset($match[++$i]) ? $match[$i] : ''; |
||
| 96 | } |
||
| 97 | $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3; |
||
| 98 | |||
| 99 | // Parse array syntax. |
||
| 100 | $keys = preg_split('/\]?\[/', rtrim($key, ']')); |
||
| 101 | $last = array_pop($keys); |
||
| 102 | $parent = &$info; |
||
| 103 | |||
| 104 | // Create nested arrays. |
||
| 105 | foreach ($keys as $key) { |
||
| 106 | if ($key == '') { |
||
| 107 | $key = count($parent); |
||
| 108 | } |
||
| 109 | if (!isset($parent[$key]) || !is_array($parent[$key])) { |
||
| 110 | $parent[$key] = array(); |
||
| 111 | } |
||
| 112 | $parent = &$parent[$key]; |
||
| 113 | } |
||
| 114 | |||
| 115 | // Handle PHP constants. |
||
| 116 | if (preg_match('/^\w+$/i', $value) && defined($value)) { |
||
| 117 | $value = constant($value); |
||
| 118 | } |
||
| 119 | |||
| 120 | // Insert actual value. |
||
| 121 | if ($last == '') { |
||
| 122 | $last = count($parent); |
||
| 123 | } |
||
| 124 | $parent[$last] = $value; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | return $info; |
||
| 129 | } |
||
| 130 | |||
| 141 |