| Conditions | 5 |
| Paths | 16 |
| Total Lines | 63 |
| Code Lines | 33 |
| 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 |
||
| 137 | public function generate(Rendering $rendering) |
||
| 138 | { |
||
| 139 | // Copy some options to the serializer |
||
| 140 | $this->serializer->useMultibyteStringFunctions = $this->useMultibyteStringFunctions; |
||
| 141 | |||
| 142 | // Compile the templates to PHP |
||
| 143 | $compiledTemplates = array_map([$this, 'compileTemplate'], $rendering->getTemplates()); |
||
| 144 | |||
| 145 | // Start the code right after the class name, we'll prepend the header when we're done |
||
| 146 | $php = []; |
||
| 147 | $php[] = ' extends \\s9e\\TextFormatter\\Renderers\\PHP'; |
||
| 148 | $php[] = '{'; |
||
| 149 | $php[] = ' protected $params=' . self::export($rendering->getAllParameters()) . ';'; |
||
| 150 | $php[] = ' protected function renderNode(\\DOMNode $node)'; |
||
| 151 | $php[] = ' {'; |
||
| 152 | $php[] = ' ' . SwitchStatement::generate('$node->nodeName', $compiledTemplates, '$this->at($node);'); |
||
| 153 | $php[] = ' }'; |
||
| 154 | |||
| 155 | // Append the Quick renderer if applicable |
||
| 156 | if ($this->enableQuickRenderer) |
||
| 157 | { |
||
| 158 | $php[] = Quick::getSource($compiledTemplates); |
||
| 159 | } |
||
| 160 | |||
| 161 | // Close the class definition |
||
| 162 | $php[] = '}'; |
||
| 163 | |||
| 164 | // Assemble the source |
||
| 165 | $php = implode("\n", $php); |
||
| 166 | |||
| 167 | // Finally, optimize the control structures |
||
| 168 | if (isset($this->controlStructuresOptimizer)) |
||
| 169 | { |
||
| 170 | $php = $this->controlStructuresOptimizer->optimize($php); |
||
| 171 | } |
||
| 172 | |||
| 173 | // Generate a name for that class if necessary, and save it |
||
| 174 | $className = (isset($this->className)) |
||
| 175 | ? $this->className |
||
| 176 | : $this->defaultClassPrefix . sha1($php); |
||
| 177 | $this->lastClassName = $className; |
||
| 178 | |||
| 179 | // Prepare the header |
||
| 180 | $header = "\n" |
||
| 181 | . "/**\n" |
||
| 182 | . "* @package s9e\TextFormatter\n" |
||
| 183 | . "* @copyright Copyright (c) 2010-2017 The s9e Authors\n" |
||
| 184 | . "* @license http://www.opensource.org/licenses/mit-license.php The MIT License\n" |
||
| 185 | . "*/\n"; |
||
| 186 | |||
| 187 | // Declare the namespace and class name |
||
| 188 | $pos = strrpos($className, '\\'); |
||
| 189 | if ($pos !== false) |
||
| 190 | { |
||
| 191 | $header .= 'namespace ' . substr($className, 0, $pos) . ";\n\n"; |
||
| 192 | $className = substr($className, 1 + $pos); |
||
| 193 | } |
||
| 194 | |||
| 195 | // Prepend the header and the class name |
||
| 196 | $php = $header . 'class ' . $className . $php; |
||
| 197 | |||
| 198 | return $php; |
||
| 199 | } |
||
| 200 | |||
| 238 | } |