Completed
Pull Request — master (#116)
by Richard
02:35
created

PseudoMatcher   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 19
c 10
b 0
f 0
lcom 1
cbo 3
dl 0
loc 62
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A registerFunction() 0 3 1
A matches() 0 14 4
B getFuncParts() 0 12 6
A hasFunction() 0 5 3
A getFuncArgs() 0 10 4
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2015 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.0                                                             */
7
namespace Transphporm\Hook;
8
use \Transphporm\Parser\Tokenizer;
9
/** Determines whether $element matches the pseudo rule such as nth-child() or [attribute="value"] */
10
class PseudoMatcher {
11
	private $pseudo;
12
	private $valueParser;
13
	private $functionSet;
14
	private $functions = [];
15
16
	public function __construct($pseudo, \Transphporm\Parser\Value $valueParser, \Transphporm\FunctionSet $functionSet) {
17
		$this->pseudo = $pseudo;
18
		$this->valueParser = $valueParser;
19
		$this->functionSet = $functionSet;
20
	}
21
22
	public function registerFunction(\Transphporm\Pseudo $pseudo) {
23
		$this->functions[] = $pseudo;
24
	}
25
26
	public function matches($element) {
27
		$matches = true;
28
		$this->functionSet->setElement($element);
29
30
		foreach ($this->pseudo as $pseudo) {
31
			$tokenizer = new \Transphporm\Parser\Tokenizer($pseudo);
32
			$tokens = $tokenizer->getTokens();
33
			foreach ($this->functions as $function) {
34
				$parts = $this->getFuncParts($tokens);
35
				$matches = $matches && $function->match($parts['name'], $parts['args'], $element);
36
			}
37
		}
38
		return $matches;
39
	}
40
41
	private function getFuncParts($tokens) {
42
		$parts = [];
43
		if ($tokens[0]['type'] === Tokenizer::NAME) $parts['name'] = $tokens[0]['value'];
44
		else $parts['name'] = null;
45
		if ($parts['name'] === null || (isset($tokens[1]) && $tokens[1]['type'] === Tokenizer::OPEN_SQUARE_BRACKET)) {
46
			$parts['name'] = null;
47
			$parts['args'] = $this->valueParser->parseTokens($tokens, $this->functionSet);
48
		}
49
		elseif (isset($tokens[1])) $parts['args'] = $this->valueParser->parseTokens($tokens[1]['value'], $this->functionSet);
50
		else $parts['args'] = [];
51
		return $parts;
52
	}
53
54
	public function hasFunction($name) {
55
		foreach ($this->pseudo as $pseudo) {
56
			if (strpos($pseudo, $name) === 0) return true;
57
		}
58
	}
59
60
	// TODO: Improve the functionality of getFuncArgs and make it similar to when using `match`
61
	public function getFuncArgs($name) {
62
		//$this->functionSet->setElement($element);
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63
		foreach ($this->pseudo as $pseudo) {
64
			if (strpos($pseudo, $name) === 0) {
65
				$tokenizer = new \Transphporm\Parser\Tokenizer($pseudo);
66
				$tokens = $tokenizer->getTokens();
67
				return isset($tokens[1]) ? $tokens[1]['value'][0]['value'] : '';
68
			}
69
		}
70
	}
71
}
72