| Conditions | 7 |
| Paths | 5 |
| Total Lines | 53 |
| Code Lines | 30 |
| 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 |
||
| 118 | public static function parseEqualityExpr($expr) |
||
| 119 | { |
||
| 120 | // Match an equality between a variable and a literal or the concatenation of strings |
||
| 121 | $eq = '(?<equality>' |
||
| 122 | . '(?<key>@[-\\w]+|\\$\\w+|\\.)' |
||
| 123 | . '(?<operator>\\s*=\\s*)' |
||
| 124 | . '(?:' |
||
| 125 | . '(?<literal>(?<string>"[^"]*"|\'[^\']*\')|0|[1-9][0-9]*)' |
||
| 126 | . '|' |
||
| 127 | . '(?<concat>concat\\(\\s*(?&string)\\s*(?:,\\s*(?&string)\\s*)+\\))' |
||
| 128 | . ')' |
||
| 129 | . '|' |
||
| 130 | . '(?:(?<literal>(?&literal))|(?<concat>(?&concat)))(?&operator)(?<key>(?&key))' |
||
| 131 | . ')'; |
||
| 132 | |||
| 133 | // Match a string that is entirely composed of equality checks separated with "or" |
||
| 134 | $regexp = '(^(?J)\\s*' . $eq . '\\s*(?:or\\s*(?&equality)\\s*)*$)'; |
||
| 135 | |||
| 136 | if (!preg_match($regexp, $expr)) |
||
| 137 | { |
||
| 138 | return false; |
||
| 139 | } |
||
| 140 | |||
| 141 | preg_match_all("((?J)$eq)", $expr, $matches, PREG_SET_ORDER); |
||
| 142 | |||
| 143 | $map = []; |
||
| 144 | foreach ($matches as $m) |
||
| 145 | { |
||
| 146 | $key = $m['key']; |
||
| 147 | if (!empty($m['concat'])) |
||
| 148 | { |
||
| 149 | preg_match_all('(\'[^\']*\'|"[^"]*")', $m['concat'], $strings); |
||
| 150 | |||
| 151 | $value = ''; |
||
| 152 | foreach ($strings[0] as $string) |
||
| 153 | { |
||
| 154 | $value .= substr($string, 1, -1); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | else |
||
| 158 | { |
||
| 159 | $value = $m['literal']; |
||
| 160 | if ($value[0] === "'" || $value[0] === '"') |
||
| 161 | { |
||
| 162 | $value = substr($value, 1, -1); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | $map[$key][] = $value; |
||
| 167 | } |
||
| 168 | |||
| 169 | return $map; |
||
| 170 | } |
||
| 171 | } |