Completed
Push — master ( 01d92b...e4ee10 )
by Lars
05:00
created

SelectorConverter   A

Complexity

Total Complexity 6

Size/Duplication

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