Completed
Push — master ( 05ef16...3f1b18 )
by Lars
02:53
created

SelectorConverter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 37
ccs 12
cts 13
cp 0.9231
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B toXPath() 0 27 5
1
<?php
2
3
namespace voku\helper;
4
5
use Symfony\Component\CssSelector\CssSelectorConverter;
6
7
class SelectorConverter
8
{
9
  protected static $compiled = array();
10
11
  /**
12
   * @param $selector
13
   *
14
   * @return mixed|string
15
   */
16 54
  public static function toXPath($selector)
17
  {
18 54
    if (isset(self::$compiled[$selector])) {
19 44
      return self::$compiled[$selector];
20
    }
21
22
    // Select DOMText
23 24
    if ($selector === 'text') {
24 3
      return '//text()';
25
    }
26
27
    // Select DOMComment
28 21
    if ($selector === 'comment') {
29 2
      return '//comment()';
30
    }
31
32 19
    if (!class_exists('Symfony\\Component\\CssSelector\\CssSelectorConverter')) {
33
      throw new \RuntimeException('Unable to filter with a CSS selector as the Symfony CssSelector 2.8+ is not installed (you can use filterXPath instead).');
34
    }
35
36 19
    $converter = new CssSelectorConverter(true);
37
38 19
    $xPathQuery = $converter->toXPath($selector);
39 19
    self::$compiled[$selector] = $xPathQuery;
40
41 19
    return $xPathQuery;
42
  }
43
}
44