Completed
Push — master ( da7c37...06f4a4 )
by Tom
03:19
created
src/FunctionSet.php 1 patch
Braces   +4 added lines, -3 removed lines patch added patch discarded remove patch
@@ -26,11 +26,12 @@
 block discarded – undo
26 26
 	private function getArgs0($name, $args) {
27 27
 		if (isset($this->functions[$name]) && !($this->functions[$name] instanceof TSSFunction\Data)) {
28 28
 			$tokens = $args[0];
29
-			if ($tokens->count() == 0) return [];
29
+			if ($tokens->count() == 0) {
30
+				return [];
31
+			}
30 32
 			$parser = new \Transphporm\Parser\Value($this);
31 33
 			return $parser->parseTokens($tokens, $this->elementData->getData($this->element));
32
-		}
33
-		else {
34
+		} else {
34 35
 			return iterator_to_array($args[0]);
35 36
 		}
36 37
 
Please login to merge, or discard this patch.
src/Property/Content.php 1 patch
Braces   +28 added lines, -13 removed lines patch added patch discarded remove patch
@@ -14,7 +14,9 @@  discard block
 block discarded – undo
14 14
 	}
15 15
 
16 16
 	public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) {
17
-		if (!$this->shouldRun($element)) return false;
17
+		if (!$this->shouldRun($element)) {
18
+			return false;
19
+		}
18 20
 
19 21
 		$values = $this->formatter->format($values, $rules);
20 22
 
@@ -22,14 +24,19 @@  discard block
 block discarded – undo
22 24
 			//Remove the current contents
23 25
 			$this->removeAllChildren($element);
24 26
 			//Now make a text node
25
-			if ($this->getContentMode($rules) === 'replace') $this->replaceContent($element, $values);
26
-			else $this->appendContent($element, $values);
27
+			if ($this->getContentMode($rules) === 'replace') {
28
+				$this->replaceContent($element, $values);
29
+			} else {
30
+				$this->appendContent($element, $values);
31
+			}
27 32
 		}
28 33
 	}
29 34
 
30 35
 	private function shouldRun($element) {
31 36
 		do {
32
-			if ($element->getAttribute('transphporm') == 'includedtemplate') return false;
37
+			if ($element->getAttribute('transphporm') == 'includedtemplate') {
38
+				return false;
39
+			}
33 40
 		}
34 41
 		while (($element = $element->parentNode) instanceof \DomElement);
35 42
 		return true;
@@ -56,9 +63,10 @@  discard block
 block discarded – undo
56 63
 	public function getNode($node, $document) {
57 64
 		foreach ($node as $n) {
58 65
 			if (is_array($n)) {
59
-				foreach ($this->getNode($n, $document) as $new) yield $new;
60
-			}
61
-			else {
66
+				foreach ($this->getNode($n, $document) as $new) {
67
+					yield $new;
68
+				}
69
+			} else {
62 70
 				yield $this->convertNode($n, $document);
63 71
 			}
64 72
 		}
@@ -69,9 +77,10 @@  discard block
 block discarded – undo
69 77
 			$new = $document->importNode($node, true);
70 78
 			//Removing this might cause problems with caching...
71 79
 			//$new->setAttribute('transphporm', 'added');
72
-		}
73
-		else {
74
-			if ($node instanceof \DomText) $node = $node->nodeValue;
80
+		} else {
81
+			if ($node instanceof \DomText) {
82
+				$node = $node->nodeValue;
83
+			}
75 84
 			$new = $document->createElement('text');
76 85
 
77 86
 			$new->appendChild($document->createTextNode($node));
@@ -82,14 +91,18 @@  discard block
 block discarded – undo
82 91
 
83 92
 
84 93
 	private function replaceContent($element, $content) {
85
-		if ($element->getAttribute('transphporm') == 'added') return;
94
+		if ($element->getAttribute('transphporm') == 'added') {
95
+			return;
96
+		}
86 97
 		//If this rule was cached, the elements that were added last time need to be removed prior to running the rule again.
87 98
 		if ($element->getAttribute('transphporm')) {
88 99
 			$this->replaceCachedContent($element);
89 100
 		}
90 101
 
91 102
 		foreach ($this->getNode($content, $element->ownerDocument) as $node) {
92
-			if (!$node->getAttribute('transphporm'))  $node->setAttribute('transphporm', 'added');
103
+			if (!$node->getAttribute('transphporm')) {
104
+				$node->setAttribute('transphporm', 'added');
105
+			}
93 106
 			$element->parentNode->insertBefore($node, $element);
94 107
 		}
95 108
 
@@ -123,6 +136,8 @@  discard block
 block discarded – undo
123 136
 	}
124 137
 
125 138
 	private function removeAllChildren($element) {
126
-		while ($element->hasChildNodes()) $element->removeChild($element->firstChild);
139
+		while ($element->hasChildNodes()) {
140
+			$element->removeChild($element->firstChild);
141
+		}
127 142
 	}
128 143
 }
129 144
\ No newline at end of file
Please login to merge, or discard this patch.
src/Builder.php 4 patches
Doc Comments   +4 added lines patch added patch discarded remove patch
@@ -85,6 +85,10 @@
 block discarded – undo
85 85
 	}
