| Conditions | 7 |
| Paths | 24 |
| Total Lines | 79 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 30 | public function parse(string $queryString) : array |
||
| 31 | { |
||
| 32 | // allow HTML entities notation |
||
| 33 | $queryString = str_replace('&', '&', $queryString); |
||
| 34 | |||
| 35 | $paramNames = array(); |
||
| 36 | |||
| 37 | // extract parameter names from the query string |
||
| 38 | $result = array(); |
||
| 39 | preg_match_all('/&?([^&]+)=.*/sixU', $queryString, $result, PREG_PATTERN_ORDER); |
||
| 40 | if(isset($result[1])) { |
||
| 41 | $paramNames = $result[1]; |
||
| 42 | } |
||
| 43 | |||
| 44 | // to avoid iterating over the param names, we simply concatenate it |
||
| 45 | $search = implode('', $paramNames); |
||
| 46 | |||
| 47 | // store whether we need to adjust any of the names: |
||
| 48 | // this is true if we find dots or spaces in any of them. |
||
| 49 | $fixRequired = stristr($search, '.') || stristr($search, ' '); |
||
| 50 | |||
| 51 | unset($search); |
||
| 52 | |||
| 53 | $table = array(); |
||
| 54 | |||
| 55 | // A fix is required: replace all parameter names with placeholders, |
||
| 56 | // which do not conflict with parse_str and which will be restored |
||
| 57 | // with the actual parameter names after the parsing. |
||
| 58 | // |
||
| 59 | // It is necessary to do this even before the parsing, to resolve |
||
| 60 | // possible naming conflicts like having both parameters "foo.bar" |
||
| 61 | // and "foo_bar" in the query string: since "foo.bar" would be converted |
||
| 62 | // to "foo_bar", one of the two would be replaced. |
||
| 63 | if($fixRequired) |
||
| 64 | { |
||
| 65 | $counter = 1; |
||
| 66 | $placeholders = array(); |
||
| 67 | foreach($paramNames as $paramName) |
||
| 68 | { |
||
| 69 | // create a unique placeholder name |
||
| 70 | $placeholder = '__PLACEHOLDER'.$counter.'__'; |
||
| 71 | |||
| 72 | // store the placeholder name to replace later |
||
| 73 | $table[$placeholder] = $paramName; |
||
| 74 | |||
| 75 | // add the placeholder to replace in the query string before parsing |
||
| 76 | $placeholders[$paramName.'='] = $placeholder.'='; |
||
| 77 | |||
| 78 | $counter++; |
||
| 79 | } |
||
| 80 | |||
| 81 | // next challenge: replacing the parameter names by placeholders |
||
| 82 | // safely. We sort the list by longest name first, to avoid shorter |
||
| 83 | // parameter names being replaced first that can be part of longer ones. |
||
| 84 | uksort($placeholders, function($a, $b) { |
||
| 85 | return strlen($b) - strlen($a); |
||
| 86 | }); |
||
| 87 | |||
| 88 | // replace all instances with the placeholder |
||
| 89 | $queryString = str_replace(array_keys($placeholders), array_values($placeholders), $queryString); |
||
| 90 | } |
||
| 91 | |||
| 92 | // parse the query string natively |
||
| 93 | $parsed = array(); |
||
| 94 | parse_str($queryString, $parsed); |
||
| 95 | |||
| 96 | // do any of the parameter names need to be fixed? |
||
| 97 | if(!$fixRequired) { |
||
| 98 | return $parsed; |
||
| 99 | } |
||
| 100 | |||
| 101 | $keep = array(); |
||
| 102 | |||
| 103 | foreach($parsed as $name => $value) |
||
| 104 | { |
||
| 105 | $keep[$table[$name]] = $value; |
||
| 106 | } |
||
| 107 | |||
| 108 | return $keep; |
||
| 109 | } |
||
| 111 |