| 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; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  | 
            
                        
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: