| Conditions | 8 |
| Paths | 8 |
| Total Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 18 |
| CRAP Score | 8.0093 |
| Changes | 0 | ||
| 1 | <?php |
||
| 28 | 154 | public static function toXPath(string $selector, bool $ignoreCssSelectorErrors = false) |
|
| 29 | { |
||
| 30 | 154 | if (isset(self::$compiled[$selector])) { |
|
| 31 | 94 | return self::$compiled[$selector]; |
|
| 32 | } |
||
| 33 | |||
| 34 | // Select DOMText |
||
| 35 | 94 | if ($selector === 'text') { |
|
| 36 | 3 | return '//text()'; |
|
| 37 | } |
||
| 38 | |||
| 39 | // Select DOMComment |
||
| 40 | 91 | if ($selector === 'comment') { |
|
| 41 | 2 | return '//comment()'; |
|
| 42 | } |
||
| 43 | |||
| 44 | 89 | if (\strpos($selector, '//') === 0) { |
|
| 45 | 11 | return $selector; |
|
| 46 | } |
||
| 47 | |||
| 48 | 80 | if (!\class_exists(CssSelectorConverter::class)) { |
|
| 49 | throw new \RuntimeException('Unable to filter with a CSS selector as the Symfony CssSelector 2.8+ is not installed (you can use filterXPath instead).'); |
||
| 50 | } |
||
| 51 | |||
| 52 | 80 | $converter = new CssSelectorConverter(true); |
|
| 53 | |||
| 54 | 80 | if ($ignoreCssSelectorErrors) { |
|
| 55 | try { |
||
| 56 | 4 | $xPathQuery = $converter->toXPath($selector); |
|
| 57 | 1 | } catch (\Exception $e) { |
|
| 58 | 4 | $xPathQuery = $selector; |
|
| 59 | } |
||
| 60 | } else { |
||
| 61 | 76 | $xPathQuery = $converter->toXPath($selector); |
|
| 62 | } |
||
| 63 | |||
| 64 | 80 | self::$compiled[$selector] = $xPathQuery; |
|
| 65 | |||
| 66 | 80 | return $xPathQuery; |
|
| 67 | } |
||
| 68 | } |
||
| 69 |