Completed
Push — master ( 6b0cbf...ef4cd6 )
by Lars
02:56
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
/**
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 76
  public static function toXPath($selector)
22
  {
23 76
    if (isset(self::$compiled[$selector])) {
24 57
      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 (!class_exists('Symfony\\Component\\CssSelector\\CssSelectorConverter')) {
38
      throw new \RuntimeException('Unable to filter with a CSS selector as the Symfony CssSelector 2.8+ is not installed (you can use filterXPath instead).');
39
    }
40
41 31
    $converter = new CssSelectorConverter(true);
42
43 31
    $xPathQuery = $converter->toXPath($selector);
44 31
    self::$compiled[$selector] = $xPathQuery;
45
46 31
    return $xPathQuery;
47
  }
48
}
49