@@ -20,11 +20,15 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | public function run(\DomElement $element) { |
| 22 | 22 | //Don't run if there's a pseudo element like nth-child() and this element doesn't match it |
| 23 | - if (!$this->pseudoMatcher->matches($element)) return; |
|
| 23 | + if (!$this->pseudoMatcher->matches($element)) { |
|
| 24 | + return; |
|
| 25 | + } |
|
| 24 | 26 | |
| 25 | 27 | foreach ($this->rules as $name => $value) { |
| 26 | 28 | $result = $this->callProperty($name, $element, $this->valueParser->parse(trim($value), $element)); |
| 27 | - if ($result === false) break; |
|
| 29 | + if ($result === false) { |
|
| 30 | + break; |
|
| 31 | + } |
|
| 28 | 32 | } |
| 29 | 33 | } |
| 30 | 34 | |
@@ -45,7 +49,9 @@ discard block |
||
| 45 | 49 | } |
| 46 | 50 | |
| 47 | 51 | private function callProperty($name, $element, $value) { |
| 48 | - if (isset($this->properties[$name])) return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties); |
|
| 52 | + if (isset($this->properties[$name])) { |
|
| 53 | + return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties); |
|
| 54 | + } |
|
| 49 | 55 | return false; |
| 50 | 56 | } |
| 51 | 57 | } |
@@ -49,7 +49,9 @@ discard block |
||
| 49 | 49 | $this->data = new ValueData($data ? $data : $this->baseData); |
| 50 | 50 | $this->last = null; |
| 51 | 51 | |
| 52 | - if (count($tokens) <= 0) return [$data]; |
|
| 52 | + if (count($tokens) <= 0) { |
|
| 53 | + return [$data]; |
|
| 54 | + } |
|
| 53 | 55 | |
| 54 | 56 | foreach (new TokenFilterIterator($tokens, [Tokenizer::WHITESPACE]) as $token) { |
| 55 | 57 | $this->{$this->tokenFuncs[$token['type']]}($token); |
@@ -71,13 +73,15 @@ discard block |
||
| 71 | 73 | //Reads the last selected value from $data regardless if it's an array or object and overrides $this->data with the new value |
| 72 | 74 | //Dot moves $data to the next object in $data foo.bar moves the $data pointer from `foo` to `bar` |
| 73 | 75 | private function processDot($token) { |
| 74 | - if ($this->last !== null) $this->data->traverse($this->last); |
|
| 75 | - else { |
|
| 76 | + if ($this->last !== null) { |
|
| 77 | + $this->data->traverse($this->last); |
|
| 78 | + } else { |
|
| 76 | 79 | //When . is not preceeded by anything, treat it as part of the string instead of an operator |
| 77 | 80 | // foo.bar is treated as looking up `bar` in `foo` whereas .foo is treated as the string ".foo" |
| 78 | 81 | $lastResult = $this->result->pop(); |
| 79 | - if ($lastResult) $this->data = new ValueData($lastResult); |
|
| 80 | - else { |
|
| 82 | + if ($lastResult) { |
|
| 83 | + $this->data = new ValueData($lastResult); |
|
| 84 | + } else { |
|
| 81 | 85 | $this->processString(['value' => '.']); |
| 82 | 86 | $this->result->setMode(Tokenizer::CONCAT); |
| 83 | 87 | } |
@@ -90,9 +94,10 @@ discard block |
||
| 90 | 94 | $parser = new Value($this->baseData, $this->autoLookup); |
| 91 | 95 | if ($this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($this->last)) { |
| 92 | 96 | $this->callTransphpormFunctions($token); |
| 93 | - } |
|
| 94 | - else { |
|
| 95 | - if ($this->last !== null) $this->data->traverse($this->last); |
|
| 97 | + } else { |
|
| 98 | + if ($this->last !== null) { |
|
| 99 | + $this->data->traverse($this->last); |
|
| 100 | + } |
|
| 96 | 101 | $this->last = $parser->parseTokens($token['value'], null)[0]; |
| 97 | 102 | } |
| 98 | 103 | } |
@@ -112,8 +117,7 @@ discard block |
||
| 112 | 117 | private function processBrackets($token) { |
| 113 | 118 | if ($this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($this->last)) { |
| 114 | 119 | $this->callTransphpormFunctions($token); |
| 115 | - } |
|
| 116 | - else { |
|
| 120 | + } else { |
|
| 117 | 121 | $this->processNested($token); |
| 118 | 122 | } |
| 119 | 123 | } |
@@ -130,7 +134,9 @@ discard block |
||
| 130 | 134 | foreach ($this->result->getResult() as $i => $value) { |
| 131 | 135 | if (is_scalar($value)) { |
| 132 | 136 | $val = $this->data->read($value); |
| 133 | - if ($val) $this->result[$i] = $val; |
|
| 137 | + if ($val) { |
|
| 138 | + $this->result[$i] = $val; |
|
| 139 | + } |
|
| 134 | 140 | } |
| 135 | 141 | } |
| 136 | 142 | $this->last = null; |
@@ -142,12 +148,10 @@ discard block |
||
| 142 | 148 | try { |
| 143 | 149 | $value = $this->data->extract($this->last, $this->autoLookup); |
| 144 | 150 | $this->result->processValue($value); |
| 145 | - } |
|
| 146 | - catch (\UnexpectedValueException $e) { |
|
| 151 | + } catch (\UnexpectedValueException $e) { |
|
| 147 | 152 | if (!$this->autoLookup) { |
| 148 | 153 | $this->result->processValue($this->last); |
| 149 | - } |
|
| 150 | - else { |
|
| 154 | + } else { |
|
| 151 | 155 | $this->result->clear(); |
| 152 | 156 | $this->result[0] = false; |
| 153 | 157 | } |
@@ -69,8 +69,11 @@ discard block |
||
| 69 | 69 | $i += $this->doStrings($tokens, $char, $i); |
| 70 | 70 | $i += $this->doBrackets($tokens, $char, $i); |
| 71 | 71 | } |
| 72 | - if ($returnObj) return new Tokens($tokens); |
|
| 73 | - else return $tokens; |
|
| 72 | + if ($returnObj) { |
|
| 73 | + return new Tokens($tokens); |
|
| 74 | + } else { |
|
| 75 | + return $tokens; |
|
| 76 | + } |
|
| 74 | 77 | } |
| 75 | 78 | |
| 76 | 79 | private function doSimpleTokens(&$tokens, $char) { |
@@ -93,10 +96,15 @@ discard block |
||
| 93 | 96 | } |
| 94 | 97 | |
| 95 | 98 | private function processLiterals(&$tokens, $name) { |
| 96 | - if (is_numeric($name)) $tokens[] = ['type' => self::NUMERIC, 'value' => $name]; |
|
| 97 | - else if ($name == 'true') $tokens[] = ['type' => self::BOOL, 'value' => true]; |
|
| 98 | - else if ($name == 'false') $tokens[] = ['type' => self::BOOL, 'value' => false]; |
|
| 99 | - else $tokens[] = ['type' => self::NAME, 'value' => $name]; |
|
| 99 | + if (is_numeric($name)) { |
|
| 100 | + $tokens[] = ['type' => self::NUMERIC, 'value' => $name]; |
|
| 101 | + } else if ($name == 'true') { |
|
| 102 | + $tokens[] = ['type' => self::BOOL, 'value' => true]; |
|
| 103 | + } else if ($name == 'false') { |
|
| 104 | + $tokens[] = ['type' => self::BOOL, 'value' => false]; |
|
| 105 | + } else { |
|
| 106 | + $tokens[] = ['type' => self::NAME, 'value' => $name]; |
|
| 107 | + } |
|
| 100 | 108 | } |
| 101 | 109 | |
| 102 | 110 | private function doBrackets(&$tokens, $char, $i) { |
@@ -130,7 +138,9 @@ discard block |
||
| 130 | 138 | private function extractString($pos) { |
| 131 | 139 | $char = $this->str[$pos]; |
| 132 | 140 | $end = strpos($this->str, $char, $pos+1); |
| 133 | - while ($end !== false && $this->str[$end-1] == '\\') $end = strpos($this->str, $char, $end+1); |
|
| 141 | + while ($end !== false && $this->str[$end-1] == '\\') { |
|
| 142 | + $end = strpos($this->str, $char, $end+1); |
|
| 143 | + } |
|
| 134 | 144 | |
| 135 | 145 | return substr($this->str, $pos+1, $end-$pos-1); |
| 136 | 146 | } |
@@ -139,19 +149,27 @@ discard block |
||
| 139 | 149 | $close = strpos($this->str, $closeBracket, $open); |
| 140 | 150 | |
| 141 | 151 | $cPos = $open+1; |
| 142 | - while (($cPos = strpos($this->str, $startBracket, $cPos+1)) !== false && $cPos < $close) $close = strpos($this->str, $closeBracket, $close+1); |
|
| 152 | + while (($cPos = strpos($this->str, $startBracket, $cPos+1)) !== false && $cPos < $close) { |
|
| 153 | + $close = strpos($this->str, $closeBracket, $close+1); |
|
| 154 | + } |
|
| 143 | 155 | return substr($this->str, $open+1, $close-$open-1); |
| 144 | 156 | } |
| 145 | 157 | |
| 146 | 158 | private function identifyChar($chr) { |
| 147 | - if (isset($this->chars[$chr])) return $this->chars[$chr]; |
|
| 148 | - else return self::NAME; |
|
| 159 | + if (isset($this->chars[$chr])) { |
|
| 160 | + return $this->chars[$chr]; |
|
| 161 | + } else { |
|
| 162 | + return self::NAME; |
|
| 163 | + } |
|
| 149 | 164 | } |
| 150 | 165 | |
| 151 | 166 | private function getChar($num) { |
| 152 | 167 | $chars = array_reverse($this->chars); |
| 153 | - if (isset($chars[$num])) return $chars[$num]; |
|
| 154 | - else return false; |
|
| 168 | + if (isset($chars[$num])) { |
|
| 169 | + return $chars[$num]; |
|
| 170 | + } else { |
|
| 171 | + return false; |
|
| 172 | + } |
|
| 155 | 173 | } |
| 156 | 174 | |
| 157 | 175 | public function serialize($tokens) { |
@@ -169,8 +187,11 @@ discard block |
||
| 169 | 187 | |
| 170 | 188 | private function serializeValue($token) { |
| 171 | 189 | if (isset($token['value'])) { |
| 172 | - if ($token['value'] instanceof Tokens) return $this->serialize($token['value']); |
|
| 173 | - else return $token['value']; |
|
| 190 | + if ($token['value'] instanceof Tokens) { |
|
| 191 | + return $this->serialize($token['value']); |
|
| 192 | + } else { |
|
| 193 | + return $token['value']; |
|
| 194 | + } |
|
| 174 | 195 | } |
| 175 | 196 | } |
| 176 | 197 | } |
@@ -69,7 +69,9 @@ discard block |
||
| 69 | 69 | $selector->type = $token['type']; |
| 70 | 70 | $selectors[] = $selector; |
| 71 | 71 | } |
| 72 | - if (isset($token['value'])) $selectors[count($selectors)-1]->string = $token['value']; |
|
| 72 | + if (isset($token['value'])) { |
|
| 73 | + $selectors[count($selectors)-1]->string = $token['value']; |
|
| 74 | + } |
|
| 73 | 75 | } |
| 74 | 76 | return $selectors; |
| 75 | 77 | } |
@@ -79,7 +81,9 @@ discard block |
||
| 79 | 81 | $selectors = $this->split($css); |
| 80 | 82 | $xpath = '/'; |
| 81 | 83 | foreach ($selectors as $selector) { |
| 82 | - if (isset($this->translators[$selector->type])) $xpath .= $this->translators[$selector->type]($selector->string, $xpath); |
|
| 84 | + if (isset($this->translators[$selector->type])) { |
|
| 85 | + $xpath .= $this->translators[$selector->type]($selector->string, $xpath); |
|
| 86 | + } |
|
| 83 | 87 | } |
| 84 | 88 | |
| 85 | 89 | $xpath = str_replace('/[', '/*[', $xpath); |
@@ -90,7 +94,9 @@ discard block |
||
| 90 | 94 | private function removeSpacesFromDirectDecend($css) { |
| 91 | 95 | $tokens = []; |
| 92 | 96 | foreach ($css->splitOnToken(Tokenizer::GREATER_THAN) as $token) { |
| 93 | - foreach ($token->trim() as $t) $tokens[] = $t; |
|
| 97 | + foreach ($token->trim() as $t) { |
|
| 98 | + $tokens[] = $t; |
|
| 99 | + } |
|
| 94 | 100 | $tokens[] = ['type' => Tokenizer::GREATER_THAN]; |
| 95 | 101 | } |
| 96 | 102 | return new Tokens(array_slice($tokens, 0, -1)); |
@@ -15,21 +15,28 @@ discard block |
||
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
| 18 | - if (!$this->shouldRun($element)) return false; |
|
| 18 | + if (!$this->shouldRun($element)) { |
|
| 19 | + return false; |
|
| 20 | + } |
|
| 19 | 21 | $values = $this->formatter->format($values, $rules); |
| 20 | 22 | |
| 21 | 23 | if (!$this->processPseudo($values, $element, $pseudoMatcher)) { |
| 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; |
@@ -53,9 +60,10 @@ discard block |
||
| 53 | 60 | private function getNode($node, $document) { |
| 54 | 61 | foreach ($node as $n) { |
| 55 | 62 | if (is_array($n)) { |
| 56 | - foreach ($this->getNode($n, $document) as $new) yield $new; |
|
| 57 | - } |
|
| 58 | - else { |
|
| 63 | + foreach ($this->getNode($n, $document) as $new) { |
|
| 64 | + yield $new; |
|
| 65 | + } |
|
| 66 | + } else { |
|
| 59 | 67 | yield $this->convertNode($n, $document); |
| 60 | 68 | } |
| 61 | 69 | } |
@@ -66,9 +74,10 @@ discard block |
||
| 66 | 74 | $new = $document->importNode($node, true); |
| 67 | 75 | //Removing this might cause problems with caching... |
| 68 | 76 | //$new->setAttribute('transphporm', 'added'); |
| 69 | - } |
|
| 70 | - else { |
|
| 71 | - if ($node instanceof \DomText) $node = $node->nodeValue; |
|
| 77 | + } else { |
|
| 78 | + if ($node instanceof \DomText) { |
|
| 79 | + $node = $node->nodeValue; |
|
| 80 | + } |
|
| 72 | 81 | $new = $document->createElement('text'); |
| 73 | 82 | |
| 74 | 83 | $new->appendChild($document->createTextNode($node)); |
@@ -114,6 +123,8 @@ discard block |
||
| 114 | 123 | } |
| 115 | 124 | |
| 116 | 125 | private function removeAllChildren($element) { |
| 117 | - while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
|
| 126 | + while ($element->hasChildNodes()) { |
|
| 127 | + $element->removeChild($element->firstChild); |
|
| 128 | + } |
|
| 118 | 129 | } |
| 119 | 130 | } |