Completed
Push — master ( 19ad78...9c7a65 )
by Tom
02:40
created

Repeat::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
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\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($value, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) {
18
		if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element);
19
20
		$rule = $rules['repeat'];
0 ignored issues
show
Unused Code introduced by
$rule is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
21
22
		$parts = explode(' ', trim($rules['repeat']));
23
		$max = isset($parts[1]) ? $parts[1] : PHP_INT_MAX;
24
25
		$count = 0;
26
		foreach ($value as $key => $iteration) {
27
			$clone = $element->cloneNode(true);
28
			//Mark all but one of the nodes as having been added by transphporm, when the hook is run again, these are removed
29
			if ($count++ > 0) $clone->setAttribute('transphporm', 'added');
30
			if ($count > $max) break;
31
			
32
			$this->elementData->bind($clone, $iteration, 'iteration');
33
			$this->elementData->bind($clone, $key, 'key');
34
			$element->parentNode->insertBefore($clone, $element);
35
36
			//Re-run the hook on the new element, but use the iterated data
37
			//Don't run repeat on the clones element or it will loop forever
38
			unset($rules['repeat']);
39
			$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...
40
		}
41
		//Remove the original element
42
		$element->parentNode->removeChild($element);
43
		return false;
44
	}
45
46
	private function createHook($newRules, $pseudoMatcher, $properties) {
47
		$hook = new \Transphporm\Hook\PropertyHook($newRules, $pseudoMatcher, new \Transphporm\Parser\Value($this->functionSet));
48
		foreach ($properties as $name => $property) $hook->registerProperty($name, $property);
49
		return $hook;
50
	}
51
}