86 86
 
87 87
 	//Add a postprocessing hook. This cleans up anything transphporm has added to the markup which needs to be removed
88
+
89
+	/**
90
+	 * @param Template $template
91
+	 */
88 92
 	private function doPostProcessing($template) {
89 93
 		$template->addHook('//*[@transphporm]', new Hook\PostProcess());
90 94
 		return $template;
Please login to merge, or discard this patch.
Braces   +12 added lines, -4 removed lines patch added patch discarded remove patch
@@ -27,7 +27,9 @@  discard block
 block discarded – undo
27 27
 		$this->cache = new Cache(new \ArrayObject());
28 28
 		$this->filePath = new FilePath();
29 29
 		$modules = is_array($modules) ? $modules : $this->defaultModules;
30
-		foreach ($modules as $module) $this->loadModule(new $module);
30
+		foreach ($modules as $module) {
31
+			$this->loadModule(new $module);
32
+		}
31 33
 	}
32 34
 
33 35
 	//Allow setting the time used by Transphporm for caching. This is for testing purposes
@@ -80,7 +82,9 @@  discard block
 block discarded – undo
80 82
 		$valueParser = new Parser\Value($functionSet);
81 83
 		$this->config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix(), md5($this->tss)), $this->filePath, $headers);
82 84
 
83
-		foreach ($this->modules as $module) $module->load($this->config);
85
+		foreach ($this->modules as $module) {
86
+			$module->load($this->config);
87
+		}
84 88
 		return $template;
85 89
 	}
86 90
 
@@ -94,7 +98,9 @@  discard block
 block discarded – undo
94 98
 	//Load a template, firstly check if it's a file or a valid string
95 99
 	private function loadTemplate() {
96 100
         $result = ['cache' => $this->template, 'headers' => []];
97
-		if (strpos($this->template, "\n") === false && is_file($this->template)) $result = $this->loadTemplateFromFile($this->template);
101
+		if (strpos($this->template, "\n") === false && is_file($this->template)) {
102
+			$result = $this->loadTemplateFromFile($this->template);
103
+		}
98 104
 		return $result;
99 105
 	}
100 106
 
@@ -113,6 +119,8 @@  discard block
 block discarded – undo
113 119
 
114 120
 	public function __destruct() {
115 121
 		//Required hack as DomXPath can only register static functions clear the statically stored instance to avoid memory leaks
116
-		if (isset($this->config)) $this->config->getCssToXpath()->cleanup();
122
+		if (isset($this->config)) {
123
+			$this->config->getCssToXpath()->cleanup();
124
+		}
117 125
 	}
118 126
 }
Please login to merge, or discard this patch.
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -42,9 +42,9 @@  discard block
 block discarded – undo
42 42
 	}
43 43
 
