@@ -21,15 +21,13 @@ |
||
21 | 21 | $tokens = $args[0]; |
22 | 22 | $parser = new \Transphporm\Parser\Value($this); |
23 | 23 | $args[0] = $parser->parseTokens($tokens, $this->elementData->getData($this->element)); |
24 | - } |
|
25 | - else if ($args[0] instanceof Parser\Tokens) { |
|
24 | + } else if ($args[0] instanceof Parser\Tokens) { |
|
26 | 25 | $args[0] = iterator_to_array($args[0]); |
27 | 26 | } |
28 | 27 | if (isset($this->functions[$name])) { |
29 | 28 | return $this->functions[$name]->run($args[0], $this->element); |
30 | 29 | } |
31 | - } |
|
32 | - catch (\Exception $e) { |
|
30 | + } catch (\Exception $e) { |
|
33 | 31 | throw new RunException(Exception::TSS_FUNCTION, $name, $e); |
34 | 32 | } |
35 | 33 | return true; |
@@ -70,8 +70,11 @@ discard block |
||
70 | 70 | $i += $this->doStrings($tokens, $char, $i); |
71 | 71 | $i += $this->doBrackets($tokens, $char, $i); |
72 | 72 | } |
73 | - if ($returnObj) return new Tokens($tokens); |
|
74 | - else return $tokens; |
|
73 | + if ($returnObj) { |
|
74 | + return new Tokens($tokens); |
|
75 | + } else { |
|
76 | + return $tokens; |
|
77 | + } |
|
75 | 78 | } |
76 | 79 | |
77 | 80 | private function doSimpleTokens(&$tokens, $char) { |
@@ -94,10 +97,15 @@ discard block |
||
94 | 97 | } |
95 | 98 | |
96 | 99 | private function processLiterals(&$tokens, $name) { |
97 | - if (is_numeric($name)) $tokens[] = ['type' => self::NUMERIC, 'value' => $name]; |
|
98 | - else if ($name == 'true') $tokens[] = ['type' => self::BOOL, 'value' => true]; |
|
99 | - else if ($name == 'false') $tokens[] = ['type' => self::BOOL, 'value' => false]; |
|
100 | - else $tokens[] = ['type' => self::NAME, 'value' => $name]; |
|
100 | + if (is_numeric($name)) { |
|
101 | + $tokens[] = ['type' => self::NUMERIC, 'value' => $name]; |
|
102 | + } else if ($name == 'true') { |
|
103 | + $tokens[] = ['type' => self::BOOL, 'value' => true]; |
|
104 | + } else if ($name == 'false') { |
|
105 | + $tokens[] = ['type' => self::BOOL, 'value' => false]; |
|
106 | + } else { |
|
107 | + $tokens[] = ['type' => self::NAME, 'value' => $name]; |
|
108 | + } |
|
101 | 109 | } |
102 | 110 | |
103 | 111 | private function doBrackets(&$tokens, $char, $i) { |
@@ -131,7 +139,9 @@ discard block |
||
131 | 139 | private function extractString($pos) { |
132 | 140 | $char = $this->str[$pos]; |
133 | 141 | $end = strpos($this->str, $char, $pos+1); |
134 | - while ($end !== false && $this->str[$end-1] == '\\') $end = strpos($this->str, $char, $end+1); |
|
142 | + while ($end !== false && $this->str[$end-1] == '\\') { |
|
143 | + $end = strpos($this->str, $char, $end+1); |
|
144 | + } |
|
135 | 145 | |
136 | 146 | return substr($this->str, $pos+1, $end-$pos-1); |
137 | 147 | } |
@@ -140,19 +150,27 @@ discard block |
||
140 | 150 | $close = strpos($this->str, $closeBracket, $open); |
141 | 151 | |
142 | 152 | $cPos = $open+1; |
143 | - while (($cPos = strpos($this->str, $startBracket, $cPos+1)) !== false && $cPos < $close) $close = strpos($this->str, $closeBracket, $close+1); |
|
153 | + while (($cPos = strpos($this->str, $startBracket, $cPos+1)) !== false && $cPos < $close) { |
|
154 | + $close = strpos($this->str, $closeBracket, $close+1); |
|
155 | + } |
|
144 | 156 | return substr($this->str, $open+1, $close-$open-1); |
145 | 157 | } |
146 | 158 | |
147 | 159 | private function identifyChar($chr) { |
148 | - if (isset($this->chars[$chr])) return $this->chars[$chr]; |
|
149 | - else return self::NAME; |
|
160 | + if (isset($this->chars[$chr])) { |
|
161 | + return $this->chars[$chr]; |
|
162 | + } else { |
|
163 | + return self::NAME; |
|
164 | + } |
|
150 | 165 | } |
151 | 166 | |
152 | 167 | private function getChar($num) { |
153 | 168 | $chars = array_reverse($this->chars); |
154 | - if (isset($chars[$num])) return $chars[$num]; |
|
155 | - else return false; |
|
169 | + if (isset($chars[$num])) { |
|
170 | + return $chars[$num]; |
|
171 | + } else { |
|
172 | + return false; |
|
173 | + } |
|
156 | 174 | } |
157 | 175 | |
158 | 176 | public function serialize($tokens) { |
@@ -170,8 +188,11 @@ discard block |
||
170 | 188 | |
171 | 189 | private function serializeValue($token) { |
172 | 190 | if (isset($token['value'])) { |
173 | - if ($token['value'] instanceof Tokens) return $this->serialize($token['value']); |
|
174 | - else return $token['value']; |
|
191 | + if ($token['value'] instanceof Tokens) { |
|
192 | + return $this->serialize($token['value']); |
|
193 | + } else { |
|
194 | + return $token['value']; |
|
195 | + } |
|
175 | 196 | } |
176 | 197 | } |
177 | 198 | } |
@@ -31,14 +31,15 @@ discard block |
||
31 | 31 | $this->tss->skip($processing['skip']+1); |
32 | 32 | $rules = array_merge($rules, $processing['rules']); |
33 | 33 | continue; |
34 | - } |
|
35 | - else if ($token['type'] === Tokenizer::NEW_LINE) { |
|
34 | + } else if ($token['type'] === Tokenizer::NEW_LINE) { |
|
36 | 35 | $line++; |
37 | 36 | continue; |
38 | 37 | } |
39 | 38 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
40 | 39 | $this->tss->skip(count($selector)); |
41 | - if (count($selector) === 0) break; |
|
40 | + if (count($selector) === 0) { |
|
41 | + break; |
|
42 | + } |
|
42 | 43 | |
43 | 44 | $newRules = $this->cssToRules($selector, count($rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $line); |
44 | 45 | $rules = $this->writeRule($rules, $newRules); |
@@ -49,7 +50,9 @@ discard block |
||
49 | 50 | } |
50 | 51 | |
51 | 52 | private function checkError($rules) { |
52 | - if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
|
53 | + if (empty($rules) && count($this->tss) > 0) { |
|
54 | + throw new \Exception('No TSS rules parsed'); |
|
55 | + } |
|
53 | 56 | } |
54 | 57 | |
55 | 58 | private function CssToRules($selector, $index, $properties, $line) { |
@@ -76,7 +79,9 @@ discard block |
||
76 | 79 | } |
77 | 80 | |
78 | 81 | private function processingInstructions($token, $indexStart) { |
79 | - if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
|
82 | + if ($token['type'] !== Tokenizer::AT_SIGN) { |
|
83 | + return false; |
|
84 | + } |
|
80 | 85 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
81 | 86 | $funcName = $tokens->from(Tokenizer::NAME, true)->read(); |
82 | 87 | $args = $this->valueParser->parseTokens($tokens->from(Tokenizer::NAME)); |
@@ -86,15 +91,20 @@ discard block |
||
86 | 91 | } |
87 | 92 | |
88 | 93 | private function import($args, $indexStart) { |
89 | - if ($this->file !== null) $fileName = dirname(realpath($this->file)) . DIRECTORY_SEPARATOR . $args[0]; |
|
90 | - else $fileName = $args[0]; |
|
94 | + if ($this->file !== null) { |
|
95 | + $fileName = dirname(realpath($this->file)) . DIRECTORY_SEPARATOR . $args[0]; |
|
96 | + } else { |
|
97 | + $fileName = $args[0]; |
|
98 | + } |
|
91 | 99 | $sheet = new Sheet(file_get_contents($fileName), $fileName, $this->xPath, $this->valueParser); |
92 | 100 | return $sheet->parse($indexStart); |
93 | 101 | } |
94 | 102 | |
95 | 103 | private function sortRules($a, $b) { |
96 | 104 | //If they have the same depth, compare on index |
97 | - if ($a->depth === $b->depth) return $a->index < $b->index ? -1 : 1; |
|
105 | + if ($a->depth === $b->depth) { |
|
106 | + return $a->index < $b->index ? -1 : 1; |
|
107 | + } |
|
98 | 108 | |
99 | 109 | return ($a->depth < $b->depth) ? -1 : 1; |
100 | 110 | } |
@@ -103,7 +113,9 @@ discard block |
||
103 | 113 | $pos = 0; |
104 | 114 | while (($pos = strpos($str, $open, $pos)) !== false) { |
105 | 115 | $end = strpos($str, $close, $pos); |
106 | - if ($end === false) break; |
|
116 | + if ($end === false) { |
|
117 | + break; |
|
118 | + } |
|
107 | 119 | $str = substr_replace($str, '', $pos, $end-$pos+strlen($close)); |
108 | 120 | } |
109 | 121 |
@@ -28,8 +28,7 @@ discard block |
||
28 | 28 | try { |
29 | 29 | $parts = $this->getFuncParts($tokens); |
30 | 30 | $matches = $matches && $function->match($parts['name'], $parts['args'], $element); |
31 | - } |
|
32 | - catch (\Exception $e) { |
|
31 | + } catch (\Exception $e) { |
|
33 | 32 | throw new \Transphporm\RunException(\Transphporm\Exception::PSEUDO, $parts['name'], $e); |
34 | 33 | } |
35 | 34 | } |
@@ -42,31 +41,37 @@ discard block |
||
42 | 41 | $parts['name'] = $this->getFuncName($tokens); |
43 | 42 | if ($parts['name'] === null || in_array($parts['name'], ['data', 'iteration', 'root'])) { |
44 | 43 | $parts['args'] = $this->valueParser->parseTokens($tokens); |
45 | - } |
|
46 | - else if (count($tokens) > 1) { |
|
44 | + } else if (count($tokens) > 1) { |
|
47 | 45 | $tokens->rewind(); |
48 | 46 | $tokens->next(); |
49 | 47 | $parts['args'] = $this->valueParser->parseTokens($tokens->current()['value']); |
48 | + } else { |
|
49 | + $parts['args'] = [['']]; |
|
50 | 50 | } |
51 | - else $parts['args'] = [['']]; |
|
52 | 51 | return $parts; |
53 | 52 | } |
54 | 53 | |
55 | 54 | private function getFuncName($tokens) { |
56 | - if ($tokens->type() === Tokenizer::NAME) return $tokens->read(); |
|
55 | + if ($tokens->type() === Tokenizer::NAME) { |
|
56 | + return $tokens->read(); |
|
57 | + } |
|
57 | 58 | return null; |
58 | 59 | } |
59 | 60 | |
60 | 61 | public function hasFunction($name) { |
61 | 62 | foreach ($this->pseudo as $tokens) { |
62 | - if ($name === $this->getFuncName($tokens)) return true; |
|
63 | + if ($name === $this->getFuncName($tokens)) { |
|
64 | + return true; |
|
65 | + } |
|
63 | 66 | } |
64 | 67 | } |
65 | 68 | |
66 | 69 | public function getFuncArgs($name) { |
67 | 70 | foreach ($this->pseudo as $tokens) { |
68 | 71 | $parts = $this->getFuncParts($tokens); |
69 | - if ($name === $parts['name']) return $parts['args']; |
|
72 | + if ($name === $parts['name']) { |
|
73 | + return $parts['args']; |
|
74 | + } |
|
70 | 75 | } |
71 | 76 | } |
72 | 77 | } |
@@ -14,7 +14,9 @@ discard block |
||
14 | 14 | } |
15 | 15 | |
16 | 16 | public function format($value, $rules) { |
17 | - if (!isset($rules['format'])) return $value; |
|
17 | + if (!isset($rules['format'])) { |
|
18 | + return $value; |
|
19 | + } |
|
18 | 20 | $tokens = $rules['format']; |
19 | 21 | |
20 | 22 | $functionName = $tokens->from(\Transphporm\Parser\Tokenizer::NAME, true)->read(); |
@@ -27,8 +29,7 @@ discard block |
||
27 | 29 | |
28 | 30 | try { |
29 | 31 | return $this->processFormat($options, $functionName, $value); |
30 | - } |
|
31 | - catch (\Exception $e) { |
|
32 | + } catch (\Exception $e) { |
|
32 | 33 | throw new \Transphporm\RunException(\Transphporm\Exception::FORMATTER, $functionName, $e); |
33 | 34 | } |
34 | 35 | } |
@@ -43,7 +44,9 @@ discard block |
||
43 | 44 | } |
44 | 45 | } |
45 | 46 | } |
46 | - if (!$functionExists) throw new \Exception("Formatter '$functionName' does not exist"); |
|
47 | + if (!$functionExists) { |
|
48 | + throw new \Exception("Formatter '$functionName' does not exist"); |
|
49 | + } |
|
47 | 50 | return $value; |
48 | 51 | } |
49 | 52 | } |
@@ -30,11 +30,15 @@ discard block |
||
30 | 30 | |
31 | 31 | public function run(\DomElement $element) { |
32 | 32 | $this->functionSet->setElement($element); |
33 | - if ($this->file !== null) $this->baseDir = dirname(realpath($this->file)) . DIRECTORY_SEPARATOR; |
|
33 | + if ($this->file !== null) { |
|
34 | + $this->baseDir = dirname(realpath($this->file)) . DIRECTORY_SEPARATOR; |
|
35 | + } |
|
34 | 36 | $this->configLine = $this->line; |
35 | 37 | try { |
36 | 38 | //Don't run if there's a pseudo element like nth-child() and this element doesn't match it |
37 | - if (!$this->pseudoMatcher->matches($element)) return; |
|
39 | + if (!$this->pseudoMatcher->matches($element)) { |
|
40 | + return; |
|
41 | + } |
|
38 | 42 | |
39 | 43 | // TODO: Have all rule values parsed before running them so that things like `content-append` are not expecting tokens |
40 | 44 | // problem with this is that anything in data changed by run properties is not shown |
@@ -42,10 +46,11 @@ discard block |
||
42 | 46 | |
43 | 47 | foreach ($this->rules as $name => $value) { |
44 | 48 | $result = $this->callProperty($name, $element, $this->getArgs($value)); |
45 | - if ($result === false) break; |
|
49 | + if ($result === false) { |
|
50 | + break; |
|
51 | + } |
|
46 | 52 | } |
47 | - } |
|
48 | - catch (\Transphporm\RunException $e) { |
|
53 | + } catch (\Transphporm\RunException $e) { |
|
49 | 54 | throw new \Transphporm\Exception($e, $this->file, $this->line); |
50 | 55 | } |
51 | 56 | } |
@@ -62,9 +67,10 @@ discard block |
||
62 | 67 | if (isset($this->properties[$name])) { |
63 | 68 | try { |
64 | 69 | return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties); |
65 | - } |
|
66 | - catch (\Exception $e) { |
|
67 | - if ($e instanceof \Transphporm\RunException) throw $e; |
|
70 | + } catch (\Exception $e) { |
|
71 | + if ($e instanceof \Transphporm\RunException) { |
|
72 | + throw $e; |
|
73 | + } |
|
68 | 74 | throw new \Transphporm\RunException(\Transphporm\Exception::PROPERTY, $name, $e); |
69 | 75 | } |
70 | 76 | } |
@@ -11,14 +11,20 @@ |
||
11 | 11 | |
12 | 12 | public function match($name, $args, \DomElement $element) { |
13 | 13 | |
14 | - if ($name !== 'nth-child') return true; |
|
14 | + if ($name !== 'nth-child') { |
|
15 | + return true; |
|
16 | + } |
|
15 | 17 | |
16 | 18 | $this->count++; |
17 | 19 | $criteria = $args[0]; |
18 | 20 | |
19 | - if (is_callable([$this, $criteria])) return $this->$criteria($this->count); |
|
20 | - else if (!is_numeric($criteria)) throw new \Exception("Argument passed to 'nth-child' must be 'odd', 'even', or of type int"); |
|
21 | - else return $this->count == $criteria; |
|
21 | + if (is_callable([$this, $criteria])) { |
|
22 | + return $this->$criteria($this->count); |
|
23 | + } else if (!is_numeric($criteria)) { |
|
24 | + throw new \Exception("Argument passed to 'nth-child' must be 'odd', 'even', or of type int"); |
|
25 | + } else { |
|
26 | + return $this->count == $criteria; |
|
27 | + } |
|
22 | 28 | } |
23 | 29 | |
24 | 30 | private function odd($num) { |
@@ -26,7 +26,9 @@ discard block |
||
26 | 26 | $this->cache = new Cache(new \ArrayObject()); |
27 | 27 | |
28 | 28 | $modules = is_array($modules) ? $modules : $this->defaultModules; |
29 | - foreach ($modules as $module) $this->loadModule(new $module); |
|
29 | + foreach ($modules as $module) { |
|
30 | + $this->loadModule(new $module); |
|
31 | + } |
|
30 | 32 | } |
31 | 33 | |
32 | 34 | //Allow setting the time used by Transphporm for caching. This is for testing purposes |
@@ -51,7 +53,9 @@ discard block |
||
51 | 53 | $valueParser = new Parser\Value($data); |
52 | 54 | $config = new Config($data, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($data, $template->getPrefix()), $headers, $this->baseDir); |
53 | 55 | |
54 | - foreach ($this->modules as $module) $module->load($config); |
|
56 | + foreach ($this->modules as $module) { |
|
57 | + $module->load($config); |
|
58 | + } |
|
55 | 59 | |
56 | 60 | $this->processRules($template, $config); |
57 | 61 | |
@@ -66,7 +70,9 @@ discard block |
||
66 | 70 | $rules = $this->getRules($template, $config); |
67 | 71 | |
68 | 72 | foreach ($rules as $rule) { |
69 | - if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config); |
|
73 | + if ($rule->shouldRun($this->time)) { |
|
74 | + $this->executeTssRule($rule, $template, $config); |
|
75 | + } |
|
70 | 76 | } |
71 | 77 | } |
72 | 78 | |
@@ -91,8 +97,9 @@ discard block |
||
91 | 97 | if (trim($this->template)[0] !== '<') { |
92 | 98 | $xml = $this->cache->load($this->template, filemtime($this->template)); |
93 | 99 | return $xml ? $xml : ['body' => file_get_contents($this->template), 'headers' => []]; |
100 | + } else { |
|
101 | + return ['body' => $this->template, 'headers' => []]; |
|
94 | 102 | } |
95 | - else return ['body' => $this->template, 'headers' => []]; |
|
96 | 103 | } |
97 | 104 | |
98 | 105 | //Load the TSS rules either from a file or as a string |
@@ -107,10 +114,14 @@ discard block |
||
107 | 114 | //Try to load the cached rules, if not set in the cache (or expired) parse the supplied sheet |
108 | 115 | $rules = $this->cache->load($key, filemtime($this->tss)); |
109 | 116 | |
110 | - if (!$rules) return $this->cache->write($key, (new Parser\Sheet(file_get_contents($this->tss), $this->tss, $config->getCssToXpath(), $config->getValueParser()))->parse()); |
|
111 | - else return $rules; |
|
117 | + if (!$rules) { |
|
118 | + return $this->cache->write($key, (new Parser\Sheet(file_get_contents($this->tss), $this->tss, $config->getCssToXpath(), $config->getValueParser()))->parse()); |
|
119 | + } else { |
|
120 | + return $rules; |
|
121 | + } |
|
122 | + } else { |
|
123 | + return (new Parser\Sheet($this->tss, $this->baseDir, $config->getCssToXpath(), $config->getValueParser()))->parse(); |
|
112 | 124 | } |
113 | - else return (new Parser\Sheet($this->tss, $this->baseDir, $config->getCssToXpath(), $config->getValueParser()))->parse(); |
|
114 | 125 | } |
115 | 126 | |
116 | 127 | public function setCache(\ArrayAccess $cache) { |