Not   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 37
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A match() 0 6 2
A notElement() 0 11 3
A matches() 0 8 4
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2017 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.2                                                             */
7
namespace Transphporm\Pseudo;
8
class Not implements \Transphporm\Pseudo {
9
	private $cssToXpath;
10
    private $config;
11
12
	public function __construct(\Transphporm\Parser\CssToXpath $cssToXpath, \Transphporm\Config $config) {
13
		$this->cssToXpath = $cssToXpath;
14
        $this->config = $config;
15
	}
16
17
	public function match($name, $args, \DomElement $element) {
18
		if ($name !== 'not') return true;
19
20
		$xpath = new \DomXpath($element->ownerDocument);
21
		return $this->notElement($args, $xpath, $element);
22
	}
23
24
	private function notElement($css, $xpath, $element) {
25
26
		foreach ($css as $selector) {
27
			$tokenizer = new \Transphporm\Parser\Tokenizer($selector);
28
			$xpathString = $this->cssToXpath->getXpath($tokenizer->getTokens());
29
            $pseudo = $this->cssToXpath->getPseudo($tokenizer->getTokens());
30
            $pseudoMatcher = $this->config->createPseudoMatcher($pseudo);
31
			if ($this->matches($xpath->query($xpathString), $element, $pseudoMatcher)) return false;
32
		}
33
		return true;
34
	}
35
36
    private function matches($foundElements, $element, $pseudoMatcher) {
37
        //Find all nodes matched by the expressions in the brackets :not(EXPR)
38
        foreach ($foundElements as $matchedElement) {
39
            //Check to see whether this node was matched by the not query
40
            if ($pseudoMatcher->matches($matchedElement) && $element->isSameNode($matchedElement)) return true;
41
        }
42
        return false;
43
    }
44
}
45