| Conditions | 51 |
| Paths | > 20000 |
| Total Lines | 171 |
| 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 |
||
| 88 | public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler) |
||
| 89 | { |
||
| 90 | $compiler->loopNesting++; |
||
| 91 | // init |
||
| 92 | $this->isNamed = false; |
||
| 93 | // check and get attributes |
||
| 94 | $_attr = $this->getAttributes($compiler, $args); |
||
| 95 | $from = $_attr[ 'from' ]; |
||
| 96 | $item = $compiler->getId($_attr[ 'item' ]); |
||
| 97 | if ($item === false) { |
||
| 98 | $item = $compiler->getVariableName($_attr[ 'item' ]); |
||
| 99 | } |
||
| 100 | $key = $name = null; |
||
| 101 | $attributes = array('item' => $item); |
||
| 102 | if (isset($_attr[ 'key' ])) { |
||
| 103 | $key = $compiler->getId($_attr[ 'key' ]); |
||
| 104 | if ($key === false) { |
||
| 105 | $key = $compiler->getVariableName($_attr[ 'key' ]); |
||
| 106 | } |
||
| 107 | $attributes[ 'key' ] = $key; |
||
| 108 | } |
||
| 109 | if (isset($_attr[ 'name' ])) { |
||
| 110 | $this->isNamed = true; |
||
| 111 | $name = $attributes[ 'name' ] = $compiler->getId($_attr[ 'name' ]); |
||
| 112 | } |
||
| 113 | foreach ($attributes as $a => $v) { |
||
| 114 | if ($v === false) { |
||
| 115 | $compiler->trigger_template_error("'{$a}' attribute/variable has illegal value", null, true); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | $fromName = $compiler->getVariableName($_attr[ 'from' ]); |
||
| 119 | if ($fromName) { |
||
|
|
|||
| 120 | foreach (array('item', 'key') as $a) { |
||
| 121 | if (isset($attributes[ $a ]) && $attributes[ $a ] === $fromName) { |
||
| 122 | $compiler->trigger_template_error( |
||
| 123 | "'{$a}' and 'from' may not have same variable name '{$fromName}'", |
||
| 124 | null, |
||
| 125 | true |
||
| 126 | ); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | $itemVar = "\$_smarty_tpl->tpl_vars['{$item}']"; |
||
| 131 | $local = '$__foreach_' . $attributes[ 'item' ] . '_' . $this->counter++ . '_'; |
||
| 132 | // search for used tag attributes |
||
| 133 | $itemAttr = array(); |
||
| 134 | $namedAttr = array(); |
||
| 135 | $this->scanForProperties($attributes, $compiler); |
||
| 136 | if (!empty($this->matchResults[ 'item' ])) { |
||
| 137 | $itemAttr = $this->matchResults[ 'item' ]; |
||
| 138 | } |
||
| 139 | if (!empty($this->matchResults[ 'named' ])) { |
||
| 140 | $namedAttr = $this->matchResults[ 'named' ]; |
||
| 141 | } |
||
| 142 | if (isset($_attr[ 'properties' ]) && preg_match_all('/[\'](.*?)[\']/', $_attr[ 'properties' ], $match)) { |
||
| 143 | foreach ($match[ 1 ] as $prop) { |
||
| 144 | if (in_array($prop, $this->itemProperties)) { |
||
| 145 | $itemAttr[ $prop ] = true; |
||
| 146 | } else { |
||
| 147 | $compiler->trigger_template_error("Invalid property '{$prop}'", null, true); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | if ($this->isNamed) { |
||
| 151 | foreach ($match[ 1 ] as $prop) { |
||
| 152 | if (in_array($prop, $this->nameProperties)) { |
||
| 153 | $nameAttr[ $prop ] = true; |
||
| 154 | } else { |
||
| 155 | $compiler->trigger_template_error("Invalid property '{$prop}'", null, true); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | if (isset($itemAttr[ 'first' ])) { |
||
| 161 | $itemAttr[ 'index' ] = true; |
||
| 162 | } |
||
| 163 | if (isset($namedAttr[ 'first' ])) { |
||
| 164 | $namedAttr[ 'index' ] = true; |
||
| 165 | } |
||
| 166 | if (isset($namedAttr[ 'last' ])) { |
||
| 167 | $namedAttr[ 'iteration' ] = true; |
||
| 168 | $namedAttr[ 'total' ] = true; |
||
| 169 | } |
||
| 170 | if (isset($itemAttr[ 'last' ])) { |
||
| 171 | $itemAttr[ 'iteration' ] = true; |
||
| 172 | $itemAttr[ 'total' ] = true; |
||
| 173 | } |
||
| 174 | if (isset($namedAttr[ 'show' ])) { |
||
| 175 | $namedAttr[ 'total' ] = true; |
||
| 176 | } |
||
| 177 | if (isset($itemAttr[ 'show' ])) { |
||
| 178 | $itemAttr[ 'total' ] = true; |
||
| 179 | } |
||
| 180 | $keyTerm = ''; |
||
| 181 | if (isset($attributes[ 'key' ])) { |
||
| 182 | $keyTerm = "\$_smarty_tpl->tpl_vars['{$key}']->value => "; |
||
| 183 | } |
||
| 184 | if (isset($itemAttr[ 'key' ])) { |
||
| 185 | $keyTerm = "{$itemVar}->key => "; |
||
| 186 | } |
||
| 187 | if ($this->isNamed) { |
||
| 188 | $foreachVar = "\$_smarty_tpl->tpl_vars['__smarty_foreach_{$attributes['name']}']"; |
||
| 189 | } |
||
| 190 | $needTotal = isset($itemAttr[ 'total' ]); |
||
| 191 | // Register tag |
||
| 192 | $this->openTag( |
||
| 193 | $compiler, |
||
| 194 | 'foreach', |
||
| 195 | array('foreach', $compiler->nocache, $local, $itemVar, empty($itemAttr) ? 1 : 2) |
||
| 196 | ); |
||
| 197 | // maybe nocache because of nocache variables |
||
| 198 | $compiler->nocache = $compiler->nocache | $compiler->tag_nocache; |
||
| 199 | // generate output code |
||
| 200 | $output = "<?php\n"; |
||
| 201 | $output .= "\$_from = \$_smarty_tpl->smarty->ext->_foreach->init(\$_smarty_tpl, $from, " . |
||
| 202 | var_export($item, true); |
||
| 203 | if ($name || $needTotal || $key) { |
||
| 204 | $output .= ', ' . var_export($needTotal, true); |
||
| 205 | } |
||
| 206 | if ($name || $key) { |
||
| 207 | $output .= ', ' . var_export($key, true); |
||
| 208 | } |
||
| 209 | if ($name) { |
||
| 210 | $output .= ', ' . var_export($name, true) . ', ' . var_export($namedAttr, true); |
||
| 211 | } |
||
| 212 | $output .= ");\n"; |
||
| 213 | if (isset($itemAttr[ 'show' ])) { |
||
| 214 | $output .= "{$itemVar}->show = ({$itemVar}->total > 0);\n"; |
||
| 215 | } |
||
| 216 | if (isset($itemAttr[ 'iteration' ])) { |
||
| 217 | $output .= "{$itemVar}->iteration = 0;\n"; |
||
| 218 | } |
||
| 219 | if (isset($itemAttr[ 'index' ])) { |
||
| 220 | $output .= "{$itemVar}->index = -1;\n"; |
||
| 221 | } |
||
| 222 | $output .= "if (\$_from !== null) {\n"; |
||
| 223 | $output .= "foreach (\$_from as {$keyTerm}{$itemVar}->value) {\n"; |
||
| 224 | if (isset($attributes[ 'key' ]) && isset($itemAttr[ 'key' ])) { |
||
| 225 | $output .= "\$_smarty_tpl->tpl_vars['{$key}']->value = {$itemVar}->key;\n"; |
||
| 226 | } |
||
| 227 | if (isset($itemAttr[ 'iteration' ])) { |
||
| 228 | $output .= "{$itemVar}->iteration++;\n"; |
||
| 229 | } |
||
| 230 | if (isset($itemAttr[ 'index' ])) { |
||
| 231 | $output .= "{$itemVar}->index++;\n"; |
||
| 232 | } |
||
| 233 | if (isset($itemAttr[ 'first' ])) { |
||
| 234 | $output .= "{$itemVar}->first = !{$itemVar}->index;\n"; |
||
| 235 | } |
||
| 236 | if (isset($itemAttr[ 'last' ])) { |
||
| 237 | $output .= "{$itemVar}->last = {$itemVar}->iteration === {$itemVar}->total;\n"; |
||
| 238 | } |
||
| 239 | if (isset($foreachVar)) { |
||
| 240 | if (isset($namedAttr[ 'iteration' ])) { |
||
| 241 | $output .= "{$foreachVar}->value['iteration']++;\n"; |
||
| 242 | } |
||
| 243 | if (isset($namedAttr[ 'index' ])) { |
||
| 244 | $output .= "{$foreachVar}->value['index']++;\n"; |
||
| 245 | } |
||
| 246 | if (isset($namedAttr[ 'first' ])) { |
||
| 247 | $output .= "{$foreachVar}->value['first'] = !{$foreachVar}->value['index'];\n"; |
||
| 248 | } |
||
| 249 | if (isset($namedAttr[ 'last' ])) { |
||
| 250 | $output .= "{$foreachVar}->value['last'] = {$foreachVar}->value['iteration'] === {$foreachVar}->value['total'];\n"; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | if (!empty($itemAttr)) { |
||
| 254 | $output .= "{$local}saved = {$itemVar};\n"; |
||
| 255 | } |
||
| 256 | $output .= '?>'; |
||
| 257 | return $output; |
||
| 258 | } |
||
| 259 | |||
| 346 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: