@@ -25,14 +25,19 @@ discard block |
||
| 25 | 25 | $this->file = $tss; |
| 26 | 26 | $this->rules = $this->cache->load($tss); |
| 27 | 27 | $this->filePath->addPath(dirname(realpath($tss))); |
| 28 | - if (empty($this->rules)) $tss = file_get_contents($tss); |
|
| 29 | - else return; |
|
| 28 | + if (empty($this->rules)) { |
|
| 29 | + $tss = file_get_contents($tss); |
|
| 30 | + } else { |
|
| 31 | + return; |
|
| 32 | + } |
|
| 30 | 33 | } |
| 31 | 34 | $this->tss = (new Tokenizer($tss))->getTokens(); |
| 32 | 35 | } |
| 33 | 36 | |
| 34 | 37 | public function parse($indexStart = 0) { |
| 35 | - if (!empty($this->rules)) return $this->rules['rules']; |
|
| 38 | + if (!empty($this->rules)) { |
|
| 39 | + return $this->rules['rules']; |
|
| 40 | + } |
|
| 36 | 41 | $rules = $this->parseTokens($indexStart); |
| 37 | 42 | usort($rules, [$this, 'sortRules']); |
| 38 | 43 | $this->checkError($rules); |
@@ -44,8 +49,9 @@ discard block |
||
| 44 | 49 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
| 45 | 50 | if ($processing = $this->processingInstructions($token, count($this->rules)+$indexStart)) { |
| 46 | 51 | $this->rules = array_merge($this->rules, $processing); |
| 52 | + } else if ($token['type'] !== Tokenizer::NEW_LINE) { |
|
| 53 | + $this->addRules($token, $indexStart); |
|
| 47 | 54 | } |
| 48 | - else if ($token['type'] !== Tokenizer::NEW_LINE) $this->addRules($token, $indexStart); |
|
| 49 | 55 | } |
| 50 | 56 | |
| 51 | 57 | return $this->rules; |
@@ -55,14 +61,18 @@ discard block |
||
| 55 | 61 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
| 56 | 62 | |
| 57 | 63 | $this->tss->skip(count($selector)); |
| 58 | - if (count($selector) === 0) return; |
|
| 64 | + if (count($selector) === 0) { |
|
| 65 | + return; |
|
| 66 | + } |
|
| 59 | 67 | |
| 60 | 68 | $newRules = $this->cssToRules($selector, count($this->rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $token['line']); |
| 61 | 69 | $this->rules = $this->writeRule($this->rules, $newRules); |
| 62 | 70 | } |
| 63 | 71 | |
| 64 | 72 | private function checkError($rules) { |
| 65 | - if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
|
| 73 | + if (empty($rules) && count($this->tss) > 0) { |
|
| 74 | + throw new \Exception('No TSS rules parsed'); |
|
| 75 | + } |
|
| 66 | 76 | } |
| 67 | 77 | |
| 68 | 78 | private function CssToRules($selector, $index, $properties, $line) { |
@@ -89,7 +99,9 @@ discard block |
||
| 89 | 99 | } |
| 90 | 100 | |
| 91 | 101 | private function processingInstructions($token, $indexStart) { |
| 92 | - if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
|
| 102 | + if ($token['type'] !== Tokenizer::AT_SIGN) { |
|
| 103 | + return false; |
|
| 104 | + } |
|
| 93 | 105 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
| 94 | 106 | $funcName = $tokens->from(Tokenizer::NAME, true)->read(); |
| 95 | 107 | $args = $this->valueParser->parseTokens($tokens->from(Tokenizer::NAME)); |
@@ -109,7 +121,9 @@ discard block |
||
| 109 | 121 | |
| 110 | 122 | private function sortRules($a, $b) { |
| 111 | 123 | //If they have the same depth, compare on index |
| 112 | - if ($a->depth === $b->depth) return $a->index < $b->index ? -1 : 1; |
|
| 124 | + if ($a->depth === $b->depth) { |
|
| 125 | + return $a->index < $b->index ? -1 : 1; |
|
| 126 | + } |
|
| 113 | 127 | |
| 114 | 128 | return ($a->depth < $b->depth) ? -1 : 1; |
| 115 | 129 | } |
@@ -70,7 +70,9 @@ discard block |
||
| 70 | 70 | $selector->type = $token['type']; |
| 71 | 71 | $selectors[] = $selector; |
| 72 | 72 | } |
| 73 | - if (isset($token['value'])) $selectors[count($selectors)-1]->string = $token['value']; |
|
| 73 | + if (isset($token['value'])) { |
|
| 74 | + $selectors[count($selectors)-1]->string = $token['value']; |
|
| 75 | + } |
|
| 74 | 76 | } |
| 75 | 77 | return $selectors; |
| 76 | 78 | } |
@@ -80,7 +82,9 @@ discard block |
||
| 80 | 82 | $selectors = $this->split($css); |
| 81 | 83 | $xpath = '/'; |
| 82 | 84 | foreach ($selectors as $selector) { |
| 83 | - if (isset($this->translators[$selector->type])) $xpath .= $this->translators[$selector->type]($selector->string, $xpath); |
|
| 85 | + if (isset($this->translators[$selector->type])) { |
|
| 86 | + $xpath .= $this->translators[$selector->type]($selector->string, $xpath); |
|
| 87 | + } |
|
| 84 | 88 | } |
| 85 | 89 | |
| 86 | 90 | $xpath = str_replace('/[', '/*[', $xpath); |
@@ -92,11 +96,15 @@ discard block |
||
| 92 | 96 | $tokens = new Tokens; |
| 93 | 97 | $split = $css->splitOnToken(Tokenizer::GREATER_THAN); |
| 94 | 98 | |
| 95 | - if (count($split) <= 1) return $css; |
|
| 99 | + if (count($split) <= 1) { |
|
| 100 | + return $css; |
|
| 101 | + } |
|
| 96 | 102 | |
| 97 | 103 | for ($i = 0; $i < count($split); $i++) { |
| 98 | 104 | $tokens->add($split[$i]->trim()); |
| 99 | - if (isset($split[$i+1])) $tokens->add(['type' => Tokenizer::GREATER_THAN]); |
|
| 105 | + if (isset($split[$i+1])) { |
|
| 106 | + $tokens->add(['type' => Tokenizer::GREATER_THAN]); |
|
| 107 | + } |
|
| 100 | 108 | } |
| 101 | 109 | |
| 102 | 110 | return $tokens; |
@@ -39,8 +39,11 @@ discard block |
||
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | public function add($token) { |
| 42 | - if ($token instanceof Tokens) $this->tokens = array_merge($token->tokens); |
|
| 43 | - else $this->tokens[] = $token; |
|
| 42 | + if ($token instanceof Tokens) { |
|
| 43 | + $this->tokens = array_merge($token->tokens); |
|
| 44 | + } else { |
|
| 45 | + $this->tokens[] = $token; |
|
| 46 | + } |
|
| 44 | 47 | } |
| 45 | 48 | |
| 46 | 49 | |
@@ -54,9 +57,13 @@ discard block |
||
| 54 | 57 | |
| 55 | 58 | private function getKeyToSlice($tokenType) { |
| 56 | 59 | $keys = $this->getKeysOfTokenType($tokenType); |
| 57 | - if (empty($keys)) return false; |
|
| 60 | + if (empty($keys)) { |
|
| 61 | + return false; |
|
| 62 | + } |
|
| 58 | 63 | $key = $keys[0]; |
| 59 | - for ($i = 0; $key < $this->iterator && isset($keys[$i]); $i++) $key = $keys[$i]; |
|
| 64 | + for ($i = 0; $key < $this->iterator && isset($keys[$i]); $i++) { |
|
| 65 | + $key = $keys[$i]; |
|
| 66 | + } |
|
| 60 | 67 | return $key; |
| 61 | 68 | } |
| 62 | 69 | |
@@ -70,10 +77,17 @@ discard block |
||
| 70 | 77 | |
| 71 | 78 | private function sliceTokens($tokenType, $type, $increment = false) { |
| 72 | 79 | $key = $this->getKeyToSlice($tokenType); |
| 73 | - if ($key === false) return new Tokens([]); |
|
| 74 | - if ($increment) $key++; |
|
| 75 | - if ($type === "from") return new Tokens(array_slice($this->tokens, $key)); |
|
| 76 | - else return new Tokens(array_slice($this->tokens, $this->iterator, $key)); |
|
| 80 | + if ($key === false) { |
|
| 81 | + return new Tokens([]); |
|
| 82 | + } |
|
| 83 | + if ($increment) { |
|
| 84 | + $key++; |
|
| 85 | + } |
|
| 86 | + if ($type === "from") { |
|
| 87 | + return new Tokens(array_slice($this->tokens, $key)); |
|
| 88 | + } else { |
|
| 89 | + return new Tokens(array_slice($this->tokens, $this->iterator, $key)); |
|
| 90 | + } |
|
| 77 | 91 | } |
| 78 | 92 | |
| 79 | 93 | public function skip($count) { |
@@ -84,8 +98,11 @@ discard block |
||
| 84 | 98 | $splitTokens = []; |
| 85 | 99 | $i = 0; |
| 86 | 100 | foreach ($this->tokens as $token) { |
| 87 | - if ($token['type'] === $tokenType) $i++; |
|
| 88 | - else $splitTokens[$i][] = $token; |
|
| 101 | + if ($token['type'] === $tokenType) { |
|
| 102 | + $i++; |
|
| 103 | + } else { |
|
| 104 | + $splitTokens[$i][] = $token; |
|
| 105 | + } |
|
| 89 | 106 | } |
| 90 | 107 | return array_map(function ($tokens) { |
| 91 | 108 | return new Tokens($tokens); |
@@ -109,7 +126,9 @@ discard block |
||
| 109 | 126 | |
| 110 | 127 | public function removeLine() { |
| 111 | 128 | $tokens = $this->tokens; |
| 112 | - foreach ($tokens as &$token) unset($token['line']); |
|
| 129 | + foreach ($tokens as &$token) { |
|
| 130 | + unset($token['line']); |
|
| 131 | + } |
|
| 113 | 132 | return new Tokens($tokens); |
| 114 | 133 | } |
| 115 | 134 | |
@@ -40,8 +40,11 @@ discard block |
||
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | public function move($n) { |
| 43 | - if ($n === false) $this->pos = strlen($this->str)-1; |
|
| 44 | - else $this->pos += $n; |
|
| 43 | + if ($n === false) { |
|
| 44 | + $this->pos = strlen($this->str)-1; |
|
| 45 | + } else { |
|
| 46 | + $this->pos += $n; |
|
| 47 | + } |
|
| 45 | 48 | } |
| 46 | 49 | |
| 47 | 50 | public function next() { |
@@ -60,8 +63,11 @@ discard block |
||
| 60 | 63 | |
| 61 | 64 | public function identifyChar($offset = 0) { |
| 62 | 65 | $chr = $this->str[$this->pos + $offset]; |
| 63 | - if (!empty($this->chars[$chr])) return $this->chars[$chr]; |
|
| 64 | - else return Tokenizer::NAME; |
|
| 66 | + if (!empty($this->chars[$chr])) { |
|
| 67 | + return $this->chars[$chr]; |
|
| 68 | + } else { |
|
| 69 | + return Tokenizer::NAME; |
|
| 70 | + } |
|
| 65 | 71 | } |
| 66 | 72 | |
| 67 | 73 | public function has($offset = 0) { |
@@ -85,7 +91,9 @@ discard block |
||
| 85 | 91 | $pos = $this->pos + $offset; |
| 86 | 92 | $char = $this->str[$pos]; |
| 87 | 93 | $end = strpos($this->str, $char, $pos+1); |
| 88 | - while ($end !== false && $this->str[$end-1] == '\\') $end = strpos($this->str, $char, $end+1); |
|
| 94 | + while ($end !== false && $this->str[$end-1] == '\\') { |
|
| 95 | + $end = strpos($this->str, $char, $end+1); |
|
| 96 | + } |
|
| 89 | 97 | |
| 90 | 98 | return substr($this->str, $pos+1, $end-$pos-1); |
| 91 | 99 | } |
@@ -95,7 +103,9 @@ discard block |
||
| 95 | 103 | $close = strpos($this->str, $closeBracket, $open); |
| 96 | 104 | |
| 97 | 105 | $cPos = $open+1; |
| 98 | - while (($cPos = strpos($this->str, $startBracket, $cPos+1)) !== false && $cPos < $close) $close = strpos($this->str, $closeBracket, $close+1); |
|
| 106 | + while (($cPos = strpos($this->str, $startBracket, $cPos+1)) !== false && $cPos < $close) { |
|
| 107 | + $close = strpos($this->str, $closeBracket, $close+1); |
|
| 108 | + } |
|
| 99 | 109 | return substr($this->str, $open+1, $close-$open-1); |
| 100 | 110 | } |
| 101 | 111 | |
@@ -32,9 +32,14 @@ |
||
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | private function processLiterals($tokens, $name, $str) { |
| 35 | - if (is_numeric($name)) $tokens->add(['type' => Tokenizer::NUMERIC, 'value' => $name]); |
|
| 36 | - else if ($name == 'true') $tokens->add(['type' => Tokenizer::BOOL, 'value' => true]); |
|
| 37 | - else if ($name == 'false') $tokens->add(['type' => Tokenizer::BOOL, 'value' => false]); |
|
| 38 | - else $tokens->add(['type' => Tokenizer::NAME, 'value' => $name, 'line' => $str->lineNo()]); |
|
| 35 | + if (is_numeric($name)) { |
|
| 36 | + $tokens->add(['type' => Tokenizer::NUMERIC, 'value' => $name]); |
|
| 37 | + } else if ($name == 'true') { |
|
| 38 | + $tokens->add(['type' => Tokenizer::BOOL, 'value' => true]); |
|
| 39 | + } else if ($name == 'false') { |
|
| 40 | + $tokens->add(['type' => Tokenizer::BOOL, 'value' => false]); |
|
| 41 | + } else { |
|
| 42 | + $tokens->add(['type' => Tokenizer::NAME, 'value' => $name, 'line' => $str->lineNo()]); |
|
| 43 | + } |
|
| 39 | 44 | } |
| 40 | 45 | } |
| 41 | 46 | \ No newline at end of file |