PropertyHook::callProperties()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 3
nc 3
nop 1
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\Hook;
8
/** Hooks into the template system, gets assigned as `ul li` or similar and `run()` is called with any elements that match */
9
class PropertyHook implements \Transphporm\Hook {
10
	private $rules;
11
	private $configLine;
12
	private $file;
13
    private $filePath;
14
	private $line;
15
	private $valueParser;
16
	private $pseudoMatcher;
17
	private $properties = [];
18
	private $functionSet;
19
20 View Code Duplication
	public function __construct(array $rules, &$configLine, $file, $line, PseudoMatcher $pseudoMatcher,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
            \Transphporm\Parser\Value $valueParser, \Transphporm\FunctionSet $functionSet, \Transphporm\FilePath $filePath) {
22
		$this->rules = $rules;
23
		$this->configLine = &$configLine;
24
		$this->file = $file;
25
        $this->filePath = $filePath;
26
		$this->line = $line;
27
		$this->valueParser = $valueParser;
28
		$this->pseudoMatcher = $pseudoMatcher;
29
		$this->functionSet = $functionSet;
30
	}
31
32
	public function runOnImmutableElements(): bool {
33
		return false;
34
	}
35
36
	public function run(\DomElement $element) {
37
		//Set the baseDir so that all files for this rule are relative to the file it came from
38
        if ($this->file !== null) $this->filePath->setBaseDir(dirname(realpath($this->file)));
39
		$this->functionSet->setElement($element);
40
		$this->configLine = $this->line;
41
		try {
42
			//Don't run if there's a pseudo element like nth-child() and this element doesn't match it
43
			if (!$this->pseudoMatcher->matches($element)) return;
44
			$this->callProperties($element);
45
		}
46
		catch (\Transphporm\RunException $e) {
47
			throw new \Transphporm\Exception($e, $this->file, $this->line);
48
		}
49
	}
50
51
	// TODO: Have all rule values parsed before running them so that things like `content-append` are not expecting tokens
52
	// problem with this is that anything in data changed by run properties is not shown
53
	// TODO: Allow `update-frequency` to be parsed before it is accessed in rule (might need to switch location of rule check)
54
	private function callProperties($element) {
55
		foreach ($this->rules as $name => $value) {
56
			$result = $this->callProperty($name, $element, $this->getArgs($value));
57
			if ($result === false) break;
58
		}
59
	}
60
	private function getArgs($value) {
61
		return $this->valueParser->parseTokens($value);
62
	}
63
64
	public function registerProperty($name, \Transphporm\Property $property) {
65
		$this->properties[$name] = $property;
66
	}
67
68
	private function callProperty($name, $element, $value) {
69
		if (isset($this->properties[$name])) {
70
			try {
71
				return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties);
72
			}
73
			catch (\Exception $e) {
74
				if ($e instanceof \Transphporm\RunException) throw $e;
75
				throw new \Transphporm\RunException(\Transphporm\Exception::PROPERTY, $name, $e);
76
			}
77
		}
78
	}
79
}
80