| Conditions | 4 |
| Paths | 3 |
| Total Lines | 56 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 25 |
| CRAP Score | 4 |
| Changes | 6 | ||
| Bugs | 3 | Features | 3 |
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 |
||
| 53 | 2 | public static function css2XPath( $cssSelector ) |
|
| 54 | 1 | { |
|
| 55 | /* based on work by Tijs Verkoyen - http://blog.verkoyen.eu/blog/p/detail/css-selector-to-xpath-query/ */ |
||
| 56 | $translateList = array( |
||
| 57 | // E F: Matches any F element that is a descendant of an E element |
||
| 58 | '/(\w+)\s+(?=([^"]*"[^"]*")*[^"]*$)(\w+)/' |
||
| 59 | 2 | => '\1//\3', |
|
| 60 | // E > F: Matches any F element that is a child of an element E |
||
| 61 | '/(\w+)\s*>\s*(\w+)/' |
||
| 62 | 2 | => '\1/\2', |
|
| 63 | // E:first-child: Matches element E when E is the first child of its parent |
||
| 64 | '/(\w+|\*):first-child/' |
||
| 65 | 2 | => '*[1]/self::\1', |
|
| 66 | // E:checked, E:disabled or E:selected |
||
|
1 ignored issue
–
show
|
|||
| 67 | '/(\w+|\*):(checked|disabled|selected)/' |
||
| 68 | 2 | => '\1 [ @\2 ]', |
|
| 69 | // E + F: Matches any F element immediately preceded by an element |
||
| 70 | '/(\w+)\s*\+\s*(\w+)/' |
||
| 71 | 2 | => '\1/following-sibling::*[1]/self::\2', |
|
| 72 | // E ~ F: Matches any F element preceded by an element |
||
| 73 | '/(\w+)\s*\~\s*(\w+)/' |
||
| 74 | 2 | => '\1/following-sibling::*/self::\2', |
|
| 75 | // E[foo]: Matches any E element with the "foo" attribute set (whatever the value) |
||
| 76 | '/(\w+)\[([\w\-]+)]/' |
||
| 77 | 2 | => '\1 [ @\2 ]', |
|
| 78 | // E[foo="warning"]: Matches any E element whose "foo" attribute value is exactly equal to "warning" |
||
| 79 | '/(\w+)\[([\w\-]+)\=\"(.*)\"]/' |
||
| 80 | 2 | => '\1[ contains( concat( " ", normalize-space(@\2), " " ), concat( " ", "\3", " " ) ) ]', |
|
| 81 | // .warning: HTML only. The same as *[class~="warning"] |
||
| 82 | '/(^|\s)\.([\w\-]+)+/' |
||
| 83 | 2 | => '*[ contains( concat( " ", normalize-space(@class), " " ), concat( " ", "\2", " " ) ) ]', |
|
| 84 | // div.warning: HTML only. The same as DIV[class~="warning"] |
||
| 85 | '/(\w+|\*)\.([\w\-]+)+/' |
||
| 86 | 2 | => '\1[ contains( concat( " ", normalize-space(@class), " " ), concat( " ", "\2", " " ) ) ]', |
|
| 87 | // E#myid: Matches any E element with id-attribute equal to "myid" |
||
| 88 | '/(\w+)+\#([\w\-]+)/' |
||
| 89 | 2 | => '\1[ @id = "\2" ]', |
|
| 90 | // #myid: Matches any E element with id-attribute equal to "myid" |
||
| 91 | '/\#([\w\-]+)/' |
||
| 92 | => '*[ @id = "\1" ]' |
||
| 93 | 2 | ); |
|
| 94 | |||
| 95 | 2 | $cssSelectors = array_keys($translateList); |
|
| 96 | 2 | $xPathQueries = array_values($translateList); |
|
| 97 | do { |
||
| 98 | 2 | $continue = false; |
|
| 99 | 2 | $cssSelector = (string) preg_replace($cssSelectors, $xPathQueries, $cssSelector); |
|
| 100 | 2 | foreach ( $cssSelectors as $selector ) { |
|
| 101 | 2 | if ( preg_match($selector, $cssSelector) ) { |
|
| 102 | 1 | $continue = true; |
|
| 103 | 1 | break; |
|
| 104 | } |
||
| 105 | 2 | } |
|
| 106 | 2 | } while ( $continue ); |
|
| 107 | 2 | return '//'.$cssSelector; |
|
| 108 | } |
||
| 109 | } |
||
| 110 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.