| Conditions | 36 |
| Paths | > 20000 |
| Total Lines | 113 |
| Code Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 87 | public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) |
||
| 88 | { |
||
| 89 | $params = $compiler->getCompiledParams($params); |
||
| 90 | $tpl = $compiler->getTemplateSource($params['tplPointer']); |
||
| 91 | |||
| 92 | // assigns params |
||
| 93 | $src = $params['from']; |
||
| 94 | |||
| 95 | if ($params['item'] !== 'null') { |
||
| 96 | if ($params['key'] !== 'null') { |
||
| 97 | $key = $params['key']; |
||
| 98 | } |
||
| 99 | $val = $params['item']; |
||
| 100 | } elseif ($params['key'] !== 'null') { |
||
| 101 | $val = $params['key']; |
||
| 102 | } else { |
||
| 103 | throw new CompilationException($compiler, 'Foreach <em>item</em> parameter missing'); |
||
| 104 | } |
||
| 105 | $name = $params['name']; |
||
| 106 | |||
| 107 | if (substr($val, 0, 1) !== '"' && substr($val, 0, 1) !== '\'') { |
||
| 108 | throw new CompilationException($compiler, 'Foreach <em>item</em> parameter must be of type string'); |
||
| 109 | } |
||
| 110 | if (isset($key) && substr($val, 0, 1) !== '"' && substr($val, 0, 1) !== '\'') { |
||
| 111 | throw new CompilationException($compiler, 'Foreach <em>key</em> parameter must be of type string'); |
||
| 112 | } |
||
| 113 | |||
| 114 | // evaluates which global variables have to be computed |
||
| 115 | $varName = '$dwoo.foreach.' . trim($name, '"\'') . '.'; |
||
| 116 | $shortVarName = '$.foreach.' . trim($name, '"\'') . '.'; |
||
| 117 | $usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false; |
||
| 118 | $usesFirst = strpos($tpl, $varName . 'first') !== false || strpos($tpl, $shortVarName . 'first') !== false; |
||
| 119 | $usesLast = strpos($tpl, $varName . 'last') !== false || strpos($tpl, $shortVarName . 'last') !== false; |
||
| 120 | $usesIndex = $usesFirst || strpos($tpl, $varName . 'index') !== false || strpos($tpl, $shortVarName . 'index') !== false; |
||
| 121 | $usesIteration = $usesLast || strpos($tpl, $varName . 'iteration') !== false || strpos($tpl, $shortVarName . 'iteration') !== false; |
||
| 122 | $usesShow = strpos($tpl, $varName . 'show') !== false || strpos($tpl, $shortVarName . 'show') !== false; |
||
| 123 | $usesTotal = $usesLast || strpos($tpl, $varName . 'total') !== false || strpos($tpl, $shortVarName . 'total') !== false; |
||
| 124 | |||
| 125 | if (strpos($name, '$this->scope[') !== false) { |
||
| 126 | $usesAny = $usesFirst = $usesLast = $usesIndex = $usesIteration = $usesShow = $usesTotal = true; |
||
| 127 | } |
||
| 128 | |||
| 129 | // override globals vars if implode is used |
||
| 130 | if ($params['implode'] !== 'null') { |
||
| 131 | $implode = $params['implode']; |
||
| 132 | $usesAny = true; |
||
| 133 | $usesLast = true; |
||
| 134 | $usesIteration = true; |
||
| 135 | $usesTotal = true; |
||
| 136 | } |
||
| 137 | |||
| 138 | // gets foreach id |
||
| 139 | $cnt = self::$cnt ++; |
||
| 140 | |||
| 141 | // build pre content output |
||
| 142 | $pre = Compiler::PHP_OPEN . "\n" . '$_fh' . $cnt . '_data = ' . $src . ';'; |
||
| 143 | // adds foreach properties |
||
| 144 | if ($usesAny) { |
||
| 145 | $pre .= "\n" . '$this->globals["foreach"][' . $name . '] = array' . "\n("; |
||
| 146 | if ($usesIndex) { |
||
| 147 | $pre .= "\n\t" . '"index" => 0,'; |
||
| 148 | } |
||
| 149 | if ($usesIteration) { |
||
| 150 | $pre .= "\n\t" . '"iteration" => 1,'; |
||
| 151 | } |
||
| 152 | if ($usesFirst) { |
||
| 153 | $pre .= "\n\t" . '"first" => null,'; |
||
| 154 | } |
||
| 155 | if ($usesLast) { |
||
| 156 | $pre .= "\n\t" . '"last" => null,'; |
||
| 157 | } |
||
| 158 | if ($usesShow) { |
||
| 159 | $pre .= "\n\t" . '"show" => $this->isArray($_fh' . $cnt . '_data, true),'; |
||
| 160 | } |
||
| 161 | if ($usesTotal) { |
||
| 162 | $pre .= "\n\t" . '"total" => $this->count($_fh' . $cnt . '_data),'; |
||
| 163 | } |
||
| 164 | $pre .= "\n);\n" . '$_fh' . $cnt . '_glob =& $this->globals["foreach"][' . $name . '];'; |
||
| 165 | } |
||
| 166 | // checks if foreach must be looped |
||
| 167 | $pre .= "\n" . 'if ($this->isTraversable($_fh' . $cnt . '_data' . (isset($params['hasElse']) ? ', true' : '') . ') == true)' . "\n{"; |
||
| 168 | // iterates over keys |
||
| 169 | $pre .= "\n\t" . 'foreach ($_fh' . $cnt . '_data as ' . (isset($key) ? '$this->scope[' . $key . ']=>' : '') . '$this->scope[' . $val . '])' . "\n\t{"; |
||
| 170 | // updates properties |
||
| 171 | if ($usesFirst) { |
||
| 172 | $pre .= "\n\t\t" . '$_fh' . $cnt . '_glob["first"] = (string) ($_fh' . $cnt . '_glob["index"] === 0);'; |
||
| 173 | } |
||
| 174 | if ($usesLast) { |
||
| 175 | $pre .= "\n\t\t" . '$_fh' . $cnt . '_glob["last"] = (string) ($_fh' . $cnt . '_glob["iteration"] === $_fh' . $cnt . '_glob["total"]);'; |
||
| 176 | } |
||
| 177 | $pre .= "\n/* -- foreach start output */\n" . Compiler::PHP_CLOSE; |
||
| 178 | |||
| 179 | // build post content output |
||
| 180 | $post = Compiler::PHP_OPEN . "\n"; |
||
| 181 | |||
| 182 | if (isset($implode)) { |
||
| 183 | $post .= '/* -- implode */' . "\n" . 'if (!$_fh' . $cnt . '_glob["last"]) {' . "\n\t" . 'echo ' . $implode . ";\n}\n"; |
||
| 184 | } |
||
| 185 | $post .= '/* -- foreach end output */'; |
||
| 186 | // update properties |
||
| 187 | if ($usesIndex) { |
||
| 188 | $post .= "\n\t\t" . '$_fh' . $cnt . '_glob["index"]+=1;'; |
||
| 189 | } |
||
| 190 | if ($usesIteration) { |
||
| 191 | $post .= "\n\t\t" . '$_fh' . $cnt . '_glob["iteration"]+=1;'; |
||
| 192 | } |
||
| 193 | // end loop |
||
| 194 | $post .= "\n\t}\n}" . Compiler::PHP_CLOSE; |
||
| 195 | if (isset($params['hasElse'])) { |
||
| 196 | $post .= $params['hasElse']; |
||
| 197 | } |
||
| 198 | |||
| 199 | return $pre . $content . $post; |
||
| 200 | } |
||
| 202 |