Completed
Push — master ( 09a120...247d83 )
by Tom
02:21
created

TemplateFunction::getTss()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 2
eloc 2
nc 2
nop 1
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
/* Handles template() function calls from the stylesheet */
9
class TemplateFunction {
10
	private $xml;
11
	private $selector;
12
	private $tss;
13
14
	public function __construct($xml, $selector = null, $tss = null) {
15
		$this->xml = $xml;
16
		$this->selector = $selector;
17
		$this->tss = $tss;
18
	}
19
20
	private function templateSubsection($doc) {
21
		$xpathStr = (new \Transphporm\Parser\CssToXpath($this->selector, new \Transphporm\Parser\Value($this)))->getXpath();
22
		$xpath = new \DomXpath($doc);
23
		$nodes = $xpath->query($xpathStr);
24
		$result = [];
25
		foreach ($nodes as $node) {
26
			$result[] = $node;
27
		}
28
		return $result;
29
	}
30
31
	private function getTss($val) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
32
		return isset($val[2]) ? $val[2] : null;
33
	}
34
35
	private function getClonedElement($node) {
36
		$clone = $node->cloneNode(true);
37
		if ($this->tss !== null && $clone instanceof \DomElement) $clone->setAttribute('transphporm', 'includedtemplate');
38
		return $clone;
39
	}
40
41
	public function getTemplateNodes($data) {
42
		$newTemplate = new \Transphporm\Builder($this->xml, $this->tss);
43
44
		$doc = $newTemplate->output($data, true)->body;
45
		if ($this->selector != '') return $this->templateSubsection($doc);
46
		
47
		$newNode = $doc->documentElement;
48
		$result = [];
49
		if ($newNode->tagName === 'template') {
50
			foreach ($newNode->childNodes as $node) {
51
				$result[] = $this->getClonedElement($node);
52
			}
53
		}
54
		//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...
55
		return $result;
56
	}
57
}