Completed
Push — master ( f3b478...5e182e )
by Tom
03:14 queued 14s
created

Repeat::run()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.2
cc 4
eloc 14
nc 4
nop 5
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
	private $line;
12
13
	public function __construct(\Transphporm\FunctionSet $functionSet, \Transphporm\Hook\ElementData $elementData, &$line) {
14
		$this->functionSet = $functionSet;
15
		$this->elementData = $elementData;
16
		$this->line = &$line;
17
	}
18
19
	public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) {
20
		$values = $this->fixEmpty($values);
21
		if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element);
22
		$max = $this->getMax($values);
23
		$count = 0;
24
		$repeat = $this->getRepeatValue($values, $max);
25
		//Don't run repeat on the cloned element or it will loop forever
26
		unset($rules['repeat']);
27
		$hook = $this->createHook($rules, $pseudoMatcher, $properties);
28
29
		foreach ($repeat as $key => $iteration) {
30
			if ($count+1 > $max) break;
31
			$clone = $this->cloneElement($element, $iteration, $key, $count++);
32
			//Re-run the hook on the new element, but use the iterated data
33
			$hook->run($clone);
34
		}
35
		//Remove the original element
36
		$element->parentNode->removeChild($element);
37
		return false;
38
	}
39
40
	private function getRepeatValue($values, &$max) {
41
		$mode = $this->getMode($values);
42
		if ($mode === 'each') $repeat = $values[0];
43
		else if ($mode === 'loop') {
44
			$repeat = range($values[0], $max);
45
			$max++;
46
		}
47
		return $repeat;
0 ignored issues
show
Bug introduced by
The variable $repeat does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
48
	}
49
50
	private function getMode($args) {
51
		return isset($args[2]) ? $args[2] : 'each';
52
	}
53
54
	private function fixEmpty($value) {
55
		if (empty($value[0])) $value[0] = [];
56
		return $value;
57
	}
58
59
	private function cloneElement($element, $iteration, $key, $count) {
60
		$clone = $element->cloneNode(true);
61
		$this->tagElement($clone, $count);
62
63
		$this->elementData->bind($clone, $iteration, 'iteration');
64
		$this->elementData->bind($clone, $key, 'key');
65
		$element->parentNode->insertBefore($clone, $element);
66
		return $clone;
67
	}
68
69
	private function tagElement($element, $count) {
70
		//Mark all but one of the nodes as having been added by transphporm, when the hook is run again, these are removed
71
		if ($count > 0) $element->setAttribute('transphporm', 'added');
72
	}
73
74
	private function getMax($values) {
75
		return isset($values[1]) ? $values[1] : PHP_INT_MAX;
76
	}
77
78
	private function createHook($newRules, $pseudoMatcher, $properties) {
79
		$var = ""; // PropertyHook requires that baseDir be passed by refrence
80
		// and there is no reason to pass it so create $var to avoid errors
81
		$hook = new \Transphporm\Hook\PropertyHook($newRules, $var, $this->line, "", $this->line, $pseudoMatcher, new \Transphporm\Parser\Value($this->functionSet), $this->functionSet);
82
		foreach ($properties as $name => $property) $hook->registerProperty($name, $property);
83
		return $hook;
84
	}
85
}
86