Completed
Pull Request — master (#17)
by Auke
10:01
created

xml::css2XPath()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 56
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 5
Bugs 3 Features 3
Metric Value
c 5
b 3
f 3
dl 0
loc 56
ccs 4
cts 4
cp 1
rs 9.0544
cc 4
eloc 37
nc 3
nop 1
crap 4

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
/*
4
 * This file is part of the Ariadne Component Library.
5
 *
6
 * (c) Muze <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace arc;
13
14
class xml
15
{
16
17 2
    public static function __callStatic( $name, $args )
18
    {
19 2
        return call_user_func_array( [ new xml\Writer(), $name ], $args );
20
    }
21
22 2
    public static function parse( $xml, $encoding = null )
23
    {
24 2
        $parser = new xml\Parser();
25 2
        return $parser->parse( $xml, $encoding );
26
    }
27
28 1
    public static function css2XPath( $cssSelector )
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
29
    {
30
        /* based on work by Tijs Verkoyen - http://blog.verkoyen.eu/blog/p/detail/css-selector-to-xpath-query/ */
31
        $translateList = array(
32
            // E F: Matches any F element that is a descendant of an E element
33 1
            '/(\w+)\s+(?=([^"]*"[^"]*")*[^"]*$)(\w+)/'
34
            => '\1//\3',
35
            // E > F: Matches any F element that is a child of an element E
36
            '/(\w+)\s*>\s*(\w+)/'
37
            => '\1/\2',
38
            // E:first-child: Matches element E when E is the first child of its parent
39
            '/(\w+|\*):first-child/'
40
            => '*[1]/self::\1',
41
            // E:checked or E:disabled or E:selected
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42
            '/(\w+|\*):(checked|disabled|selected)/'
43
            => '\1 [ @\2 ]',
44
            // E + F: Matches any F element immediately preceded by an element
45
            '/(\w+)\s*\+\s*(\w+)/'
46
            => '\1/following-sibling::*[1]/self::\2',
47
            // E ~ F: Matches any F element preceded by an element
48
            '/(\w+)\s*\~\s*(\w+)/'
49
            => '\1/following-sibling::*/self::\2',
50
            // E[foo]: Matches any E element with the "foo" attribute set (whatever the value)
51
            '/(\w+)\[([\w\-]+)]/'
52
            => '\1 [ @\2 ]',
53 1
            // E[foo="warning"]: Matches any E element whose "foo" attribute value is exactly equal to "warning"
54
            '/(\w+)\[([\w\-]+)\=\"(.*)\"]/'
55
            => '\1[ contains( concat( " ", normalize-space(@\2), " " ), concat( " ", "\3", " " ) ) ]',
56
            // .warning: HTML only. The same as *[class~="warning"]
57
            '/(^|\s)\.([\w\-]+)+/'
58
            => '*[ contains( concat( " ", normalize-space(@class), " " ), concat( " ", "\2", " " ) ) ]',
59
            // div.warning: HTML only. The same as DIV[class~="warning"]
60
            '/(\w+|\*)\.([\w\-]+)+/'
61
            => '\1[ contains( concat( " ", normalize-space(@class), " " ), concat( " ", "\2", " " ) ) ]',
62
            // E#myid: Matches any E element with id-attribute equal to "myid"
63
            '/(\w+)+\#([\w\-]+)/'
64 1
            => '\1[ @id = "\2" ]',
65
            // #myid: Matches any E element with id-attribute equal to "myid"
66
            '/\#([\w\-]+)/'
67
            => '*[ @id = "\1" ]'
68
        );
69
70
        $cssSelectors = array_keys($translateList);
71
        $xPathQueries = array_values($translateList);
72
        do {
73
            $continue = false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
74
            $cssSelector = (string) preg_replace($cssSelectors, $xPathQueries, $cssSelector);
75
            foreach ( $cssSelectors as $selector ) {
76
                if ( preg_match($selector, $cssSelector) ) {
77
                    $continue = true;
78
                    break;
79
                }       
80
            }
81
        } while ( $continue );
82
        return '//'.$cssSelector;
83
    }
84
}
85