| Conditions | 13 |
| Paths | 1536 |
| Total Lines | 121 |
| Lines | 21 |
| Ratio | 17.36 % |
| 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 |
||
| 99 | public function compile(\Twig\Compiler $compiler) |
||
| 100 | { |
||
| 101 | $i = self::$count++; |
||
| 102 | |||
| 103 | $collectionTypeName = $this->getNode('collectionType')->getNode(0)->getAttribute('name'); |
||
| 104 | |||
| 105 | $compiler |
||
| 106 | ->addDebugInfo($this); |
||
| 107 | |||
| 108 | if ($this->hasNode('collectionFilters')) { |
||
| 109 | $compiler->write("\$context['_collection_type_filters'] = [];\n"); |
||
| 110 | $compiler->write("\$context['".$collectionTypeName."'] = null;\n"); |
||
| 111 | $compiler->write("\$context['_collection_type_filters'] = ")->subcompile($this->getNode('collectionFilters'))->raw("['_collection_type_filters']; unset(\$context['".$collectionTypeName."']['_collection_type_filters']);\n"); |
||
| 112 | |||
| 113 | View Code Duplication | if ($this->hasNode('withParameters')) { |
|
| 114 | $compiler->write('$withParameters = array_merge(')->subcompile($this->getNode('withParameters'))->raw(", \$context['_collection_type_filters']);\n"); |
||
| 115 | } else { |
||
| 116 | $compiler->write("\$withParameters = \$context['_collection_type_filters'];\n"); |
||
| 117 | } |
||
| 118 | View Code Duplication | } else { |
|
| 119 | if ($this->hasNode('withParameters')) { |
||
| 120 | $compiler->raw('$withParameters = ')->subcompile($this->getNode('withParameters'))->raw(";\n"); |
||
| 121 | } else { |
||
| 122 | $compiler->raw("\$withParameters = [];\n"); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | View Code Duplication | if ($this->hasNode('withoutParameters')) { |
|
| 127 | $compiler->raw('$withoutParameters = ')->subcompile($this->getNode('withoutParameters'))->raw(";\n"); |
||
| 128 | } else { |
||
| 129 | $compiler->raw("\$withoutParameters = [];\n"); |
||
| 130 | } |
||
| 131 | |||
| 132 | $compiler->write('$swpCollectionMetaLoader'.$i." = \$this->env->getExtension('SWP\Component\TemplatesSystem\Twig\Extension\GimmeExtension')->getLoader();\n"); |
||
| 133 | View Code Duplication | if ($this->hasNode('ignoreContext')) { |
|
| 134 | $compiler->write('$swpContext'.$i."GimmeList = \$this->env->getExtension('SWP\Component\TemplatesSystem\Twig\Extension\GimmeExtension')->getContext();\n"); |
||
| 135 | $compiler->write('$swpIgnoreContext'.$i.'GimmeList = $swpContext'.$i.'GimmeList->temporaryUnset(')->subcompile($this->getNode('ignoreContext'))->raw(");\n"); |
||
| 136 | } |
||
| 137 | $compiler->write('')->subcompile($this->getNode('collectionType'))->raw(' = twig_ensure_traversable($swpCollectionMetaLoader'.$i.'->load("')->raw($collectionTypeName)->raw('", '); |
||
| 138 | $compiler->raw('$withParameters, $withoutParameters, \SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface::COLLECTION));')->raw("\n"); |
||
| 139 | |||
| 140 | // the (array) cast bypasses a PHP 5.2.6 bug |
||
| 141 | $compiler->write("\$context['_parent'] = (array) \$context;\n"); |
||
| 142 | |||
| 143 | if ($this->hasNode('else')) { |
||
| 144 | $compiler->write("\$context['_iterated'] = false;\n"); |
||
| 145 | } |
||
| 146 | |||
| 147 | if ($this->getAttribute('with_loop')) { |
||
| 148 | $compiler |
||
| 149 | ->write("\$context['loop'] = array(\n") |
||
| 150 | ->write(" 'parent' => \$context['_parent'],\n") |
||
| 151 | ->write(" 'index0' => 0,\n") |
||
| 152 | ->write(" 'index' => 1,\n") |
||
| 153 | ->write(" 'first' => true,\n") |
||
| 154 | ->write(");\n"); |
||
| 155 | |||
| 156 | if (!$this->getAttribute('ifexpr')) { |
||
| 157 | $compiler |
||
| 158 | ->write('if (is_array(')->subcompile($this->getNode('collectionType'))->raw(') || (is_object(')->subcompile($this->getNode('collectionType'))->raw(') && ')->subcompile($this->getNode('collectionType'))->raw(" instanceof Countable)) {\n") |
||
| 159 | ->indent() |
||
| 160 | ->write('$length = count(')->subcompile($this->getNode('collectionType'))->raw(");\n") |
||
| 161 | ->write("\$context['loop']['revindex0'] = \$length - 1;\n") |
||
| 162 | ->write("\$context['loop']['revindex'] = \$length;\n") |
||
| 163 | ->write("\$context['loop']['length'] = \$length;\n") |
||
| 164 | ->write("\$context['loop']['totalLength'] = \$length;\n") |
||
| 165 | ->write("\$context['loop']['last'] = 1 === \$length;\n") |
||
| 166 | ->outdent() |
||
| 167 | ->write("}\n"); |
||
| 168 | |||
| 169 | $compiler |
||
| 170 | ->write('if(is_object(')->subcompile($this->getNode('collectionType'))->raw(') && ')->subcompile($this->getNode('collectionType'))->raw(" instanceof \SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection) {\n") |
||
| 171 | ->indent() |
||
| 172 | ->write('$context[\'loop\'][\'totalLength\'] = ')->subcompile($this->getNode('collectionType'))->raw("->getTotalItemsCount();\n") |
||
| 173 | ->outdent() |
||
| 174 | ->write("}\n"); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | $this->loop->setAttribute('else', $this->hasNode('else')); |
||
| 179 | $this->loop->setAttribute('with_loop', $this->getAttribute('with_loop')); |
||
| 180 | $this->loop->setAttribute('ifexpr', $this->getAttribute('ifexpr')); |
||
| 181 | |||
| 182 | if (null !== $this->getNode('collectionType')) { |
||
| 183 | $compiler |
||
| 184 | ->write('foreach (') |
||
| 185 | ->subcompile($this->getNode('collectionType')) |
||
| 186 | ->raw(' as $_key') |
||
| 187 | ->raw(' => ') |
||
| 188 | ->subcompile($this->getNode('variable')) |
||
| 189 | ->raw(") {\n") |
||
| 190 | ->indent() |
||
| 191 | ->subcompile($this->getNode('body')) |
||
| 192 | ->outdent() |
||
| 193 | ->write("}\n"); |
||
| 194 | } |
||
| 195 | |||
| 196 | if ($this->hasNode('else')) { |
||
| 197 | $compiler |
||
| 198 | ->write("if (!\$context['_iterated']) {\n") |
||
| 199 | ->indent() |
||
| 200 | ->subcompile($this->getNode('else')) |
||
| 201 | ->outdent() |
||
| 202 | ->write("}\n"); |
||
| 203 | } |
||
| 204 | |||
| 205 | if ($this->hasNode('ignoreContext')) { |
||
| 206 | $compiler->write('$swpContext'.$i.'GimmeList->restoreTemporaryUnset($swpIgnoreContext'.$i."GimmeList);\n"); |
||
| 207 | } |
||
| 208 | $compiler->write("\$_parent = \$context['_parent'];\n"); |
||
| 209 | |||
| 210 | // remove some "private" loop variables (needed for nested loops) |
||
| 211 | $compiler->write('unset($context[\''.$this->getNode('variable')->getNode(0)->getAttribute('name').'\'], $context[\'_iterated\'], $context[\''.$collectionTypeName.'\'], $context[\'_parent\'], $context[\'loop\']);'."\n"); |
||
| 212 | |||
| 213 | if ($this->hasNode('collectionFilters')) { |
||
| 214 | $compiler->write("unset(\$context['_collection_type_filters']);\n"); |
||
| 215 | } |
||
| 216 | |||
| 217 | // keep the values set in the inner context for variables defined in the outer context |
||
| 218 | $compiler->write("\$context = array_intersect_key(\$context, \$_parent) + \$_parent;\n"); |
||
| 219 | } |
||
| 220 | } |
||
| 221 |