| Conditions | 27 |
| Paths | 58 |
| Total Lines | 112 |
| Code Lines | 65 |
| Lines | 8 |
| Ratio | 7.14 % |
| 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 |
||
| 99 | private function expandMatch(array $matches) |
||
| 100 | { |
||
| 101 | static $rfc1738to3986 = array('+' => '%20', '%7e' => '~'); |
||
| 102 | |||
| 103 | $replacements = array(); |
||
| 104 | $parsed = self::parseExpression($matches[1]); |
||
| 105 | $prefix = self::$operatorHash[$parsed['operator']]['prefix']; |
||
| 106 | $joiner = self::$operatorHash[$parsed['operator']]['joiner']; |
||
| 107 | $useQuery = self::$operatorHash[$parsed['operator']]['query']; |
||
| 108 | |||
| 109 | foreach ($parsed['values'] as $value) { |
||
| 110 | |||
| 111 | if (!isset($this->variables[$value['value']])) { |
||
| 112 | continue; |
||
| 113 | } |
||
| 114 | |||
| 115 | $variable = $this->variables[$value['value']]; |
||
| 116 | $actuallyUseQuery = $useQuery; |
||
| 117 | $expanded = ''; |
||
| 118 | |||
| 119 | if (is_array($variable)) { |
||
| 120 | |||
| 121 | $isAssoc = $this->isAssoc($variable); |
||
| 122 | $kvp = array(); |
||
| 123 | foreach ($variable as $key => $var) { |
||
| 124 | |||
| 125 | if ($isAssoc) { |
||
| 126 | $key = rawurlencode($key); |
||
| 127 | $isNestedArray = is_array($var); |
||
| 128 | } else { |
||
| 129 | $isNestedArray = false; |
||
| 130 | } |
||
| 131 | |||
| 132 | if (!$isNestedArray) { |
||
| 133 | $var = rawurlencode($var); |
||
| 134 | if ($parsed['operator'] == '+' || |
||
| 135 | $parsed['operator'] == '#' |
||
| 136 | ) { |
||
| 137 | $var = $this->decodeReserved($var); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | if ($value['modifier'] == '*') { |
||
| 142 | if ($isAssoc) { |
||
| 143 | if ($isNestedArray) { |
||
| 144 | // Nested arrays must allow for deeply nested |
||
| 145 | // structures. |
||
| 146 | $var = strtr( |
||
| 147 | http_build_query([$key => $var]), |
||
| 148 | $rfc1738to3986 |
||
| 149 | ); |
||
| 150 | } else { |
||
| 151 | $var = $key . '=' . $var; |
||
| 152 | } |
||
| 153 | } elseif ($key > 0 && $actuallyUseQuery) { |
||
| 154 | $var = $value['value'] . '=' . $var; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | $kvp[$key] = $var; |
||
| 159 | } |
||
| 160 | |||
| 161 | if (empty($variable)) { |
||
| 162 | $actuallyUseQuery = false; |
||
| 163 | } elseif ($value['modifier'] == '*') { |
||
| 164 | $expanded = implode($joiner, $kvp); |
||
| 165 | if ($isAssoc) { |
||
| 166 | // Don't prepend the value name when using the explode |
||
| 167 | // modifier with an associative array. |
||
| 168 | $actuallyUseQuery = false; |
||
| 169 | } |
||
| 170 | } else { |
||
| 171 | if ($isAssoc) { |
||
| 172 | // When an associative array is encountered and the |
||
| 173 | // explode modifier is not set, then the result must be |
||
| 174 | // a comma separated list of keys followed by their |
||
| 175 | // respective values. |
||
| 176 | foreach ($kvp as $k => &$v) { |
||
| 177 | $v = $k . ',' . $v; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | $expanded = implode(',', $kvp); |
||
| 181 | } |
||
| 182 | |||
| 183 | } else { |
||
| 184 | if ($value['modifier'] == ':') { |
||
| 185 | $variable = substr($variable, 0, $value['position']); |
||
| 186 | } |
||
| 187 | $expanded = rawurlencode($variable); |
||
| 188 | if ($parsed['operator'] == '+' || $parsed['operator'] == '#') { |
||
| 189 | $expanded = $this->decodeReserved($expanded); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | if ($actuallyUseQuery) { |
||
| 194 | if (!$expanded && $joiner != '&') { |
||
| 195 | $expanded = $value['value']; |
||
| 196 | } else { |
||
| 197 | $expanded = $value['value'] . '=' . $expanded; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | $replacements[] = $expanded; |
||
| 202 | } |
||
| 203 | |||
| 204 | $ret = implode($joiner, $replacements); |
||
| 205 | if ($ret && $prefix) { |
||
| 206 | return $prefix . $ret; |
||
| 207 | } |
||
| 208 | |||
| 209 | return $ret; |
||
| 210 | } |
||
| 211 | |||
| 242 |