44 44
 	public function setLocale($locale) {
45
-        $format = new \Transphporm\Module\Format($locale);
46
-        $this->modules[get_class($format)] = $format;
47
-    }
45
+		$format = new \Transphporm\Module\Format($locale);
46
+		$this->modules[get_class($format)] = $format;
47
+	}
48 48
 
49 49
 	public function addPath($dir) {
50 50
 		$this->filePath->addPath($dir);
@@ -97,15 +97,15 @@  discard block
 block discarded – undo
97 97
 
98 98
 	//Load a template, firstly check if it's a file or a valid string
99 99
 	private function loadTemplate() {
100
-        $result = ['cache' => $this->template, 'headers' => []];
100
+		$result = ['cache' => $this->template, 'headers' => []];
101 101
 		if (strpos($this->template, "\n") === false && is_file($this->template)) $result = $this->loadTemplateFromFile($this->template);
102 102
 		return $result;
103 103
 	}
104 104
 
105
-    private function loadTemplateFromFile($file) {
106
-        $xml = $this->cache->load($this->cacheKey . $this->template, filemtime($this->template));
107
-        return $xml ? $xml : ['cache' => file_get_contents($this->template) ?: "", 'headers' => []];
108
-    }
105
+	private function loadTemplateFromFile($file) {
106
+		$xml = $this->cache->load($this->cacheKey . $this->template, filemtime($this->template));
107
+		return $xml ? $xml : ['cache' => file_get_contents($this->template) ?: "", 'headers' => []];
108
+	}
109 109
 
110 110
 	public function setCache(\ArrayAccess $cache) {
111 111
 		$this->cache = new Cache($cache);
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -69,7 +69,7 @@  discard block
 block discarded – undo
69 69
 			   'body' => $this->doPostProcessing($template)->output($document)
70 70
 			];
71 71
 
72
-			$this->cache->write($tssCache->getCacheKey($data) . $this->template, $result);
72
+			$this->cache->write($tssCache->getCacheKey($data).$this->template, $result);
73 73
 		}
74 74
 		unset($result['cache'], $result['renderTime']);
75 75
 		return (object) $result;
@@ -79,7 +79,7 @@  discard block
 block discarded – undo
79 79
 		$elementData = new \Transphporm\Hook\ElementData(new \SplObjectStorage(), $data);
80 80
 		$functionSet = new FunctionSet($elementData);
81 81
 		//To be a valid XML document it must have a root element, automatically wrap it in <template> to ensure it does
82
-		$template = new Template($this->isValidDoc($body) ? str_ireplace('<!doctype', '<!DOCTYPE', $body) : '<template>' . $body . '</template>' );
82
+		$template = new Template($this->isValidDoc($body) ? str_ireplace('<!doctype', '<!DOCTYPE', $body) : '<template>'.$body.'</template>');
83 83
 
84 84
 		$valueParser = new Parser\Value($functionSet);
85 85
 		$this->config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix(), md5($this->tss)), $this->filePath, $headers);
@@ -103,7 +103,7 @@  discard block
 block discarded – undo
103 103
 	}
104 104
 
105 105
     private function loadTemplateFromFile($file) {
106
-        $xml = $this->cache->load($this->cacheKey . $this->template, filemtime($this->template));
106
+        $xml = $this->cache->load($this->cacheKey.$this->template, filemtime($this->template));
107 107
         return $xml ? $xml : ['cache' => file_get_contents($this->template) ?: "", 'headers' => []];
108 108
     }
109 109
 
Please login to merge, or discard this patch.
src/SheetLoader.php 4 patches
Doc Comments   +15 added lines patch added patch discarded remove patch
@@ -15,6 +15,9 @@  discard block
 block discarded – undo
15 15
     private $rules;
16 16
     private $cacheName;
17 17
 
18
+    /**
19
+     * @param string $tss
20
+     */
18 21
     public function __construct(Cache $cache, FilePath $filePath, $tss, $time) {
19 22
     	$this->cache = $cache;
20 23
         $this->filePath = $filePath;
@@ -99,6 +102,9 @@  discard block
 block discarded – undo
99 102
 		return $rules;
100 103
     }
