Complex classes like Element 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 Element, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class Element { |
||
45 | const ELEMENT_TERM = 0; |
||
46 | const ELEMENT_DEFINITION = 1; |
||
47 | const ELEMENT_SOURCE = 2; |
||
48 | const ELEMENT_LINK = 3; |
||
49 | const ELEMENT_STYLE = 4; |
||
50 | |||
51 | const ELEMENT_FIELDCOUNT = 5; // number of fields stored for each element; (last field's index) + 1 |
||
52 | |||
53 | const LINK_TEMPLATE_ID = 'LingoLink'; |
||
54 | |||
55 | private $formattedTerm = null; |
||
56 | private $formattedDefinitions = null; |
||
57 | |||
58 | private $mDefinitions = []; |
||
59 | private $mTerm = null; |
||
60 | |||
61 | private $hasBeenDisplayed = false; |
||
62 | |||
63 | /** |
||
64 | 1 | * Lingo\Element constructor. |
|
65 | * |
||
66 | 1 | * @param $term |
|
67 | * @param $definition |
||
68 | 1 | */ |
|
69 | 1 | public function __construct( &$term, &$definition = null ) { |
|
77 | |||
78 | /** |
||
79 | * @param array $definition |
||
80 | */ |
||
81 | public function addDefinition( &$definition ) { |
||
84 | |||
85 | /** |
||
86 | * @param DOMDocument $doc |
||
87 | * |
||
88 | * @return DOMNode|DOMText |
||
89 | */ |
||
90 | public function getFormattedTerm( DOMDocument &$doc ) { |
||
104 | |||
105 | /** |
||
106 | * @param DOMDocument $doc |
||
107 | */ |
||
108 | private function buildFormattedTerm( DOMDocument &$doc ) { |
||
120 | |||
121 | /** |
||
122 | * @return bool |
||
123 | */ |
||
124 | private function isSimpleLink() { |
||
129 | |||
130 | /** |
||
131 | * @param DOMDocument $doc |
||
132 | * @return DOMElement |
||
133 | */ |
||
134 | protected function buildFormattedTermAsLink( DOMDocument &$doc ) { |
||
158 | |||
159 | /** |
||
160 | * @param DOMDocument $doc |
||
161 | * |
||
162 | * @return DOMElement |
||
163 | */ |
||
164 | protected function buildFormattedTermAsTooltip( DOMDocument &$doc ) { |
||
177 | |||
178 | /** |
||
179 | * @param DOMDocument $doc |
||
180 | * |
||
181 | * @return DOMElement |
||
182 | */ |
||
183 | protected function buildTerm( DOMDocument &$doc ) { |
||
194 | |||
195 | /** |
||
196 | * @param Title $target |
||
197 | * @param DOMElement $link |
||
198 | * |
||
199 | * @return DOMElement |
||
200 | */ |
||
201 | protected function &addClassAttributeToLink( $target, &$link ) { |
||
229 | |||
230 | /** |
||
231 | * @param Title $target |
||
232 | * @param DOMElement $link |
||
233 | * |
||
234 | * @return DOMElement |
||
235 | */ |
||
236 | protected function &addTitleAttributeToLink( $target, &$link ) { |
||
249 | |||
250 | /** |
||
251 | * @return string[] |
||
252 | */ |
||
253 | public function getFormattedDefinitions() { |
||
261 | |||
262 | /** |
||
263 | */ |
||
264 | protected function buildFormattedDefinitions() { |
||
298 | |||
299 | /** |
||
300 | * @return string |
||
301 | */ |
||
302 | public function getId() { |
||
305 | |||
306 | /** |
||
307 | * @return mixed |
||
308 | */ |
||
309 | public function getCurrentKey() { |
||
312 | |||
313 | /** |
||
314 | * @param $key |
||
315 | * |
||
316 | * @return mixed |
||
317 | */ |
||
318 | public function getTerm( $key ) { |
||
321 | |||
322 | /** |
||
323 | * @param $key |
||
324 | * |
||
325 | * @return mixed |
||
326 | */ |
||
327 | public function getSource( &$key ) { |
||
330 | |||
331 | /** |
||
332 | * @param $key |
||
333 | * |
||
334 | * @return mixed |
||
335 | */ |
||
336 | public function getDefinition( &$key ) { |
||
339 | |||
340 | /** |
||
341 | * @param $key |
||
342 | * |
||
343 | * @return mixed |
||
344 | */ |
||
345 | public function getLink( &$key ) { |
||
348 | |||
349 | /** |
||
350 | * @param $key |
||
351 | * |
||
352 | * @return mixed |
||
353 | */ |
||
354 | public function getStyle( &$key ) { |
||
357 | |||
358 | public function next() { |
||
361 | |||
362 | } |
||
363 |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state