Completed
Push — master ( eac26a...73e1e5 )
by Tom
02:32
created

Repeat   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
c 4
b 0
f 0
lcom 1
cbo 2
dl 0
loc 37
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B run() 0 23 4
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 $data;
10
11
	public function __construct($data) {
12
		$this->data = $data;
13
	}
14
15
	public function run($value, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) {
16
		if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element);
17
18
		$count = 0;
19
		foreach ($value as $key => $iteration) {
20
			$clone = $element->cloneNode(true);
21
			//Mark all but one of the nodes as having been added by transphporm, when the hook is run again, these are removed
22
			if ($count > 0) $clone->setAttribute('transphporm', 'added');
23
			$this->data->bind($clone, $iteration, 'iteration');
24
			$this->data->bind($clone, $key, 'key');
25
			$element->parentNode->insertBefore($clone, $element);
26
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
31
			$this->createHook($rules, $pseudoMatcher, $properties)->run($clone);
0 ignored issues
show
Compatibility introduced by
$clone of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
32
			$count++;
33
		}
34
		//Remove the original element
35
		$element->parentNode->removeChild($element);
36
		return false;
37
	}
38
39
	private function createHook($newRules, $pseudoMatcher, $properties) {
40
		$hook = new \Transphporm\Hook\PropertyHook($newRules, $pseudoMatcher, new \Transphporm\Parser\Value($this->data));
41
		foreach ($properties as $name => $property) $hook->registerProperty($name, $property);
42
		return $hook;
43
	}
44
}