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 | 57 | public function __construct($style = self::STYLE_JOIN, array $defaults = []) { |
|
58 | |||
59 | /** |
||
60 | * Select a specific component buffer. |
||
61 | * @param $component |
||
62 | */ |
||
63 | 57 | public function select($component) { |
|
64 | 57 | $previous = $this->currentName; |
|
65 | 57 | $this->currentName = $component; |
|
66 | |||
67 | 57 | if (!array_key_exists($component, $this->buffers)) { |
|
68 | 57 | $this->buffers[$component] = $buffer = new ComponentBuffer($this->defaults); |
|
69 | } |
||
70 | |||
71 | 57 | $this->current =& $this->buffers[$component]; |
|
72 | |||
73 | 57 | return $previous; |
|
74 | } |
||
75 | |||
76 | 53 | public function echoLiteral($value) { |
|
79 | |||
80 | 42 | public function echoCode($php) { |
|
83 | |||
84 | 57 | public function appendCode($php) { |
|
87 | |||
88 | 57 | public function indent($add) { |
|
91 | |||
92 | 18 | public function depth($add = 1) { |
|
95 | |||
96 | 18 | public function depthName($name, $add = 0) { |
|
99 | |||
100 | 57 | public function pushScope(array $vars) { |
|
103 | |||
104 | 54 | public function popScope() { |
|
107 | |||
108 | 52 | public function getScopeVariables() { |
|
111 | |||
112 | 54 | public function flush() { |
|
113 | 54 | switch ($this->getStyle()) { |
|
114 | 54 | case self::STYLE_ARRAY: |
|
115 | 11 | return $this->flushArray(); |
|
116 | default: |
||
117 | 54 | return $this->flushJoin(); |
|
118 | } |
||
119 | } |
||
120 | |||
121 | private function flushJoin() { |
||
127 | |||
128 | 11 | private function flushArray() { |
|
129 | 11 | $result = []; |
|
130 | |||
131 | 11 | foreach ($this->buffers as $name => $buffer) { |
|
132 | 11 | $flushed = $buffer->flush(); |
|
133 | 11 | if (empty($flushed)) { |
|
134 | 9 | continue; |
|
135 | } |
||
136 | |||
137 | 3 | if ($name === '') { |
|
138 | 3 | $result[] = $flushed; |
|
139 | } else { |
||
140 | 3 | $result[] = var_export($name, true).' => '.$flushed; |
|
141 | } |
||
142 | } |
||
143 | |||
144 | 11 | if (empty($result)) { |
|
145 | 9 | return '[]'; |
|
146 | } else { |
||
147 | 3 | return "[\n".implode(",\n\n", $result)."\n".$this->px().']'; |
|
148 | } |
||
149 | } |
||
150 | |||
151 | 3 | protected function px($add = 0) { |
|
154 | |||
155 | /** |
||
156 | * Get the style. |
||
157 | * |
||
158 | * @return string Returns the style. |
||
159 | */ |
||
160 | 54 | public function getStyle() { |
|
163 | |||
164 | /** |
||
165 | * Set the style. |
||
166 | * |
||
167 | * @param string $style One of the **STYLE_*** constants. |
||
168 | * @return $this |
||
169 | */ |
||
170 | public function setStyle($style) { |
||
174 | |||
175 | /** |
||
176 | * Get the basename. |
||
177 | * |
||
178 | * @return string Returns the basename. |
||
179 | */ |
||
180 | public function getBasename() { |
||
183 | |||
184 | /** |
||
185 | * Set the basename. |
||
186 | * |
||
187 | * @param string $basename |
||
188 | * @return $this |
||
189 | */ |
||
190 | 57 | public function setBasename($basename) { |
|
194 | |||
195 | 57 | public function getNodeProp(\DOMNode $node, $name, $default = null) { |
|
201 | |||
202 | 21 | public function setNodeProp(\DOMNode $node = null, $name, $value) { |
|
203 | 21 | if ($node === null) { |
|
204 | 17 | return $this; |
|
205 | } |
||
206 | |||
207 | 4 | if (!$this->nodeProps->contains($node)) { |
|
208 | 4 | $this->nodeProps->attach($node, [$name => $value]); |
|
209 | } |
||
210 | |||
211 | 4 | $this->nodeProps[$node] = [$name => $value] + $this->nodeProps[$node]; |
|
212 | 4 | return $this; |
|
213 | } |
||
214 | |||
215 | 11 | public function getIndent() { |
|
218 | |||
219 | 11 | public function getDepth() { |
|
222 | |||
223 | public function getScope() { |
||
226 | |||
227 | 11 | public function getAllScopes() { |
|
230 | |||
231 | /** |
||
232 | * Create a new **CompileException** with proper context. |
||
233 | * |
||
234 | * @param \DOMNode $node The node that has the error. |
||
235 | * @param \Exception $ex The exception that represents the low-level error. |
||
236 | * @param array $context Custom context information for the exception. |
||
237 | * @return CompileException Returns a new exception that can be thrown. |
||
238 | */ |
||
239 | 3 | public function createCompilerException(\DOMNode $node, \Exception $ex, array $context = []) { |
|
272 | |||
273 | 3 | private function splitSyntaxError(SyntaxError $ex) { |
|
280 | |||
281 | /** |
||
282 | * Get the source. |
||
283 | * |
||
284 | * @return mixed Returns the source. |
||
285 | */ |
||
286 | public function getSource() { |
||
289 | |||
290 | /** |
||
291 | * Set the source. |
||
292 | * |
||
293 | * @param mixed $source |
||
294 | * @return $this |
||
295 | */ |
||
296 | 57 | public function setSource($source) { |
|
300 | |||
301 | /** |
||
302 | * Get the path. |
||
303 | * |
||
304 | * @return mixed Returns the path. |
||
305 | */ |
||
306 | 3 | public function getPath() { |
|
309 | |||
310 | /** |
||
311 | * Set the path. |
||
312 | * |
||
313 | * @param mixed $path |
||
314 | * @return $this |
||
315 | */ |
||
316 | 57 | public function setPath($path) { |
|
320 | } |
||
321 |
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.