Completed
Push — master ( 247d83...132dc2 )
by Tom
02:31
created

Template::getClonedElement()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 3
eloc 4
nc 2
nop 2
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\TSSFunction;
8
/* Handles template() function calls from the stylesheet */
9
class Template implements \Transphporm\TSSFunction {
10
	private $selector;
0 ignored issues
show
Unused Code introduced by
The property $selector is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
11
	private $elementData;
12
	private $baseDir;
13
14
	public function __construct(\Transphporm\Hook\ElementData $elementData, &$baseDir) {
15
		$this->baseDir = &$baseDir;
16
		$this->elementData = $elementData;	
17
	}
18
19
	private function readArray($array, $index) {
20
		return isset($array[$index]) ? $array[$index] : null;
21
	}
22
23
	public function run(array $args, \DomElement $element) {
24
		$selector = $this->readArray($args, 1);
25
		$tss = $this->baseDir . $this->readArray($args, 2);
26
		
27
		$newTemplate = new \Transphporm\Builder($this->baseDir . $args[0], $tss);
28
29
		$doc = $newTemplate->output($this->elementData->getData($element), true)->body;
30
		if ($selector != '') return $this->templateSubsection($doc, $selector);
31
		
32
		$newNode = $doc->documentElement;
33
		$result = [];
34
		if ($newNode->tagName === 'template') {
35
			foreach ($newNode->childNodes as $node) {
36
				$result[] = $this->getClonedElement($node, $tss);
37
			}
38
		}
39
		//else $result[] = $newNode;
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
40
		return $result;
41
42
	}
43
44
	private function templateSubsection($doc, $selector) {
45
		$xpathStr = (new \Transphporm\Parser\CssToXpath($selector, new \Transphporm\Parser\Value($this)))->getXpath();
46
		$xpath = new \DomXpath($doc);
47
		$nodes = $xpath->query($xpathStr);
48
		$result = [];
49
		foreach ($nodes as $node) {
50
			$result[] = $node;
51
		}
52
		return $result;
53
	}
54
55
	private function getClonedElement($node, $tss) {
56
		$clone = $node->cloneNode(true);
57
		if ($tss !== null && $clone instanceof \DomElement) $clone->setAttribute('transphporm', 'includedtemplate');
58
		return $clone;
59
	}
60
}