101 104
 
105
+	/**
106
+	 * @param Template $template
107
+	 */
102 108
 	public function processRules($template, \Transphporm\Config $config) {
103 109
 		$rules = $this->getRules($this->tss, $config->getCssToXpath(), $config->getValueParser());
104 110
 
@@ -113,6 +119,11 @@  discard block
 block discarded – undo
113 119
 	}
114 120
 
115 121
 	//Load the TSS
122
+
123
+	/**
124
+	 * @param Parser\CssToXpath $cssToXpath
125
+	 * @param Parser\Value $valueParser
126
+	 */
116 127
 	public function getRules($tss, $cssToXpath, $valueParser, $indexStart = 0) {
117 128
 		if (is_file($tss)) {
118 129
     		//$rules = $this->cache->load($tss);
@@ -124,6 +135,10 @@  discard block
 block discarded – undo
124 135
 		return $tss == null ? [] : (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse($indexStart);
125 136
 	}
126 137
 	//Process a TSS rule e.g. `ul li {content: "foo"; format: bar}
138
+
139
+	/**
140
+	 * @param Config $config
141
+	 */
127 142
 	private function executeTssRule($rule, $template, $config) {
128 143
 		$rule->touch();
129 144
 
Please login to merge, or discard this patch.
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -7,21 +7,21 @@  discard block
 block discarded – undo
7 7
 namespace Transphporm;
8 8
 //Separates out TSS file loading/caching from parsing
9 9
 class SheetLoader {
10
-    private $cache;
11
-    private $sheet;
12
-    private $time;
13
-    private $import = [];
14
-    private $cacheKey;
15
-    private $rules;
16
-    private $cacheName;
17
-
18
-    public function __construct(Cache $cache, FilePath $filePath, $tss, $time) {
19
-    	$this->cache = $cache;
20
-        $this->filePath = $filePath;
21
-        $this->tss = $tss;
22
-        $this->time = $time ?? time();
23
-        $this->cacheName = $tss;
24
-    }
10
+	private $cache;
11
+	private $sheet;
12
+	private $time;
13
+	private $import = [];
14
+	private $cacheKey;
15
+	private $rules;
16
+	private $cacheName;
17
+
18
+	public function __construct(Cache $cache, FilePath $filePath, $tss, $time) {
19
+		$this->cache = $cache;
20
+		$this->filePath = $filePath;
21
+		$this->tss = $tss;
22
+		$this->time = $time ?? time();
23
+		$this->cacheName = $tss;
24
+	}
25 25
 
26 26
 	private function getRulesFromCache($file) {
27 27
 		//Try to load the cached rules, if not set in the cache (or expired) parse the supplied sheet
@@ -90,14 +90,14 @@  discard block
 block discarded – undo
90 90
 	}
91 91
 
92 92
 	//write the sheet to cache
93
-    public function write($file, $rules, $imports = []) {
93
+	public function write($file, $rules, $imports = []) {
94 94
 		if (is_file($file)) {
95 95
 			$existing = $this->cache->load($file, filemtime($file));
96 96
 			if (isset($existing['import']) && empty($imports)) $imports = $existing['import'];
97 97
 			$this->cache->write($this->cacheName, ['rules' => $rules, 'import' => $imports, 'minFreq' => $this->getMinUpdateFreq($rules), 'ctime' => $this->time, 'cacheKey' => $this->cacheKey]);
98 98
 		}
99 99
 		return $rules;
100
-    }
100
+	}
101 101
 
102 102
 	public function processRules($template, \Transphporm\Config $config) {
103 103
 		$rules = $this->getRules($this->tss, $config->getCssToXpath(), $config->getValueParser());
@@ -115,12 +115,12 @@  discard block
 block discarded – undo
115 115
 	//Load the TSS
116 116
 	public function getRules($tss, $cssToXpath, $valueParser, $indexStart = 0) {
117 117
 		if (is_file($tss)) {
118
-    		//$rules = $this->cache->load($tss);
119
-    		$rules = $this->getRulesFromCache($tss)['rules'];
118
+			//$rules = $this->cache->load($tss);
119
+			$rules = $this->getRulesFromCache($tss)['rules'];
120 120
 			$this->filePath->addPath(dirname(realpath($tss)));
121 121
 			if (empty($rules)) $tss = file_get_contents($tss);
122 122
 			else return $rules;
123
-    	}
123
+		}
124 124
 		return $tss == null ? [] : (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse($indexStart);
125 125
 	}
126 126
 	//Process a TSS rule e.g. `ul li {content: "foo"; format: bar}
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -46,7 +46,7 @@  discard block
 block discarded – undo
46 46
 	public function updateRequired($data) {
47 47
 		if (!is_file($this->tss)) return true;
48 48
 
49
-		$this->cacheName = $this->getCacheKey($data) . $this->tss;
49
+		$this->cacheName = $this->getCacheKey($data).$this->tss;
50 50
 
51 51
 		$rules = $this->getRulesFromCache($this->tss, $data);
52 52
 		//Nothing was cached or the TSS file has changed, update is required
@@ -82,8 +82,8 @@  discard block
 block discarded – undo
82 82
 		if (is_file($this->tss)) $this->getRulesFromCache($this->tss);
83 83
 		if ($this->cacheKey) {
84 84
 			$parser = new Parser\Value($data);
85
-			$x= $parser->parseTokens($this->cacheKey)[0];
86
-			$this->cacheName = $x . $this->tss;
85
+			$x = $parser->parseTokens($this->cacheKey)[0];
86
+			$this->cacheName = $x.$this->tss;
87 87
 			return $x;
88 88
 		}
89 89
 		else return '';
@@ -146,6 +146,6 @@  discard block
 block discarded – undo
146 146
 
147 147
 
148 148
 	private function sortPseudo($a, $b) {
149
-		return count($a->pseudo) > count($b->pseudo)  ? 1 : -1;
149
+		return count($a->pseudo) > count($b->pseudo) ? 1 : -1;
150 150
 	}
151 151
 }
Please login to merge, or discard this patch.
Braces   +42 added lines, -15 removed lines patch added patch discarded remove patch
@@ -32,7 +32,9 @@  discard block
 block discarded – undo
32 32
 		if ($rules) {
33 33
 			foreach ($rules['import'] as $file) {
34 34
 				//Check that the import file hasn't been changed since the cache was written
35
-				if (filemtime($file) > $rules['ctime']) return false;
35
+				if (filemtime($file) > $rules['ctime']) {
36
+					return false;
37
+				}
36 38
 			}
37 39
 		}
38 40
 
@@ -44,16 +46,22 @@  discard block
 block discarded – undo
44 46
 	//   2. If the data hasn't changed since the last run
45 47
 	//If this function returns false, the rendered template is sent straight from the cache skipping 99% of transphporm's code
46 48
 	public function updateRequired($data) {
47
-		if (!is_file($this->tss)) return true;
49
+		if (!is_file($this->tss)) {
50
+			return true;
51
+		}
48 52
 
49 53
 		$this->cacheName = $this->getCacheKey($data) . $this->tss;
50 54
 
51 55
 		$rules = $this->getRulesFromCache($this->tss, $data);
52 56
 		//Nothing was cached or the TSS file has changed, update is required
53
-		if (empty($rules)) return true;
57
+		if (empty($rules)) {
58
+			return true;
59
+		}
54 60
 
55 61
 		//Find the sheet's minimum update-frequency, if it hasn't passed then no updates are required
56
-		if ($rules['ctime']+$rules['minFreq'] <= $this->time) return true;
62
+		if ($rules['ctime']+$rules['minFreq'] <= $this->time) {
63
+			return true;
64
+		}
57 65
 
58 66
 		return false;
59 67
 	}
@@ -64,7 +72,9 @@  discard block
 block discarded – undo
64 72
 
65 73
 		foreach ($rules as $rule) {
66 74
 			$ruleFreq = $rule->getUpdateFrequency();
67
-			if ($ruleFreq < $min) $min = $ruleFreq;
75
+			if ($ruleFreq < $min) {
76
+				$min = $ruleFreq;
77
+			}
68 78
 		}
69 79
 
70 80
 		return $min;
@@ -79,21 +89,26 @@  discard block
 block discarded – undo
79 89
 	}
80 90
 
81 91
 	public function getCacheKey($data) {
82
-		if (is_file($this->tss)) $this->getRulesFromCache($this->tss);
92
+		if (is_file($this->tss)) {
93
+			$this->getRulesFromCache($this->tss);
94
+		}
83 95
 		if ($this->cacheKey) {
84 96
 			$parser = new Parser\Value($data);
85 97
 			$x= $parser->parseTokens($this->cacheKey)[0];
86 98
 			$this->cacheName = $x . $this->tss;
87 99
 			return $x;
100
+		} else {
101
+			return '';
88 102
 		}
89
-		else return '';
90 103
 	}
91 104
 
92 105
 	//write the sheet to cache
93 106
     public function write($file, $rules, $imports = []) {
94 107
 		if (is_file($file)) {
95 108
 			$existing = $this->cache->load($file, filemtime($file));
96
-			if (isset($existing['import']) && empty($imports)) $imports = $existing['import'];
109
+			if (isset($existing['import']) && empty($imports)) {
110
+				$imports = $existing['import'];
111
+			}
97 112
 			$this->cache->write($this->cacheName, ['rules' => $rules, 'import' => $imports, 'minFreq' => $this->getMinUpdateFreq($rules), 'ctime' => $this->time, 'cacheKey' => $this->cacheKey]);
98 113
 		}
99 114
 		return $rules;
@@ -106,10 +121,14 @@  discard block
 block discarded – undo
106 121
 		usort($rules, [$this, 'sortRules']);
107 122
 
108 123
 		foreach ($rules as $rule) {
109
-			if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config);
124
+			if ($rule->shouldRun($this->time)) {
125
+				$this->executeTssRule($rule, $template, $config);
126
+			}
110 127
 		}
111 128
 
112
-		if (is_file($this->tss)) $this->write($this->tss, $rules, $this->import);
129
+		if (is_file($this->tss)) {
130
+			$this->write($this->tss, $rules, $this->import);
131
+		}
113 132
 	}
114 133
 
115 134
 	//Load the TSS
@@ -118,8 +137,11 @@  discard block
 block discarded – undo
118 137
     		//$rules = $this->cache->load($tss);
119 138
     		$rules = $this->getRulesFromCache($tss)['rules'];
120 139
 			$this->filePath->addPath(dirname(realpath($tss)));
121
-			if (empty($rules)) $tss = file_get_contents($tss);
122
-			else return $rules;
140
+			if (empty($rules)) {
141
+				$tss = file_get_contents($tss);
142
+			} else {
143
+				return $rules;
144
+			}
123 145
     	}
124 146
 		return $tss == null ? [] : (new Parser\Sheet($tss, $cssToXpath, $valueParser, $this->filePath, $this))->parse($indexStart);
125 147
 	}
@@ -136,10 +158,15 @@  discard block
 block discarded – undo
136 158
 
137 159
 	private function sortRules($a, $b) {
138 160
 		//If they have the same depth, compare on index
139
-		if ($a->query === $b->query) return $this->sortPseudo($a, $b);
161
+		if ($a->query === $b->query) {
162
+			return $this->sortPseudo($a, $b);
163
+		}
140 164
 
141
-		if ($a->depth === $b->depth) $property = 'index';
142
-		else $property = 'depth';
165
+		if ($a->depth === $b->depth) {
166
+			$property = 'index';
167
+		} else {
168
+			$property = 'depth';
169
+		}
143 170
 
144 171
 		return ($a->$property < $b->$property) ? -1 : 1;
145 172
 	}
Please login to merge, or discard this patch.