Completed
Push — master ( 60197c...2a5e10 )
by Lars
02:03
created

SelectorConverter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 41
ccs 14
cts 15
cp 0.9333
rs 10

1 Method

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