| Conditions | 17 |
| Paths | > 20000 |
| Total Lines | 118 |
| Code Lines | 89 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 40 | public function compile(Compiler $compiler) |
||
| 41 | { |
||
| 42 | $n = self::$_cacheCount++; |
||
| 43 | |||
| 44 | $flags = $this->hasNode('flags') ? $this->getNode('flags') : null; |
||
| 45 | |||
| 46 | $conditions = $this->hasNode('conditions') ? $this->getNode('conditions') : null; |
||
| 47 | $ignoreConditions = $this->hasNode('ignoreConditions') ? $this->getNode('ignoreConditions') : null; |
||
| 48 | $key = $this->hasNode('key') ? $this->getNode('key') : null; |
||
| 49 | $expiration = $this->hasNode('expiration') ? $this->getNode('expiration') : null; |
||
| 50 | |||
| 51 | $durationNum = $this->getAttribute('durationNum'); |
||
| 52 | $durationUnit = $this->getAttribute('durationUnit'); |
||
| 53 | $global = $this->getAttribute('global') ? 'true' : 'false'; |
||
| 54 | $elements = $this->getAttribute('elements') ? 'true' : 'false'; |
||
| 55 | |||
| 56 | $compiler |
||
| 57 | ->addDebugInfo($this) |
||
| 58 | ->write('$cacheService = ' . CacheFlag::class . "::getInstance()->templateCaches;\n") |
||
| 59 | ->write('$request = ' . Craft::class . "::\$app->getRequest();\n") |
||
| 60 | ->write("\$ignoreCache{$n} = (\$request->getIsLivePreview() || \$request->getToken()"); |
||
| 61 | |||
| 62 | if ($conditions) { |
||
| 63 | $compiler |
||
| 64 | ->raw(' || !(') |
||
| 65 | ->subcompile($conditions) |
||
| 66 | ->raw(')'); |
||
| 67 | } else if ($ignoreConditions) { |
||
| 68 | $compiler |
||
| 69 | ->raw(' || (') |
||
| 70 | ->subcompile($ignoreConditions) |
||
| 71 | ->raw(')'); |
||
| 72 | } |
||
| 73 | |||
| 74 | $compiler |
||
| 75 | ->raw(");\n") |
||
| 76 | ->write("if (!\$ignoreCache{$n}) {\n") |
||
| 77 | ->indent() |
||
| 78 | ->write("\$cacheKey{$n} = "); |
||
| 79 | |||
| 80 | if ($key) { |
||
| 81 | $compiler->subcompile($key); |
||
| 82 | } else { |
||
| 83 | $compiler->raw('"' . StringHelper::randomString() . '"'); |
||
| 84 | } |
||
| 85 | |||
| 86 | $compiler |
||
| 87 | ->raw(";\n") |
||
| 88 | ->write("\$cacheBody{$n} = \$cacheService->getTemplateCache(\$cacheKey{$n}, "); |
||
| 89 | |||
| 90 | if ($flags) { |
||
| 91 | $compiler->subcompile($flags); |
||
| 92 | } else { |
||
| 93 | $compiler->raw('null'); |
||
| 94 | } |
||
| 95 | |||
| 96 | $compiler |
||
| 97 | ->raw(", {$elements}, {$global});\n") |
||
| 98 | ->outdent() |
||
| 99 | ->write("} else {\n") |
||
| 100 | ->indent() |
||
| 101 | ->write("\$cacheBody{$n} = null;\n") |
||
| 102 | ->outdent() |
||
| 103 | ->write("}\n") |
||
| 104 | ->write("if (\$cacheBody{$n} === null) {\n") |
||
| 105 | ->indent() |
||
| 106 | ->write("if (!\$ignoreCache{$n}) {\n") |
||
| 107 | ->indent() |
||
| 108 | ->write("\$cacheService->startTemplateCache($global);\n") |
||
| 109 | ->outdent() |
||
| 110 | ->write("}\n") |
||
| 111 | ->write("ob_start();\n") |
||
| 112 | ->subcompile($this->getNode('body')) |
||
| 113 | ->write("\$cacheBody{$n} = ob_get_clean();\n") |
||
| 114 | ->write("if (!\$ignoreCache{$n}) {\n") |
||
| 115 | ->indent() |
||
| 116 | ->write("\$cacheService->endTemplateCache(\$cacheKey{$n}, "); |
||
| 117 | |||
| 118 | if ($flags) { |
||
| 119 | $compiler->subcompile($flags); |
||
| 120 | } else { |
||
| 121 | $compiler->raw('null'); |
||
| 122 | } |
||
| 123 | |||
| 124 | $compiler->raw(", {$global}, "); |
||
| 125 | |||
| 126 | if ($durationNum) { |
||
| 127 | // So silly that PHP doesn't support "+1 week" http://www.php.net/manual/en/datetime.formats.relative.php |
||
| 128 | |||
| 129 | if ($durationUnit === 'week') { |
||
| 130 | if ($durationNum == 1) { |
||
| 131 | $durationNum = 7; |
||
| 132 | $durationUnit = 'days'; |
||
| 133 | } else { |
||
| 134 | $durationUnit = 'weeks'; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | $compiler->raw("'+{$durationNum} {$durationUnit}'"); |
||
| 139 | } else { |
||
| 140 | $compiler->raw('null'); |
||
| 141 | } |
||
| 142 | |||
| 143 | $compiler->raw(', '); |
||
| 144 | |||
| 145 | if ($expiration) { |
||
| 146 | $compiler->subcompile($expiration); |
||
| 147 | } else { |
||
| 148 | $compiler->raw('null'); |
||
| 149 | } |
||
| 150 | |||
| 151 | $compiler |
||
| 152 | ->raw(", \$cacheBody{$n});\n") |
||
| 153 | ->outdent() |
||
| 154 | ->write("}\n") |
||
| 155 | ->outdent() |
||
| 156 | ->write("}\n") |
||
| 157 | ->write("echo \$cacheBody{$n};\n"); |
||
| 158 | } |
||
| 161 |