Complex classes like CompilerBuffer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CompilerBuffer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class CompilerBuffer { |
||
| 14 | const STYLE_JOIN = 'join'; |
||
| 15 | const STYLE_ARRAY = 'array'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var ComponentBuffer[] |
||
| 19 | */ |
||
| 20 | private $buffers; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var ComponentBuffer |
||
| 24 | */ |
||
| 25 | private $current; |
||
| 26 | |||
| 27 | private $currentName; |
||
| 28 | |||
| 29 | private $basename; |
||
| 30 | |||
| 31 | private $style = self::STYLE_JOIN; |
||
| 32 | |||
| 33 | private $source; |
||
| 34 | |||
| 35 | private $path; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | private $defaults; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var \SplObjectStorage |
||
| 44 | */ |
||
| 45 | private $nodeProps; |
||
| 46 | |||
| 47 | 78 | public function __construct($style = self::STYLE_JOIN, array $defaults = []) { |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Select a specific component buffer. |
||
| 61 | * @param string $component The name of the component to select. |
||
| 62 | * @param bool $add Whether to add a new component if there is already a compile buffer with the same name. |
||
| 63 | */ |
||
| 64 | 78 | public function select($component, $add = false) { |
|
| 65 | 78 | $previous = $this->currentName; |
|
| 66 | 78 | $this->currentName = $component; |
|
| 67 | |||
| 68 | 78 | if (!array_key_exists($component, $this->buffers)) { |
|
| 69 | 78 | $this->buffers[$component] = $buffer = new ComponentBuffer($this->defaults); |
|
| 70 | 78 | } elseif ($add) { |
|
| 71 | 1 | if (is_array($this->buffers[$component])) { |
|
| 72 | $this->buffers[$component][] = $buffer = new ComponentBuffer($this->defaults); |
||
| 73 | } else { |
||
| 74 | 1 | $this->buffers[$component] = [ |
|
| 75 | 1 | $this->buffers[$component], |
|
| 76 | 1 | $buffer = new ComponentBuffer($this->defaults) |
|
| 77 | 1 | ]; |
|
| 78 | } |
||
| 79 | 1 | } else { |
|
| 80 | 10 | $buffer = $this->buffers[$component]; |
|
| 81 | 10 | if (is_array($buffer)) { |
|
| 82 | $buffer = end($buffer); |
||
| 83 | if ($previous === $component) { |
||
| 84 | $buffer = prev($buffer); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | 78 | $this->current = $buffer; |
|
| 90 | |||
| 91 | 78 | return $previous; |
|
| 92 | } |
||
| 93 | |||
| 94 | 71 | public function echoLiteral($value) { |
|
| 97 | |||
| 98 | 56 | public function echoCode($php) { |
|
| 101 | |||
| 102 | 78 | public function appendCode($php) { |
|
| 105 | |||
| 106 | 78 | public function indent($add) { |
|
| 109 | |||
| 110 | 26 | public function depth($add = 1) { |
|
| 113 | |||
| 114 | 27 | public function depthName($name, $add = 0) { |
|
| 117 | |||
| 118 | 78 | public function pushScope(array $vars) { |
|
| 121 | |||
| 122 | 70 | public function popScope() { |
|
| 125 | |||
| 126 | 70 | public function getScopeVariables() { |
|
| 129 | |||
| 130 | 70 | public function flush() { |
|
| 131 | 70 | switch ($this->getStyle()) { |
|
| 132 | 70 | case self::STYLE_ARRAY: |
|
| 133 | 13 | return $this->flushArray(); |
|
| 134 | 70 | default: |
|
| 135 | 70 | return $this->flushJoin(); |
|
| 136 | 70 | } |
|
| 137 | } |
||
| 138 | |||
| 139 | private function flushJoin() { |
||
| 145 | |||
| 146 | 13 | private function flushArray() { |
|
| 147 | 13 | $result = []; |
|
| 148 | |||
| 149 | 13 | foreach ($this->buffers as $name => $buffers) { |
|
| 150 | 13 | $flushed = []; |
|
| 151 | 13 | if (is_array($buffers)) { |
|
| 152 | 1 | foreach ($buffers as $buffer) { |
|
| 153 | 1 | $flushed[] = $buffer->flush(); |
|
| 154 | 1 | } |
|
| 155 | 1 | } else { |
|
| 156 | 13 | $flushed = [$buffers->flush()]; |
|
| 157 | } |
||
| 158 | |||
| 159 | 13 | $flushed = array_filter($flushed); |
|
| 160 | 13 | if (empty($flushed)) { |
|
| 161 | 10 | continue; |
|
| 162 | } |
||
| 163 | |||
| 164 | 4 | if (count($flushed) === 1) { |
|
| 165 | 4 | $children = reset($flushed); |
|
| 166 | 4 | } else { |
|
| 167 | 1 | $children = "[\n".implode(",\n", $flushed)."\n".$this->px(+1).']'; |
|
| 168 | } |
||
| 169 | |||
| 170 | 4 | if ($name === '') { |
|
| 171 | 4 | $result[] = $children; |
|
| 172 | 4 | } else { |
|
| 173 | 2 | $result[] = var_export($name, true).' => '.ltrim($children); |
|
| 174 | } |
||
| 175 | 13 | } |
|
| 176 | |||
| 177 | 13 | if (empty($result)) { |
|
| 178 | 10 | return '[]'; |
|
| 179 | } else { |
||
| 180 | 4 | return "[\n".implode(",\n\n".$this->px(+1), $result)."\n".$this->px().']'; |
|
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | 4 | protected function px($add = 0) { |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Get the style. |
||
| 190 | * |
||
| 191 | * @return string Returns the style. |
||
| 192 | */ |
||
| 193 | 70 | public function getStyle() { |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Set the style. |
||
| 199 | * |
||
| 200 | * @param string $style One of the **STYLE_*** constants. |
||
| 201 | * @return $this |
||
| 202 | */ |
||
| 203 | public function setStyle($style) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get the basename. |
||
| 210 | * |
||
| 211 | * @return string Returns the basename. |
||
| 212 | */ |
||
| 213 | public function getBasename() { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Set the basename. |
||
| 219 | * |
||
| 220 | * @param string $basename |
||
| 221 | * @return $this |
||
| 222 | */ |
||
| 223 | 78 | public function setBasename($basename) { |
|
| 227 | |||
| 228 | 78 | public function getNodeProp(\DOMNode $node, $name, $default = null) { |
|
| 234 | |||
| 235 | 40 | public function setNodeProp(\DOMNode $node = null, $name, $value) { |
|
| 236 | 40 | if ($node === null) { |
|
| 237 | 21 | return $this; |
|
| 238 | } |
||
| 239 | |||
| 240 | 23 | if (!$this->nodeProps->contains($node)) { |
|
| 241 | 23 | $this->nodeProps->attach($node, [$name => $value]); |
|
| 242 | 23 | } |
|
| 243 | |||
| 244 | 23 | $this->nodeProps[$node] = [$name => $value] + $this->nodeProps[$node]; |
|
| 245 | 23 | return $this; |
|
| 246 | } |
||
| 247 | |||
| 248 | 14 | public function getIndent() { |
|
| 251 | |||
| 252 | 14 | public function getDepth() { |
|
| 255 | |||
| 256 | public function getScope() { |
||
| 259 | |||
| 260 | 14 | public function getAllScopes() { |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Create a new **CompileException** with proper context. |
||
| 266 | * |
||
| 267 | * @param \DOMNode $node The node that has the error. |
||
| 268 | * @param \Exception $ex The exception that represents the low-level error. |
||
| 269 | * @param array $context Custom context information for the exception. |
||
| 270 | * @return CompileException Returns a new exception that can be thrown. |
||
| 271 | */ |
||
| 272 | 8 | public function createCompilerException(\DOMNode $node, \Exception $ex, array $context = []) { |
|
| 273 | $result = $context + [ |
||
| 274 | 8 | 'path' => $this->getPath(), |
|
| 275 | 8 | 'source' => '', |
|
| 276 | 8 | 'sourcePosition' => null, |
|
| 277 | 8 | 'line' => $node->getLineNo(), |
|
| 278 | 8 | 'lines' => [] |
|
| 279 | 8 | ]; |
|
| 280 | 8 | $message = $ex->getMessage(); |
|
| 281 | |||
| 282 | 8 | if ($ex instanceof SyntaxError) { |
|
| 283 | 3 | list($error, $position) = $this->splitSyntaxError($ex); |
|
| 284 | 3 | $result['source'] = $result['source'] ?: ($node instanceof \DOMAttr ? $node->value : $node->nodeValue); |
|
| 285 | 3 | if (!isset($context['sourcePosition'])) { |
|
| 286 | 3 | $result['sourcePosition'] = $position; |
|
| 287 | 3 | } |
|
| 288 | 8 | } elseif (empty($result['source'])) { |
|
| 289 | 5 | if ($node instanceof \DOMAttr) { |
|
| 290 | 3 | $result['source'] = $node->name.'="'.$node->value.'"'; |
|
| 291 | 3 | } |
|
| 292 | 5 | } |
|
| 293 | |||
| 294 | 8 | if (!empty($this->source)) { |
|
| 295 | 8 | $allLines = explode("\n", $this->source); |
|
| 296 | |||
| 297 | 8 | $lines = []; |
|
| 298 | 8 | $line = $result['line']; |
|
| 299 | 8 | for ($i = max(0, $line - 4); $i < $line + 3; $i++) { |
|
| 300 | 8 | if (isset($allLines[$i])) { |
|
| 301 | 8 | $lines[$i + 1] = $allLines[$i]; |
|
| 302 | 8 | } |
|
| 303 | 8 | } |
|
| 304 | |||
| 305 | 8 | $result['lines'] = $lines; |
|
| 306 | 8 | } |
|
| 307 | |||
| 308 | 8 | return new CompileException($message, $result, $ex); |
|
| 309 | } |
||
| 310 | |||
| 311 | 3 | private function splitSyntaxError(SyntaxError $ex) { |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Get the source. |
||
| 321 | * |
||
| 322 | * @return mixed Returns the source. |
||
| 323 | */ |
||
| 324 | 14 | public function getSource() { |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Set the source. |
||
| 330 | * |
||
| 331 | * @param mixed $source |
||
| 332 | * @return $this |
||
| 333 | */ |
||
| 334 | 78 | public function setSource($source) { |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Get the path. |
||
| 341 | * |
||
| 342 | * @return mixed Returns the path. |
||
| 343 | */ |
||
| 344 | 8 | public function getPath() { |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Set the path. |
||
| 350 | * |
||
| 351 | * @param mixed $path |
||
| 352 | * @return $this |
||
| 353 | */ |
||
| 354 | 78 | public function setPath($path) { |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Get the entire node property array. |
||
| 361 | * |
||
| 362 | * @return \SplObjectStorage Returns the node properties. |
||
| 363 | */ |
||
| 364 | 14 | public function getNodePropArray() { |
|
| 367 | } |
||
| 368 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.