| Conditions | 5 |
| Paths | 6 |
| Total Lines | 78 |
| Code Lines | 45 |
| 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 |
||
| 169 | public static function selector2XPath($selector) |
||
| 170 | { |
||
| 171 | // remove spaces around operators |
||
| 172 | $selector = preg_replace('/\s*>\s*/', '>', $selector); |
||
| 173 | $selector = preg_replace('/\s*~\s*/', '~', $selector); |
||
| 174 | $selector = preg_replace('/\s*\+\s*/', '+', $selector); |
||
| 175 | $selector = preg_replace('/\s*,\s*/', ',', $selector); |
||
| 176 | $selectors = preg_split('/\s+(?![^\[]+\])/', $selector); |
||
| 177 | foreach ($selectors as &$selector) { |
||
| 178 | // , |
||
| 179 | $selector = preg_replace('/,/', '|descendant-or-self::', $selector); |
||
| 180 | // input:checked, :disabled, etc. |
||
| 181 | $selector = preg_replace('/(.+)?:(checked|disabled|required|autofocus)/', '\1[@\2="\2"]', $selector); |
||
| 182 | // input:autocomplete, :autocomplete |
||
| 183 | $selector = preg_replace('/(.+)?:(autocomplete)/', '\1[@\2="on"]', $selector); |
||
| 184 | // input:button, input:submit, etc. |
||
| 185 | $selector = preg_replace('/:(text|password|checkbox|radio|button|submit|reset|file|hidden|image|datetime|datetime-local|date|month|time|week|number|range|email|url|search|tel|color)/', 'input[@type="\1"]', $selector); |
||
| 186 | // foo[id] |
||
| 187 | $selector = preg_replace('/(\w+)\[([_\w-]+[_\w\d-]*)\]/', '\1[@\2]', $selector); |
||
| 188 | // [id] |
||
| 189 | $selector = preg_replace('/\[([_\w-]+[_\w\d-]*)\]/', '*[@\1]', $selector); |
||
| 190 | // foo[id=foo] |
||
| 191 | $selector = preg_replace('/\[([_\w-]+[_\w\d-]*)=[\'"]?(.*?)[\'"]?\]/', '[@\1="\2"]', $selector); |
||
| 192 | // [id=foo] |
||
| 193 | $selector = preg_replace('/^\[/', '*[', $selector); |
||
| 194 | // div#foo |
||
| 195 | $selector = preg_replace('/([_\w-]+[_\w\d-]*)\#([_\w-]+[_\w\d-]*)/', '\1[@id="\2"]', $selector); |
||
| 196 | // #foo |
||
| 197 | $selector = preg_replace('/\#([_\w-]+[_\w\d-]*)/', '*[@id="\1"]', $selector); |
||
| 198 | // div.foo |
||
| 199 | $selector = preg_replace('/([_\w-]+[_\w\d-]*)\.([_\w-]+[_\w\d-]*)/', '\1[contains(concat(" ",@class," ")," \2 ")]', $selector); |
||
| 200 | // .foo |
||
| 201 | $selector = preg_replace('/\.([_\w-]+[_\w\d-]*)/', '*[contains(concat(" ",@class," ")," \1 ")]', $selector); |
||
| 202 | // div:first-child |
||
| 203 | $selector = preg_replace('/([_\w-]+[_\w\d-]*):first-child/', '*/\1[position()=1]', $selector); |
||
| 204 | // div:last-child |
||
| 205 | $selector = preg_replace('/([_\w-]+[_\w\d-]*):last-child/', '*/\1[position()=last()]', $selector); |
||
| 206 | // :first-child |
||
| 207 | $selector = str_replace(':first-child', '*/*[position()=1]', $selector); |
||
| 208 | // :last-child |
||
| 209 | $selector = str_replace(':last-child', '*/*[position()=last()]', $selector); |
||
| 210 | // :nth-last-child |
||
| 211 | $selector = preg_replace('/:nth-last-child\((\d+)\)/', '[position()=(last() - (\1 - 1))]', $selector); |
||
| 212 | // div:nth-child |
||
| 213 | $selector = preg_replace('/([_\w-]+[_\w\d-]*):nth-child\((\d+)\)/', '*/*[position()=\2 and self::\1]', $selector); |
||
| 214 | // :nth-child |
||
| 215 | $selector = preg_replace('/:nth-child\((\d+)\)/', '*/*[position()=\1]', $selector); |
||
| 216 | // :contains(Foo) |
||
| 217 | $selector = preg_replace('/([_\w-]+[_\w\d-]*):contains\((.*?)\)/', '\1[contains(string(.),"\2")]', $selector); |
||
| 218 | // > |
||
| 219 | $selector = preg_replace('/>/', '/', $selector); |
||
| 220 | // ~ |
||
| 221 | $selector = preg_replace('/~/', '/following-sibling::', $selector); |
||
| 222 | // + |
||
| 223 | $selector = preg_replace('/\+([_\w-]+[_\w\d-]*)/', '/following-sibling::\1[position()=1]', $selector); |
||
| 224 | $selector = str_replace(']*', ']', $selector); |
||
| 225 | $selector = str_replace(']/*', ']', $selector); |
||
| 226 | } |
||
| 227 | // ' ' |
||
| 228 | $selector = implode('/descendant::', $selectors); |
||
| 229 | $selector = 'descendant-or-self::' . $selector; |
||
| 230 | // :scope |
||
| 231 | $selector = preg_replace('/(((\|)?descendant-or-self::):scope)/', '.\3', $selector); |
||
| 232 | // $element |
||
| 233 | $sub_selectors = explode(',', $selector); |
||
| 234 | foreach ($sub_selectors as $key => $sub_selector) { |
||
| 235 | $parts = explode('$', $sub_selector); |
||
| 236 | $sub_selector = array_shift($parts); |
||
| 237 | if (count($parts) && preg_match_all('/((?:[^\/]*\/?\/?)|$)/', $parts[0], $matches)) { |
||
| 238 | $results = $matches[0]; |
||
| 239 | $results[] = str_repeat('/..', count($results) - 2); |
||
| 240 | $sub_selector .= implode('', $results); |
||
| 241 | } |
||
| 242 | $sub_selectors[$key] = $sub_selector; |
||
| 243 | } |
||
| 244 | $selector = implode(',', $sub_selectors); |
||
| 245 | |||
| 246 | return $selector; |
||
| 247 | } |
||
| 311 |