@@ -98,6 +98,9 @@ |
||
98 | 98 | } |
99 | 99 | } |
100 | 100 | |
101 | + /** |
|
102 | + * @param integer $n |
|
103 | + */ |
|
101 | 104 | private function isLiteral($n) { |
102 | 105 | //Is it a normal literal character |
103 | 106 | return isset($this->str[$n]) && ($this->identifyChar($this->str[$n]) == self::NAME |
@@ -73,9 +73,9 @@ discard block |
||
73 | 73 | for ($i = 0; $i < strlen($this->str); $i++) { |
74 | 74 | $char = $this->identifyChar($this->str[$i]); |
75 | 75 | if ($commentChangeI = $this->doComments($tokens, $char, $i)) { |
76 | - $i += $commentChangeI; |
|
77 | - continue; |
|
78 | - } |
|
76 | + $i += $commentChangeI; |
|
77 | + continue; |
|
78 | + } |
|
79 | 79 | |
80 | 80 | $this->doNewLine($tokens, $char); |
81 | 81 | $this->doSimpleTokens($tokens, $char); |
@@ -88,10 +88,10 @@ discard block |
||
88 | 88 | else return $tokens; |
89 | 89 | } |
90 | 90 | |
91 | - private function doComments(&$tokens, $char, $i) { |
|
92 | - return $this->doSingleLineComments($tokens, $char, $i) + |
|
93 | - $this->doMultiLineComments($tokens, $char, $i); |
|
94 | - } |
|
91 | + private function doComments(&$tokens, $char, $i) { |
|
92 | + return $this->doSingleLineComments($tokens, $char, $i) + |
|
93 | + $this->doMultiLineComments($tokens, $char, $i); |
|
94 | + } |
|
95 | 95 | |
96 | 96 | private function doSingleLineComments(&$tokens, $char, $i) { |
97 | 97 | if ($char == Tokenizer::DIVIDE && isset($this->str[$i+1]) && $this->identifyChar($this->str[$i+1]) == Tokenizer::DIVIDE) { |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | } |
90 | 90 | |
91 | 91 | private function doComments(&$tokens, $char, $i) { |
92 | - return $this->doSingleLineComments($tokens, $char, $i) + |
|
92 | + return $this->doSingleLineComments($tokens, $char, $i)+ |
|
93 | 93 | $this->doMultiLineComments($tokens, $char, $i); |
94 | 94 | } |
95 | 95 | |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | if ($char === self::STRING) { |
170 | 170 | $string = $this->extractString($i); |
171 | 171 | $length = strlen($string)+1; |
172 | - $string = str_replace('\\' . $this->str[$i], $this->str[$i], $string); |
|
172 | + $string = str_replace('\\'.$this->str[$i], $this->str[$i], $string); |
|
173 | 173 | $tokens[] = ['type' => self::STRING, 'value' => $string, 'line' => $this->lineNo]; |
174 | 174 | return $length; |
175 | 175 | } |
@@ -84,8 +84,11 @@ discard block |
||
84 | 84 | $i += $this->doBrackets($tokens, $char, $i); |
85 | 85 | |
86 | 86 | } |
87 | - if ($returnObj) return new Tokens($tokens); |
|
88 | - else return $tokens; |
|
87 | + if ($returnObj) { |
|
88 | + return new Tokens($tokens); |
|
89 | + } else { |
|
90 | + return $tokens; |
|
91 | + } |
|
89 | 92 | } |
90 | 93 | |
91 | 94 | private function doComments(&$tokens, $char, $i) { |
@@ -103,7 +106,9 @@ discard block |
||
103 | 106 | private function doMultiLineComments(&$tokens, $char, $i) { |
104 | 107 | if ($char == Tokenizer::DIVIDE && isset($this->str[$i+1]) && $this->identifyChar($this->str[$i+1]) == Tokenizer::MULTIPLY) { |
105 | 108 | $pos = strpos($this->str, '*/', $i)+2; |
106 | - if ($this->str[$i+$pos] == "\n") $pos++; |
|
109 | + if ($this->str[$i+$pos] == "\n") { |
|
110 | + $pos++; |
|
111 | + } |
|
107 | 112 | return $pos ? $pos : 0; |
108 | 113 | } |
109 | 114 | } |
@@ -142,10 +147,15 @@ discard block |
||
142 | 147 | } |
143 | 148 | |
144 | 149 | private function processLiterals(&$tokens, $name) { |
145 | - if (is_numeric($name)) $tokens[] = ['type' => self::NUMERIC, 'value' => $name]; |
|
146 | - else if ($name == 'true') $tokens[] = ['type' => self::BOOL, 'value' => true]; |
|
147 | - else if ($name == 'false') $tokens[] = ['type' => self::BOOL, 'value' => false]; |
|
148 | - else $tokens[] = ['type' => self::NAME, 'value' => $name, 'line' => $this->lineNo]; |
|
150 | + if (is_numeric($name)) { |
|
151 | + $tokens[] = ['type' => self::NUMERIC, 'value' => $name]; |
|
152 | + } else if ($name == 'true') { |
|
153 | + $tokens[] = ['type' => self::BOOL, 'value' => true]; |
|
154 | + } else if ($name == 'false') { |
|
155 | + $tokens[] = ['type' => self::BOOL, 'value' => false]; |
|
156 | + } else { |
|
157 | + $tokens[] = ['type' => self::NAME, 'value' => $name, 'line' => $this->lineNo]; |
|
158 | + } |
|
149 | 159 | } |
150 | 160 | |
151 | 161 | private function doBrackets(&$tokens, $char, $i) { |
@@ -178,7 +188,9 @@ discard block |
||
178 | 188 | private function extractString($pos) { |
179 | 189 | $char = $this->str[$pos]; |
180 | 190 | $end = strpos($this->str, $char, $pos+1); |
181 | - while ($end !== false && $this->str[$end-1] == '\\') $end = strpos($this->str, $char, $end+1); |
|
191 | + while ($end !== false && $this->str[$end-1] == '\\') { |
|
192 | + $end = strpos($this->str, $char, $end+1); |
|
193 | + } |
|
182 | 194 | |
183 | 195 | return substr($this->str, $pos+1, $end-$pos-1); |
184 | 196 | } |
@@ -187,18 +199,26 @@ discard block |
||
187 | 199 | $close = strpos($this->str, $closeBracket, $open); |
188 | 200 | |
189 | 201 | $cPos = $open+1; |
190 | - while (($cPos = strpos($this->str, $startBracket, $cPos+1)) !== false && $cPos < $close) $close = strpos($this->str, $closeBracket, $close+1); |
|
202 | + while (($cPos = strpos($this->str, $startBracket, $cPos+1)) !== false && $cPos < $close) { |
|
203 | + $close = strpos($this->str, $closeBracket, $close+1); |
|
204 | + } |
|
191 | 205 | return substr($this->str, $open+1, $close-$open-1); |
192 | 206 | } |
193 | 207 | |
194 | 208 | private function identifyChar($chr) { |
195 | - if (isset($this->chars[$chr])) return $this->chars[$chr]; |
|
196 | - else return self::NAME; |
|
209 | + if (isset($this->chars[$chr])) { |
|
210 | + return $this->chars[$chr]; |
|
211 | + } else { |
|
212 | + return self::NAME; |
|
213 | + } |
|
197 | 214 | } |
198 | 215 | |
199 | 216 | private function getChar($num) { |
200 | 217 | $chars = array_reverse($this->chars); |
201 | - if (isset($chars[$num])) return $chars[$num]; |
|
202 | - else return false; |
|
218 | + if (isset($chars[$num])) { |
|
219 | + return $chars[$num]; |
|
220 | + } else { |
|
221 | + return false; |
|
222 | + } |
|
203 | 223 | } |
204 | 224 | } |
@@ -66,7 +66,9 @@ discard block |
||
66 | 66 | /** Loops through all assigned hooks, runs the Xpath query and calls the hook */ |
67 | 67 | private function processHooks() { |
68 | 68 | foreach ($this->hooks as list($query, $hook)) { |
69 | - foreach ($this->xpath->query($query) as $element) $hook->run($element); |
|
69 | + foreach ($this->xpath->query($query) as $element) { |
|
70 | + $hook->run($element); |
|
71 | + } |
|
70 | 72 | } |
71 | 73 | $this->hooks = []; |
72 | 74 | } |
@@ -74,7 +76,9 @@ discard block |
||
74 | 76 | /** Prints out the current DomDocument as HTML */ |
75 | 77 | private function printDocument() { |
76 | 78 | $output = ''; |
77 | - foreach ($this->document->documentElement->childNodes as $node) $output .= call_user_func($this->save, $node); |
|
79 | + foreach ($this->document->documentElement->childNodes as $node) { |
|
80 | + $output .= call_user_func($this->save, $node); |
|
81 | + } |
|
78 | 82 | return $output; |
79 | 83 | } |
80 | 84 | |
@@ -86,13 +90,18 @@ discard block |
||
86 | 90 | //Generate the document by taking only the childnodes of the template, ignoring the <template> and </template> tags |
87 | 91 | //TODO: Is there a faster way of doing this without string manipulation on the output or this loop through childnodes? |
88 | 92 | //Either return a whole DomDocument or return the output HTML |
89 | - if ($document) return $this->document; |
|
93 | + if ($document) { |
|
94 | + return $this->document; |
|
95 | + } |
|
90 | 96 | |
91 | 97 | //Print the doctype... saveHtml inexplicably does not support $doc->doctype as an argument |
92 | 98 | $output = ($this->document->doctype) ? $this->document->saveXml($this->document->doctype) . "\n" : ''; |
93 | 99 | |
94 | - if ($this->document->documentElement->tagName !== 'template') $output .= call_user_func($this->save, $this->document->documentElement); |
|
95 | - else $output = $this->printDocument(); |
|
100 | + if ($this->document->documentElement->tagName !== 'template') { |
|
101 | + $output .= call_user_func($this->save, $this->document->documentElement); |
|
102 | + } else { |
|
103 | + $output = $this->printDocument(); |
|
104 | + } |
|
96 | 105 | |
97 | 106 | //repair empty tags. Browsers break on <script /> and <div /> so can't avoid LIBXML_NOEMPTYTAG but they also break on <base></base> so repair them |
98 | 107 | $output = str_replace(['></img>', '></br>', '></meta>', '></base>', '></link>', '></hr>', '></input>'], ' />', $output); |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | $this->save = function($content = null) { |
38 | 38 | return $this->document->saveHtml($content); |
39 | 39 | }; |
40 | - $this->document->loadHtml('<' . '?xml encoding="UTF-8">' . $doc, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); |
|
40 | + $this->document->loadHtml('<'.'?xml encoding="UTF-8">'.$doc, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); |
|
41 | 41 | |
42 | 42 | if (strpos($doc, '<!') !== 0) { |
43 | 43 | $templateNode = $this->document->getElementsByTagName('template')[0]; |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | if ($document) return $this->document; |
90 | 90 | |
91 | 91 | //Print the doctype... saveHtml inexplicably does not support $doc->doctype as an argument |
92 | - $output = ($this->document->doctype) ? $this->document->saveXml($this->document->doctype) . "\n" : ''; |
|
92 | + $output = ($this->document->doctype) ? $this->document->saveXml($this->document->doctype)."\n" : ''; |
|
93 | 93 | |
94 | 94 | if ($this->document->documentElement->tagName !== 'template') $output .= call_user_func($this->save, $this->document->documentElement); |
95 | 95 | else $output = $this->printDocument(); |
@@ -2,48 +2,48 @@ |
||
2 | 2 | namespace Transphporm; |
3 | 3 | use Transphporm\Parser\Tokenizer; |
4 | 4 | class TSSValidator { |
5 | - private $error; |
|
5 | + private $error; |
|
6 | 6 | |
7 | - public function validate($tss) { |
|
8 | - $this->error = null; |
|
9 | - $tokens = $this->tokenize($tss); |
|
7 | + public function validate($tss) { |
|
8 | + $this->error = null; |
|
9 | + $tokens = $this->tokenize($tss); |
|
10 | 10 | |
11 | - foreach ($tokens as $token) |
|
12 | - if (!$this->validateRule($token)) return false; |
|
11 | + foreach ($tokens as $token) |
|
12 | + if (!$this->validateRule($token)) return false; |
|
13 | 13 | |
14 | - return true; |
|
15 | - } |
|
14 | + return true; |
|
15 | + } |
|
16 | 16 | |
17 | - public function getLastError() { |
|
18 | - return $this->error; |
|
19 | - } |
|
17 | + public function getLastError() { |
|
18 | + return $this->error; |
|
19 | + } |
|
20 | 20 | |
21 | - private function validateRule($token) { |
|
22 | - if ($token['type'] !== Tokenizer::OPEN_BRACE) return true; |
|
21 | + private function validateRule($token) { |
|
22 | + if ($token['type'] !== Tokenizer::OPEN_BRACE) return true; |
|
23 | 23 | |
24 | - return $this->checkBraces($token) && $this->checkSemicolons($token) |
|
25 | - && $this->checkParenthesis($token); |
|
26 | - } |
|
24 | + return $this->checkBraces($token) && $this->checkSemicolons($token) |
|
25 | + && $this->checkParenthesis($token); |
|
26 | + } |
|
27 | 27 | |
28 | - private function checkBraces($token) { |
|
29 | - return strpos($token['string'], '{') === false; |
|
30 | - } |
|
28 | + private function checkBraces($token) { |
|
29 | + return strpos($token['string'], '{') === false; |
|
30 | + } |
|
31 | 31 | |
32 | - private function checkSemicolons($braceToken) { |
|
33 | - $splitTokens = $braceToken['value']->splitOnToken(Tokenizer::COLON); |
|
34 | - array_shift($splitTokens); array_pop($splitTokens); |
|
35 | - foreach ($splitTokens as $tokens) |
|
36 | - if (!in_array(Tokenizer::SEMI_COLON, array_column(iterator_to_array($tokens), 'type'))) return false; |
|
32 | + private function checkSemicolons($braceToken) { |
|
33 | + $splitTokens = $braceToken['value']->splitOnToken(Tokenizer::COLON); |
|
34 | + array_shift($splitTokens); array_pop($splitTokens); |
|
35 | + foreach ($splitTokens as $tokens) |
|
36 | + if (!in_array(Tokenizer::SEMI_COLON, array_column(iterator_to_array($tokens), 'type'))) return false; |
|
37 | 37 | |
38 | - return true; |
|
39 | - } |
|
38 | + return true; |
|
39 | + } |
|
40 | 40 | |
41 | - private function checkParenthesis($token) { |
|
42 | - return substr_count($token['string'], '(') === substr_count($token['string'], ')'); |
|
43 | - } |
|
41 | + private function checkParenthesis($token) { |
|
42 | + return substr_count($token['string'], '(') === substr_count($token['string'], ')'); |
|
43 | + } |
|
44 | 44 | |
45 | - private function tokenize($tss) { |
|
46 | - if (is_file($tss)) $tss = file_get_contents($tss); |
|
47 | - return (new Parser\Tokenizer($tss))->getTokens(); |
|
48 | - } |
|
45 | + private function tokenize($tss) { |
|
46 | + if (is_file($tss)) $tss = file_get_contents($tss); |
|
47 | + return (new Parser\Tokenizer($tss))->getTokens(); |
|
48 | + } |
|
49 | 49 | } |
@@ -8,8 +8,9 @@ discard block |
||
8 | 8 | $this->error = null; |
9 | 9 | $tokens = $this->tokenize($tss); |
10 | 10 | |
11 | - foreach ($tokens as $token) |
|
12 | - if (!$this->validateRule($token)) return false; |
|
11 | + foreach ($tokens as $token) { |
|
12 | + if (!$this->validateRule($token)) return false; |
|
13 | + } |
|
13 | 14 | |
14 | 15 | return true; |
15 | 16 | } |
@@ -19,7 +20,9 @@ discard block |
||
19 | 20 | } |
20 | 21 | |
21 | 22 | private function validateRule($token) { |
22 | - if ($token['type'] !== Tokenizer::OPEN_BRACE) return true; |
|
23 | + if ($token['type'] !== Tokenizer::OPEN_BRACE) { |
|
24 | + return true; |
|
25 | + } |
|
23 | 26 | |
24 | 27 | return $this->checkBraces($token) && $this->checkSemicolons($token) |
25 | 28 | && $this->checkParenthesis($token); |
@@ -32,8 +35,9 @@ discard block |
||
32 | 35 | private function checkSemicolons($braceToken) { |
33 | 36 | $splitTokens = $braceToken['value']->splitOnToken(Tokenizer::COLON); |
34 | 37 | array_shift($splitTokens); array_pop($splitTokens); |
35 | - foreach ($splitTokens as $tokens) |
|
36 | - if (!in_array(Tokenizer::SEMI_COLON, array_column(iterator_to_array($tokens), 'type'))) return false; |
|
38 | + foreach ($splitTokens as $tokens) { |
|
39 | + if (!in_array(Tokenizer::SEMI_COLON, array_column(iterator_to_array($tokens), 'type'))) return false; |
|
40 | + } |
|
37 | 41 | |
38 | 42 | return true; |
39 | 43 | } |
@@ -43,7 +47,9 @@ discard block |
||
43 | 47 | } |
44 | 48 | |
45 | 49 | private function tokenize($tss) { |
46 | - if (is_file($tss)) $tss = file_get_contents($tss); |
|
50 | + if (is_file($tss)) { |
|
51 | + $tss = file_get_contents($tss); |
|
52 | + } |
|
47 | 53 | return (new Parser\Tokenizer($tss))->getTokens(); |
48 | 54 | } |
49 | 55 | } |