| Conditions | 29 |
| Paths | > 20000 |
| Total Lines | 113 |
| Code Lines | 68 |
| 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 |
||
| 78 | public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) |
||
| 79 | { |
||
| 80 | $params = $compiler->getCompiledParams($params); |
||
| 81 | $tpl = $compiler->getTemplateSource($params['tplPointer']); |
||
| 82 | |||
| 83 | // assigns params |
||
| 84 | $from = $params['from']; |
||
| 85 | $name = $params['name']; |
||
| 86 | $step = $params['step']; |
||
| 87 | $to = $params['to']; |
||
| 88 | |||
| 89 | // evaluates which global variables have to be computed |
||
| 90 | $varName = '$dwoo.for.' . trim($name, '"\'') . '.'; |
||
| 91 | $shortVarName = '$.for.' . trim($name, '"\'') . '.'; |
||
| 92 | $usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false; |
||
| 93 | $usesFirst = strpos($tpl, $varName . 'first') !== false || strpos($tpl, $shortVarName . 'first') !== false; |
||
| 94 | $usesLast = strpos($tpl, $varName . 'last') !== false || strpos($tpl, $shortVarName . 'last') !== false; |
||
| 95 | $usesIndex = strpos($tpl, $varName . 'index') !== false || strpos($tpl, $shortVarName . 'index') !== false; |
||
| 96 | $usesIteration = $usesFirst || $usesLast || strpos($tpl, $varName . 'iteration') !== false || strpos($tpl, $shortVarName . 'iteration') !== false; |
||
| 97 | $usesShow = strpos($tpl, $varName . 'show') !== false || strpos($tpl, $shortVarName . 'show') !== false; |
||
| 98 | $usesTotal = $usesLast || strpos($tpl, $varName . 'total') !== false || strpos($tpl, $shortVarName . 'total') !== false; |
||
| 99 | |||
| 100 | if (strpos($name, '$this->scope[') !== false) { |
||
| 101 | $usesAny = $usesFirst = $usesLast = $usesIndex = $usesIteration = $usesShow = $usesTotal = true; |
||
| 102 | } |
||
| 103 | |||
| 104 | // gets foreach id |
||
| 105 | $cnt = self::$cnt ++; |
||
| 106 | |||
| 107 | // builds pre processing output for |
||
| 108 | $out = Compiler::PHP_OPEN . "\n" . '$_for' . $cnt . '_from = ' . $from . ';' . "\n" . '$_for' . $cnt . '_to = ' . $to . ';' . "\n" . '$_for' . $cnt . '_step = abs(' . $step . ');' . "\n" . 'if (is_numeric($_for' . $cnt . '_from) && !is_numeric($_for' . $cnt . '_to)) { $this->triggerError(\'For requires the <em>to</em> parameter when using a numerical <em>from</em>\'); }' . "\n" . '$tmp_shows = $this->isArray($_for' . $cnt . '_from, true) || (is_numeric($_for' . $cnt . '_from) && (abs(($_for' . $cnt . '_from - $_for' . $cnt . '_to)/$_for' . $cnt . '_step) !== 0 || $_for' . $cnt . '_from == $_for' . $cnt . '_to));'; |
||
| 109 | // adds for properties |
||
| 110 | if ($usesAny) { |
||
| 111 | $out .= "\n" . '$this->globals["for"][' . $name . '] = array' . "\n("; |
||
| 112 | if ($usesIndex) { |
||
| 113 | $out .= "\n\t" . '"index" => 0,'; |
||
| 114 | } |
||
| 115 | if ($usesIteration) { |
||
| 116 | $out .= "\n\t" . '"iteration" => 1,'; |
||
| 117 | } |
||
| 118 | if ($usesFirst) { |
||
| 119 | $out .= "\n\t" . '"first" => null,'; |
||
| 120 | } |
||
| 121 | if ($usesLast) { |
||
| 122 | $out .= "\n\t" . '"last" => null,'; |
||
| 123 | } |
||
| 124 | if ($usesShow) { |
||
| 125 | $out .= "\n\t" . '"show" => $tmp_shows,'; |
||
| 126 | } |
||
| 127 | if ($usesTotal) { |
||
| 128 | $out .= "\n\t" . '"total" => $this->isArray($_for' . $cnt . '_from) ? floor($this->count($_for' . $cnt . '_from) / $_for' . $cnt . '_step) : (is_numeric($_for' . $cnt . '_from) ? abs(($_for' . $cnt . '_to + 1 - $_for' . $cnt . '_from)/$_for' . $cnt . '_step) : 0),'; |
||
| 129 | } |
||
| 130 | $out .= "\n);\n" . '$_for' . $cnt . '_glob =& $this->globals["for"][' . $name . '];'; |
||
| 131 | } |
||
| 132 | // checks if for must be looped |
||
| 133 | $out .= "\n" . 'if ($tmp_shows)' . "\n{"; |
||
| 134 | // set from/to to correct values if an array was given |
||
| 135 | $out .= "\n\t" . 'if ($this->isArray($_for' . $cnt . '_from' . (isset($params['hasElse']) ? ', true' : '') . ') == true) { |
||
| 136 | $_for' . $cnt . '_to = is_numeric($_for' . $cnt . '_to) ? $_for' . $cnt . '_to - $_for' . $cnt . '_step : $this->count($_for' . $cnt . '_from) - 1; |
||
| 137 | $_for' . $cnt . '_from = 0; |
||
| 138 | }'; |
||
| 139 | |||
| 140 | // if input are pure numbers it shouldn't reorder them, if it's variables it gets too messy though so in that case a counter should be used |
||
| 141 | $reverse = false; |
||
| 142 | $condition = '<='; |
||
| 143 | $incrementer = '+'; |
||
| 144 | |||
| 145 | if (preg_match('{^(["\']?)([0-9]+)\1$}', $from, $mN1) && preg_match('{^(["\']?)([0-9]+)\1$}', $to, $mN2)) { |
||
| 146 | $from = (int)$mN1[2]; |
||
| 147 | $to = (int)$mN2[2]; |
||
| 148 | if ($from > $to) { |
||
| 149 | $reverse = true; |
||
| 150 | $condition = '>='; |
||
| 151 | $incrementer = '-'; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | // reverse from and to if needed |
||
| 156 | if (!$reverse) { |
||
| 157 | $out .= "\n\t" . 'if ($_for' . $cnt . '_from > $_for' . $cnt . '_to) { |
||
| 158 | $tmp = $_for' . $cnt . '_from; |
||
| 159 | $_for' . $cnt . '_from = $_for' . $cnt . '_to; |
||
| 160 | $_for' . $cnt . '_to = $tmp; |
||
| 161 | }'; |
||
| 162 | } |
||
| 163 | |||
| 164 | $out .= "\n\t" . 'for ($this->scope[' . $name . '] = $_for' . $cnt . '_from; $this->scope[' . $name . '] ' . $condition . ' $_for' . $cnt . '_to; $this->scope[' . $name . '] ' . $incrementer . '= $_for' . $cnt . '_step)' . "\n\t{"; |
||
| 165 | // updates properties |
||
| 166 | if ($usesIndex) { |
||
| 167 | $out .= "\n\t\t" . '$_for' . $cnt . '_glob["index"] = $this->scope[' . $name . '];'; |
||
| 168 | } |
||
| 169 | if ($usesFirst) { |
||
| 170 | $out .= "\n\t\t" . '$_for' . $cnt . '_glob["first"] = (string) ($_for' . $cnt . '_glob["iteration"] === 1);'; |
||
| 171 | } |
||
| 172 | if ($usesLast) { |
||
| 173 | $out .= "\n\t\t" . '$_for' . $cnt . '_glob["last"] = (string) ($_for' . $cnt . '_glob["iteration"] === $_for' . $cnt . '_glob["total"]);'; |
||
| 174 | } |
||
| 175 | $out .= "\n/* -- for start output */\n" . Compiler::PHP_CLOSE; |
||
| 176 | |||
| 177 | // build post processing output and cache it |
||
| 178 | $postOut = Compiler::PHP_OPEN . '/* -- for end output */'; |
||
| 179 | // update properties |
||
| 180 | if ($usesIteration) { |
||
| 181 | $postOut .= "\n\t\t" . '$_for' . $cnt . '_glob["iteration"]+=1;'; |
||
| 182 | } |
||
| 183 | // end loop |
||
| 184 | $postOut .= "\n\t}\n}\n" . Compiler::PHP_CLOSE; |
||
| 185 | |||
| 186 | if (isset($params['hasElse'])) { |
||
| 187 | $postOut .= $params['hasElse']; |
||
| 188 | } |
||
| 189 | |||
| 190 | return $out . $content . $postOut; |
||
| 191 | } |
||
| 193 |