@@ -11,6 +11,9 @@ discard block |
||
11 | 11 | private $line; |
12 | 12 | private $filePath; |
13 | 13 | |
14 | + /** |
|
15 | + * @param integer $line |
|
16 | + */ |
|
14 | 17 | public function __construct(\Transphporm\FunctionSet $functionSet, \Transphporm\Hook\ElementData $elementData, &$line, \Transphporm\FilePath $filePath) { |
15 | 18 | $this->functionSet = $functionSet; |
16 | 19 | $this->elementData = $elementData; |
@@ -58,6 +61,12 @@ discard block |
||
58 | 61 | return $value; |
59 | 62 | } |
60 | 63 | |
64 | + /** |
|
65 | + * @param \DOMElement $element |
|
66 | + * @param integer $count |
|
67 | + * |
|
68 | + * @return \DOMElement |
|
69 | + */ |
|
61 | 70 | private function cloneElement($element, $iteration, $key, $count) { |
62 | 71 | $clone = $element->cloneNode(true); |
63 | 72 | $this->tagElement($clone, $count); |
@@ -77,6 +86,9 @@ discard block |
||
77 | 86 | return isset($values[1]) ? $values[1] : PHP_INT_MAX; |
78 | 87 | } |
79 | 88 | |
89 | + /** |
|
90 | + * @param \Transphporm\Hook\PseudoMatcher $pseudoMatcher |
|
91 | + */ |
|
80 | 92 | private function createHook($newRules, $pseudoMatcher, $properties) { |
81 | 93 | $hook = new \Transphporm\Hook\PropertyHook($newRules, $this->line, null, $this->line, $pseudoMatcher, new \Transphporm\Parser\Value($this->functionSet), $this->functionSet, $this->filePath); |
82 | 94 | foreach ($properties as $name => $property) $hook->registerProperty($name, $property); |
@@ -9,13 +9,13 @@ |
||
9 | 9 | private $functionSet; |
10 | 10 | private $elementData; |
11 | 11 | private $line; |
12 | - private $filePath; |
|
12 | + private $filePath; |
|
13 | 13 | |
14 | 14 | public function __construct(\Transphporm\FunctionSet $functionSet, \Transphporm\Hook\ElementData $elementData, &$line, \Transphporm\FilePath $filePath) { |
15 | 15 | $this->functionSet = $functionSet; |
16 | 16 | $this->elementData = $elementData; |
17 | 17 | $this->line = &$line; |
18 | - $this->filePath = $filePath; |
|
18 | + $this->filePath = $filePath; |
|
19 | 19 | } |
20 | 20 | |
21 | 21 | public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
@@ -20,7 +20,9 @@ discard block |
||
20 | 20 | |
21 | 21 | public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
22 | 22 | $values = $this->fixEmpty($values); |
23 | - if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element); |
|
23 | + if ($element->getAttribute('transphporm') === 'added') { |
|
24 | + return $element->parentNode->removeChild($element); |
|
25 | + } |
|
24 | 26 | $max = $this->getMax($values); |
25 | 27 | $count = 0; |
26 | 28 | $repeat = $this->getRepeatValue($values, $max); |
@@ -29,7 +31,9 @@ discard block |
||
29 | 31 | $hook = $this->createHook($rules, $pseudoMatcher, $properties); |
30 | 32 | |
31 | 33 | foreach ($repeat as $key => $iteration) { |
32 | - if ($count+1 > $max) break; |
|
34 | + if ($count+1 > $max) { |
|
35 | + break; |
|
36 | + } |
|
33 | 37 | $clone = $this->cloneElement($element, $iteration, $key, $count++); |
34 | 38 | //Re-run the hook on the new element, but use the iterated data |
35 | 39 | $hook->run($clone); |
@@ -41,8 +45,9 @@ discard block |
||
41 | 45 | |
42 | 46 | private function getRepeatValue($values, &$max) { |
43 | 47 | $mode = $this->getMode($values); |
44 | - if ($mode === 'each') $repeat = $values[0]; |
|
45 | - else if ($mode === 'loop') { |
|
48 | + if ($mode === 'each') { |
|
49 | + $repeat = $values[0]; |
|
50 | + } else if ($mode === 'loop') { |
|
46 | 51 | $repeat = range($values[0], $max); |
47 | 52 | $max++; |
48 | 53 | } |
@@ -54,7 +59,9 @@ discard block |
||
54 | 59 | } |
55 | 60 | |
56 | 61 | private function fixEmpty($value) { |
57 | - if (empty($value[0])) $value[0] = []; |
|
62 | + if (empty($value[0])) { |
|
63 | + $value[0] = []; |
|
64 | + } |
|
58 | 65 | return $value; |
59 | 66 | } |
60 | 67 | |
@@ -70,7 +77,9 @@ discard block |
||
70 | 77 | |
71 | 78 | private function tagElement($element, $count) { |
72 | 79 | //Mark all but one of the nodes as having been added by transphporm, when the hook is run again, these are removed |
73 | - if ($count > 0) $element->setAttribute('transphporm', 'added'); |
|
80 | + if ($count > 0) { |
|
81 | + $element->setAttribute('transphporm', 'added'); |
|
82 | + } |
|
74 | 83 | } |
75 | 84 | |
76 | 85 | private function getMax($values) { |
@@ -79,7 +88,9 @@ discard block |
||
79 | 88 | |
80 | 89 | private function createHook($newRules, $pseudoMatcher, $properties) { |
81 | 90 | $hook = new \Transphporm\Hook\PropertyHook($newRules, $this->line, null, $this->line, $pseudoMatcher, new \Transphporm\Parser\Value($this->functionSet), $this->functionSet, $this->filePath); |
82 | - foreach ($properties as $name => $property) $hook->registerProperty($name, $property); |
|
91 | + foreach ($properties as $name => $property) { |
|
92 | + $hook->registerProperty($name, $property); |
|
93 | + } |
|
83 | 94 | return $hook; |
84 | 95 | } |
85 | 96 | } |
@@ -22,7 +22,7 @@ |
||
22 | 22 | if (is_file($tss)) { |
23 | 23 | $this->file = $tss; |
24 | 24 | $this->rules = $this->cache->load($tss); |
25 | - $filePath->setBaseDir(dirname(realpath($tss)) . DIRECTORY_SEPARATOR); |
|
25 | + $filePath->setBaseDir(dirname(realpath($tss)).DIRECTORY_SEPARATOR); |
|
26 | 26 | if (empty($this->rules)) $tss = file_get_contents($tss); |
27 | 27 | else return; |
28 | 28 | } |
@@ -23,8 +23,11 @@ discard block |
||
23 | 23 | $this->file = $tss; |
24 | 24 | $this->rules = $this->cache->load($tss); |
25 | 25 | $filePath->setBaseDir(dirname(realpath($tss)) . DIRECTORY_SEPARATOR); |
26 | - if (empty($this->rules)) $tss = file_get_contents($tss); |
|
27 | - else return; |
|
26 | + if (empty($this->rules)) { |
|
27 | + $tss = file_get_contents($tss); |
|
28 | + } else { |
|
29 | + return; |
|
30 | + } |
|
28 | 31 | } |
29 | 32 | $this->tss = $this->stripComments($tss, '//', "\n"); |
30 | 33 | $this->tss = $this->stripComments($this->tss, '/*', '*/'); |
@@ -36,7 +39,9 @@ discard block |
||
36 | 39 | } |
37 | 40 | |
38 | 41 | public function parse($indexStart = 0) { |
39 | - if (!empty($this->rules)) return $this->rules['rules']; |
|
42 | + if (!empty($this->rules)) { |
|
43 | + return $this->rules['rules']; |
|
44 | + } |
|
40 | 45 | $rules = $this->parseTokens($indexStart); |
41 | 46 | usort($rules, [$this, 'sortRules']); |
42 | 47 | $this->checkError($rules); |
@@ -50,12 +55,12 @@ discard block |
||
50 | 55 | if ($processing = $this->processingInstructions($token, count($this->rules)+$indexStart)) { |
51 | 56 | $this->rules = array_merge($this->rules, $processing); |
52 | 57 | continue; |
53 | - } |
|
54 | - else if ($token['type'] === Tokenizer::NEW_LINE) { |
|
58 | + } else if ($token['type'] === Tokenizer::NEW_LINE) { |
|
55 | 59 | $line++; |
56 | 60 | continue; |
61 | + } else { |
|
62 | + $this->addRules($token, $indexStart, $line); |
|
57 | 63 | } |
58 | - else $this->addRules($token, $indexStart, $line); |
|
59 | 64 | } |
60 | 65 | return $this->rules; |
61 | 66 | } |
@@ -63,14 +68,18 @@ discard block |
||
63 | 68 | private function addRules($token, $indexStart, $line) { |
64 | 69 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
65 | 70 | $this->tss->skip(count($selector)); |
66 | - if (count($selector) === 0) return; |
|
71 | + if (count($selector) === 0) { |
|
72 | + return; |
|
73 | + } |
|
67 | 74 | |
68 | 75 | $newRules = $this->cssToRules($selector, count($this->rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $line); |
69 | 76 | $this->rules = $this->writeRule($this->rules, $newRules); |
70 | 77 | } |
71 | 78 | |
72 | 79 | private function checkError($rules) { |
73 | - if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
|
80 | + if (empty($rules) && count($this->tss) > 0) { |
|
81 | + throw new \Exception('No TSS rules parsed'); |
|
82 | + } |
|
74 | 83 | } |
75 | 84 | |
76 | 85 | private function CssToRules($selector, $index, $properties, $line) { |
@@ -97,7 +106,9 @@ discard block |
||
97 | 106 | } |
98 | 107 | |
99 | 108 | private function processingInstructions($token, $indexStart) { |
100 | - if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
|
109 | + if ($token['type'] !== Tokenizer::AT_SIGN) { |
|
110 | + return false; |
|
111 | + } |
|
101 | 112 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
102 | 113 | $funcName = $tokens->from(Tokenizer::NAME, true)->read(); |
103 | 114 | $args = $this->valueParser->parseTokens($tokens->from(Tokenizer::NAME)); |
@@ -109,8 +120,11 @@ discard block |
||
109 | 120 | } |
110 | 121 | |
111 | 122 | private function import($args, $indexStart) { |
112 | - if ($this->file !== null) $fileName = $fileName = $this->filePath->getFilePath($args[0]); |
|
113 | - else $fileName = $args[0]; |
|
123 | + if ($this->file !== null) { |
|
124 | + $fileName = $fileName = $this->filePath->getFilePath($args[0]); |
|
125 | + } else { |
|
126 | + $fileName = $args[0]; |
|
127 | + } |
|
114 | 128 | $this->import[] = $fileName; |
115 | 129 | $baseDirTemp = $this->filePath->getFilePath(); |
116 | 130 | $sheet = new Sheet($fileName, $this->xPath, $this->valueParser, $this->cache, $this->filePath); |
@@ -120,7 +134,9 @@ discard block |
||
120 | 134 | |
121 | 135 | private function sortRules($a, $b) { |
122 | 136 | //If they have the same depth, compare on index |
123 | - if ($a->depth === $b->depth) return $a->index < $b->index ? -1 : 1; |
|
137 | + if ($a->depth === $b->depth) { |
|
138 | + return $a->index < $b->index ? -1 : 1; |
|
139 | + } |
|
124 | 140 | |
125 | 141 | return ($a->depth < $b->depth) ? -1 : 1; |
126 | 142 | } |
@@ -129,7 +145,9 @@ discard block |
||
129 | 145 | $pos = 0; |
130 | 146 | while (($pos = strpos($str, $open, $pos)) !== false) { |
131 | 147 | $end = strpos($str, $close, $pos); |
132 | - if ($end === false) break; |
|
148 | + if ($end === false) { |
|
149 | + break; |
|
150 | + } |
|
133 | 151 | $str = substr_replace($str, '', $pos, $end-$pos+strlen($close)); |
134 | 152 | } |
135 | 153 |
@@ -17,7 +17,7 @@ |
||
17 | 17 | private $functionSet; |
18 | 18 | |
19 | 19 | public function __construct(array $rules, &$configLine, $file, $line, PseudoMatcher $pseudoMatcher, |
20 | - \Transphporm\Parser\Value $valueParser, \Transphporm\FunctionSet $functionSet, \Transphporm\FilePath $filePath) { |
|
20 | + \Transphporm\Parser\Value $valueParser, \Transphporm\FunctionSet $functionSet, \Transphporm\FilePath $filePath) { |
|
21 | 21 | $this->rules = $rules; |
22 | 22 | $this->configLine = &$configLine; |
23 | 23 | $this->file = $file; |
@@ -25,7 +25,7 @@ |
||
25 | 25 | $this->valueParser = $valueParser; |
26 | 26 | $this->pseudoMatcher = $pseudoMatcher; |
27 | 27 | $this->functionSet = $functionSet; |
28 | - if ($this->file !== null) $filePath->setBaseDir(dirname(realpath($this->file)) . DIRECTORY_SEPARATOR); |
|
28 | + if ($this->file !== null) $filePath->setBaseDir(dirname(realpath($this->file)).DIRECTORY_SEPARATOR); |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | public function run(\DomElement $element) { |
@@ -25,7 +25,9 @@ discard block |
||
25 | 25 | $this->valueParser = $valueParser; |
26 | 26 | $this->pseudoMatcher = $pseudoMatcher; |
27 | 27 | $this->functionSet = $functionSet; |
28 | - if ($this->file !== null) $filePath->setBaseDir(dirname(realpath($this->file)) . DIRECTORY_SEPARATOR); |
|
28 | + if ($this->file !== null) { |
|
29 | + $filePath->setBaseDir(dirname(realpath($this->file)) . DIRECTORY_SEPARATOR); |
|
30 | + } |
|
29 | 31 | } |
30 | 32 | |
31 | 33 | public function run(\DomElement $element) { |
@@ -33,10 +35,11 @@ discard block |
||
33 | 35 | $this->configLine = $this->line; |
34 | 36 | try { |
35 | 37 | //Don't run if there's a pseudo element like nth-child() and this element doesn't match it |
36 | - if (!$this->pseudoMatcher->matches($element)) return; |
|
38 | + if (!$this->pseudoMatcher->matches($element)) { |
|
39 | + return; |
|
40 | + } |
|
37 | 41 | $this->callProperties($element); |
38 | - } |
|
39 | - catch (\Transphporm\RunException $e) { |
|
42 | + } catch (\Transphporm\RunException $e) { |
|
40 | 43 | throw new \Transphporm\Exception($e, $this->file, $this->line); |
41 | 44 | } |
42 | 45 | } |
@@ -47,7 +50,9 @@ discard block |
||
47 | 50 | private function callProperties($element) { |
48 | 51 | foreach ($this->rules as $name => $value) { |
49 | 52 | $result = $this->callProperty($name, $element, $this->getArgs($value)); |
50 | - if ($result === false) break; |
|
53 | + if ($result === false) { |
|
54 | + break; |
|
55 | + } |
|
51 | 56 | } |
52 | 57 | } |
53 | 58 | private function getArgs($value) { |
@@ -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 | } |
@@ -51,7 +51,7 @@ |
||
51 | 51 | |
52 | 52 | $cachedOutput = $this->loadTemplate(); |
53 | 53 | //To be a valid XML document it must have a root element, automatically wrap it in <template> to ensure it does |
54 | - $template = new Template($this->isValidDoc($cachedOutput['body']) ? str_ireplace('<!doctype', '<!DOCTYPE', $cachedOutput['body']) : '<template>' . $cachedOutput['body'] . '</template>' ); |
|
54 | + $template = new Template($this->isValidDoc($cachedOutput['body']) ? str_ireplace('<!doctype', '<!DOCTYPE', $cachedOutput['body']) : '<template>'.$cachedOutput['body'].'</template>'); |
|
55 | 55 | $valueParser = new Parser\Value($functionSet); |
56 | 56 | $config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix()), new FilePath($this->rootDir), $headers); |
57 | 57 |
@@ -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 |
@@ -55,7 +57,9 @@ discard block |
||
55 | 57 | $valueParser = new Parser\Value($functionSet); |
56 | 58 | $config = new Config($functionSet, $valueParser, $elementData, new Hook\Formatter(), new Parser\CssToXpath($functionSet, $template->getPrefix()), new FilePath($this->rootDir), $headers); |
57 | 59 | |
58 | - foreach ($this->modules as $module) $module->load($config); |
|
60 | + foreach ($this->modules as $module) { |
|
61 | + $module->load($config); |
|
62 | + } |
|
59 | 63 | |
60 | 64 | $this->processRules($template, $config); |
61 | 65 | |
@@ -70,7 +74,9 @@ discard block |
||
70 | 74 | $rules = $this->getRules($template, $config); |
71 | 75 | |
72 | 76 | foreach ($rules as $rule) { |
73 | - if ($rule->shouldRun($this->time)) $this->executeTssRule($rule, $template, $config); |
|
77 | + if ($rule->shouldRun($this->time)) { |
|
78 | + $this->executeTssRule($rule, $template, $config); |
|
79 | + } |
|
74 | 80 | } |
75 | 81 | } |
76 | 82 | |
@@ -95,8 +101,9 @@ discard block |
||
95 | 101 | if (trim($this->template)[0] !== '<') { |
96 | 102 | $xml = $this->cache->load($this->template, filemtime($this->template)); |
97 | 103 | return $xml ? $xml : ['body' => file_get_contents($this->template), 'headers' => []]; |
104 | + } else { |
|
105 | + return ['body' => $this->template, 'headers' => []]; |
|
98 | 106 | } |
99 | - else return ['body' => $this->template, 'headers' => []]; |
|
100 | 107 | } |
101 | 108 | |
102 | 109 | //Load the TSS rules either from a file or as a string |
@@ -2,20 +2,20 @@ |
||
2 | 2 | namespace Transphporm; |
3 | 3 | |
4 | 4 | class FilePath { |
5 | - private $baseDir; |
|
6 | - private $customBase; |
|
5 | + private $baseDir; |
|
6 | + private $customBase; |
|
7 | 7 | |
8 | - public function __construct($customBase = null) { |
|
9 | - $this->baseDir = ""; |
|
10 | - if ($customBase === null) $this->customBase = getcwd(); |
|
11 | - else $this->customBase = rtrim($customBase, '/'); |
|
12 | - } |
|
8 | + public function __construct($customBase = null) { |
|
9 | + $this->baseDir = ""; |
|
10 | + if ($customBase === null) $this->customBase = getcwd(); |
|
11 | + else $this->customBase = rtrim($customBase, '/'); |
|
12 | + } |
|
13 | 13 | |
14 | - public function setBaseDir($baseDir) { |
|
15 | - $this->baseDir = $baseDir; |
|
16 | - } |
|
14 | + public function setBaseDir($baseDir) { |
|
15 | + $this->baseDir = $baseDir; |
|
16 | + } |
|
17 | 17 | |
18 | - public function getFilePath($filePath = "") { |
|
18 | + public function getFilePath($filePath = "") { |
|
19 | 19 | if (isset($filePath[0]) && $filePath[0] == "/") return $this->customBase . $filePath; |
20 | 20 | else return $this->baseDir . $filePath; |
21 | 21 | } |
@@ -7,8 +7,11 @@ discard block |
||
7 | 7 | |
8 | 8 | public function __construct($customBase = null) { |
9 | 9 | $this->baseDir = ""; |
10 | - if ($customBase === null) $this->customBase = getcwd(); |
|
11 | - else $this->customBase = rtrim($customBase, '/'); |
|
10 | + if ($customBase === null) { |
|
11 | + $this->customBase = getcwd(); |
|
12 | + } else { |
|
13 | + $this->customBase = rtrim($customBase, '/'); |
|
14 | + } |
|
12 | 15 | } |
13 | 16 | |
14 | 17 | public function setBaseDir($baseDir) { |
@@ -16,7 +19,10 @@ discard block |
||
16 | 19 | } |
17 | 20 | |
18 | 21 | public function getFilePath($filePath = "") { |
19 | - if (isset($filePath[0]) && $filePath[0] == "/") return $this->customBase . $filePath; |
|
20 | - else return $this->baseDir . $filePath; |
|
22 | + if (isset($filePath[0]) && $filePath[0] == "/") { |
|
23 | + return $this->customBase . $filePath; |
|
24 | + } else { |
|
25 | + return $this->baseDir . $filePath; |
|
26 | + } |
|
21 | 27 | } |
22 | 28 | } |