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 | 65 | 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 | 65 | public function select($component, $add = false) { |
|
93 | |||
94 | 59 | public function echoLiteral($value) { |
|
97 | |||
98 | 44 | public function echoCode($php) { |
|
101 | |||
102 | 65 | public function appendCode($php) { |
|
105 | |||
106 | 65 | public function indent($add) { |
|
109 | |||
110 | 20 | public function depth($add = 1) { |
|
113 | |||
114 | 21 | public function depthName($name, $add = 0) { |
|
117 | |||
118 | 65 | public function pushScope(array $vars) { |
|
121 | |||
122 | 58 | public function popScope() { |
|
125 | |||
126 | 57 | public function getScopeVariables() { |
|
129 | |||
130 | 58 | public function flush() { |
|
138 | |||
139 | private function flushJoin() { |
||
145 | |||
146 | 12 | private function flushArray() { |
|
183 | |||
184 | 4 | protected function px($add = 0) { |
|
187 | |||
188 | /** |
||
189 | * Get the style. |
||
190 | * |
||
191 | * @return string Returns the style. |
||
192 | */ |
||
193 | 58 | 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 | 65 | public function setBasename($basename) { |
|
227 | |||
228 | 65 | public function getNodeProp(\DOMNode $node, $name, $default = null) { |
|
234 | |||
235 | 33 | public function setNodeProp(\DOMNode $node = null, $name, $value) { |
|
247 | |||
248 | 12 | public function getIndent() { |
|
251 | |||
252 | 12 | public function getDepth() { |
|
255 | |||
256 | public function getScope() { |
||
259 | |||
260 | 12 | 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 | 7 | public function createCompilerException(\DOMNode $node, \Exception $ex, array $context = []) { |
|
310 | |||
311 | 3 | private function splitSyntaxError(SyntaxError $ex) { |
|
318 | |||
319 | /** |
||
320 | * Get the source. |
||
321 | * |
||
322 | * @return mixed Returns the source. |
||
323 | */ |
||
324 | 12 | public function getSource() { |
|
327 | |||
328 | /** |
||
329 | * Set the source. |
||
330 | * |
||
331 | * @param mixed $source |
||
332 | * @return $this |
||
333 | */ |
||
334 | 65 | public function setSource($source) { |
|
338 | |||
339 | /** |
||
340 | * Get the path. |
||
341 | * |
||
342 | * @return mixed Returns the path. |
||
343 | */ |
||
344 | 7 | public function getPath() { |
|
347 | |||
348 | /** |
||
349 | * Set the path. |
||
350 | * |
||
351 | * @param mixed $path |
||
352 | * @return $this |
||
353 | */ |
||
354 | 65 | public function setPath($path) { |
|
358 | |||
359 | /** |
||
360 | * Get the entire node property array. |
||
361 | * |
||
362 | * @return \SplObjectStorage Returns the node properties. |
||
363 | */ |
||
364 | 12 | 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.