Completed
Pull Request — master (#18)
by Auke
03:42
created

xml   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 4 Features 5
Metric Value
wmc 7
c 10
b 4
f 5
lcom 0
cbo 2
dl 0
loc 86
ccs 32
cts 32
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __callStatic() 0 7 2
A parse() 0 5 1
A css2XPath() 0 56 4
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
/**
15
 * This class contains the parse and css2XPath methods.
16
 * In addition any other method statically called on this class
17
 * will reroute the call to the XML writer instance at 
18
 * \arc\xml::$writer. It is automatically instantiated if needed.
19
 * Or you can set it yourself to another Writer instance.
20
 */
21
class xml
22
{
23
    static $writer = null;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $writer.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
24
25 3
    public static function __callStatic( $name, $args )
26
    {
27 3
        if ( !isset(self::$writer) ) {
28 1
            self::$writer = new xml\Writer();
29 1
        }
30 3
        return call_user_func_array( [ self::$writer, $name ], $args );
31
    }
32
33
    /**
34
     * This parses an XML string and returns a Proxy
35
     * @param string|Proxy $xml
36
     * @return Proxy
37
     * @throws \arc\Exception
38
     */
39 5
    public static function parse( $xml=null, $encoding = null )
40
    {
41 5
        $parser = new xml\Parser();
42 5
        return $parser->parse( $xml, $encoding );
43
    }
44
45
    /**
46
     * This method turns a single CSS 2 selector into an XPath query
47
     * @param string $cssSelector
48
     * @return string
49
     */
50 2
    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...
51
    {
52
        /* based on work by Tijs Verkoyen - http://blog.verkoyen.eu/blog/p/detail/css-selector-to-xpath-query/ */
53
        $translateList = array(
54
            // E F: Matches any F element that is a descendant of an E element
55
            '/(\w+)\s+(?=([^"]*"[^"]*")*[^"]*$)(\w+)/'
56 2
            => '\1//\3',
57
            // E > F: Matches any F element that is a child of an element E
58
            '/(\w+)\s*>\s*(\w+)/'
59 2
            => '\1/\2',
60
            // E:first-child: Matches element E when E is the first child of its parent
61
            '/(\w+|\*):first-child/'
62 2
            => '*[1]/self::\1',
63
            // 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...
64
            '/(\w+|\*):(checked|disabled|selected)/'
65 2
            => '\1 [ @\2 ]',
66
            // E + F: Matches any F element immediately preceded by an element
67
            '/(\w+)\s*\+\s*(\w+)/'
68 2
            => '\1/following-sibling::*[1]/self::\2',
69
            // E ~ F: Matches any F element preceded by an element
70
            '/(\w+)\s*\~\s*(\w+)/'
71 2
            => '\1/following-sibling::*/self::\2',
72
            // E[foo]: Matches any E element with the "foo" attribute set (whatever the value)
73
            '/(\w+)\[([\w\-]+)]/'
74 2
            => '\1 [ @\2 ]',
75
            // E[foo="warning"]: Matches any E element whose "foo" attribute value is exactly equal to "warning"
76
            '/(\w+)\[([\w\-]+)\=\"(.*)\"]/'
77 2
            => '\1[ contains( concat( " ", normalize-space(@\2), " " ), concat( " ", "\3", " " ) ) ]',
78
            // .warning: HTML only. The same as *[class~="warning"]
79
            '/(^|\s)\.([\w\-]+)+/'
80 2
            => '*[ contains( concat( " ", normalize-space(@class), " " ), concat( " ", "\2", " " ) ) ]',
81
            // div.warning: HTML only. The same as DIV[class~="warning"]
82
            '/(\w+|\*)\.([\w\-]+)+/'
83 2
            => '\1[ contains( concat( " ", normalize-space(@class), " " ), concat( " ", "\2", " " ) ) ]',
84
            // E#myid: Matches any E element with id-attribute equal to "myid"
85
            '/(\w+)+\#([\w\-]+)/'
86 2
            => '\1[ @id = "\2" ]',
87
            // #myid: Matches any E element with id-attribute equal to "myid"
88
            '/\#([\w\-]+)/'
89
            => '*[ @id = "\1" ]'
90 2
        );
91
92 2
        $cssSelectors = array_keys($translateList);
93 2
        $xPathQueries = array_values($translateList);
94
        do {
95 2
            $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...
96 2
            $cssSelector = (string) preg_replace($cssSelectors, $xPathQueries, $cssSelector);
97 2
            foreach ( $cssSelectors as $selector ) {
98 2
                if ( preg_match($selector, $cssSelector) ) {
99 1
                    $continue = true;
100 1
                    break;
101
                }
102 2
            }
103 2
        } while ( $continue );
104 2
        return '//'.$cssSelector;
105
    }
106
}
107