Completed
Push — master ( 7b17ad...a29029 )
by Tom
02:30
created

Repeat   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 15
Bugs 1 Features 0
Metric Value
wmc 12
c 15
b 1
f 0
lcom 1
cbo 3
dl 0
loc 54
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 0 19 4
A cloneElement() 0 9 1
A tagElement() 0 4 2
A getMax() 0 3 2
A createHook() 0 5 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\Property;
8
class Repeat implements \Transphporm\Property {
9
	private $functionSet;
10
	private $elementData;
11
12
	public function __construct(\Transphporm\FunctionSet $functionSet, \Transphporm\Hook\ElementData $elementData) {
13
		$this->functionSet = $functionSet;
14
		$this->elementData = $elementData;
15
	}
16
17
	public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) {
18
		if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element);
19
		$max = $this->getMax($values);
20
		$count = 0;
21
22
		//What was this if statement for? removing it breaks nothing
23
		//if (empty($values[0])) $values[0] = [];
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...
24
		foreach ($values[0] as $key => $iteration) {
25
			if ($count+1 > $max) break;
26
			$clone = $this->cloneElement($element, $iteration, $key, $count++);
27
			//Re-run the hook on the new element, but use the iterated data
28
			//Don't run repeat on the clones element or it will loop forever
29
			unset($rules['repeat']);
30
			$this->createHook($rules, $pseudoMatcher, $properties)->run($clone);
31
		}
32
		//Remove the original element
33
		$element->parentNode->removeChild($element);
34
		return false;
35
	}
36
37
	private function cloneElement($element, $iteration, $key, $count) {
38
		$clone = $element->cloneNode(true);
39
		$this->tagElement($clone, $count);
40
41
		$this->elementData->bind($clone, $iteration, 'iteration');
42
		$this->elementData->bind($clone, $key, 'key');
43
		$element->parentNode->insertBefore($clone, $element);
44
		return $clone;
45
	}
46
47
	private function tagElement($element, $count) {
48
		//Mark all but one of the nodes as having been added by transphporm, when the hook is run again, these are removed
49
		if ($count > 0) $element->setAttribute('transphporm', 'added');
50
	}
51
52
	private function getMax($values) {
53
		return isset($values[1]) ? $values[1] : PHP_INT_MAX;
54
	}
55
56
	private function createHook($newRules, $pseudoMatcher, $properties) {
57
		$hook = new \Transphporm\Hook\PropertyHook($newRules, $pseudoMatcher, new \Transphporm\Parser\Value($this->functionSet), $this->functionSet);
58
		foreach ($properties as $name => $property) $hook->registerProperty($name, $property);
59
		return $hook;
60
	}
61
}
62