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 |
||
34 | abstract class Element { |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $table; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $row; |
||
45 | |||
46 | /** |
||
47 | * This array holds the local visibility settings (from the 'tx_languagevisibility_visibility' field) |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $localVisibilitySetting; |
||
52 | |||
53 | /** |
||
54 | * This array holds the global visibility setting, the global visibility setting. The translation of an element can overwrite |
||
55 | * the visibility of its own language. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $overlayVisibilitySetting; |
||
60 | |||
61 | /** |
||
62 | * @var \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend |
||
63 | */ |
||
64 | protected $cache = NULL; |
||
65 | |||
66 | /** |
||
67 | * @param string $row |
||
68 | * @param string $tablename |
||
69 | * @throws \AOE\Languagevisibility\Exceptions\InvalidRowException |
||
70 | * @return \AOE\Languagevisibility\Element |
||
|
|||
71 | */ |
||
72 | public function __construct($row, $tablename = '') { |
||
103 | |||
104 | /** |
||
105 | * Sets the table name |
||
106 | * |
||
107 | * @param string $table |
||
108 | * @return void |
||
109 | */ |
||
110 | public function setTable($table) { |
||
113 | |||
114 | /** |
||
115 | * Gets the table name |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | public function getTable() { |
||
122 | |||
123 | /** |
||
124 | * Method to determine that an Element will not be instantiated with |
||
125 | * data of an overlay. |
||
126 | */ |
||
127 | protected function isRowOriginal($row) { |
||
139 | |||
140 | /** |
||
141 | * possibility to add inits in subclasses |
||
142 | **/ |
||
143 | protected function initialisations() { |
||
145 | |||
146 | /** |
||
147 | * Returns the Uid of the Element |
||
148 | * |
||
149 | * @return int |
||
150 | */ |
||
151 | public function getUid() { |
||
154 | |||
155 | /** |
||
156 | * Returns the pid of the Element |
||
157 | * |
||
158 | * @return int |
||
159 | */ |
||
160 | public function getPid() { |
||
163 | |||
164 | /** |
||
165 | * Return the content of the title field |
||
166 | * |
||
167 | * @return unknown |
||
168 | */ |
||
169 | public function getTitle() { |
||
172 | |||
173 | /** |
||
174 | * Returns the uid of the original element. This method will only return |
||
175 | * a non zero value if the element is an overlay; |
||
176 | * |
||
177 | * @return int |
||
178 | */ |
||
179 | public function getOrigElementUid() { |
||
188 | |||
189 | /** |
||
190 | * Returns the workspace uid of an element. |
||
191 | * |
||
192 | * @return unknown |
||
193 | */ |
||
194 | public function getWorkspaceUid() { |
||
201 | |||
202 | /** |
||
203 | * Returns an description of the element. |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | public function getInformativeDescription() { |
||
221 | |||
222 | /** |
||
223 | * This method is used to determine the visibility of the element. Technically it merges the visibility of |
||
224 | * the default language record and the overlay record and returns the visibility. The visibility in the overlayrecord |
||
225 | * can overwrite the visibility of its own language. |
||
226 | * |
||
227 | * @param $languageid |
||
228 | * @return string |
||
229 | */ |
||
230 | public function getLocalVisibilitySetting($languageid) { |
||
244 | |||
245 | //make protected? |
||
246 | /** |
||
247 | * Returns the global visibility setting for the element (saved in the overlay) |
||
248 | * |
||
249 | * @param $languageid |
||
250 | * @return string |
||
251 | */ |
||
252 | public function getVisibilitySettingStoredInOverlayRecord($languageid) { |
||
269 | |||
270 | /** |
||
271 | * This method is only need to display the visibility setting in the backend. |
||
272 | * |
||
273 | * @param int $languageid |
||
274 | * @return string |
||
275 | */ |
||
276 | public function getVisibilitySettingStoredInDefaultRecord($languageid) { |
||
279 | |||
280 | /** |
||
281 | * This method returns an overlay of a record, independent from |
||
282 | * a frontend or backend context |
||
283 | * |
||
284 | * @param string $table |
||
285 | * @param string $olrow |
||
286 | * @return array |
||
287 | */ |
||
288 | protected function getContextIndependentWorkspaceOverlay($table, $olrow) { |
||
297 | |||
298 | /** |
||
299 | * receive relevant fallbackOrder |
||
300 | */ |
||
301 | public function getFallbackOrder(Language $language) { |
||
304 | |||
305 | |||
306 | /** |
||
307 | * Check if the element is set to the default language |
||
308 | * |
||
309 | * @return boolean |
||
310 | */ |
||
311 | public function isLanguageSetToDefault() { |
||
314 | |||
315 | /** |
||
316 | * Determines if the elements is a original or a overlay-element |
||
317 | * |
||
318 | * @return boolean |
||
319 | */ |
||
320 | protected function isOrigElement() { |
||
326 | |||
327 | /** |
||
328 | * Checks if the current record is set to language all (that is typically used to indicate that per default this element is visible in all langauges) |
||
329 | * |
||
330 | * @return boolean |
||
331 | */ |
||
332 | public function isLanguageSetToAll() { |
||
339 | |||
340 | /** |
||
341 | * Method to check the element is an Workspaceelement or not. |
||
342 | * |
||
343 | * @return boolean |
||
344 | */ |
||
345 | public function isLiveWorkspaceElement() { |
||
348 | |||
349 | /** |
||
350 | * Determines whether the element is a translated original record ... |
||
351 | * |
||
352 | * @return boolean |
||
353 | */ |
||
354 | public function isMonolithicTranslated() { |
||
362 | |||
363 | /** |
||
364 | * Compare element-language and foreign language. |
||
365 | * @ todo make this method work for pages |
||
366 | * |
||
367 | * @param Language $language |
||
368 | * @return boolean |
||
369 | */ |
||
370 | public function languageEquals(Language $language) { |
||
373 | |||
374 | /** |
||
375 | * Checks if this element has a translation, therefor several DB accesses are required |
||
376 | * |
||
377 | * @param $languageid |
||
378 | * @return boolean |
||
379 | */ |
||
380 | public function hasTranslation($languageid) { |
||
393 | |||
394 | /** |
||
395 | * Checks if this Element has a Translation in any workspace. |
||
396 | * |
||
397 | * @return boolean |
||
398 | */ |
||
399 | public function hasAnyTranslationInAnyWorkspace() { |
||
406 | |||
407 | /** |
||
408 | * This method can be used to determine if an overlay for a language exists. |
||
409 | * |
||
410 | * @return boolean |
||
411 | * @param int $langid |
||
412 | */ |
||
413 | protected function _hasOverlayRecordForLanguage($langid) { |
||
417 | |||
418 | /** |
||
419 | * Returns which field in the language should be used to read the default visibility |
||
420 | * |
||
421 | *@return string (blank=default / page=page) |
||
422 | **/ |
||
423 | public function getFieldToUseForDefaultVisibility() { |
||
426 | |||
427 | /** |
||
428 | * Method to get a short description of the elementtype. |
||
429 | * An extending class should overwrite this method. |
||
430 | * |
||
431 | * @return string |
||
432 | */ |
||
433 | public function getElementDescription() { |
||
436 | |||
437 | /** |
||
438 | * By default no element supports inheritance |
||
439 | * |
||
440 | * @param void |
||
441 | * @return boolean |
||
442 | */ |
||
443 | public function supportsInheritance() { |
||
446 | |||
447 | /** |
||
448 | * Gets the cache |
||
449 | * |
||
450 | * @return \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend |
||
451 | */ |
||
452 | protected function getCache() { |
||
460 | |||
461 | /** |
||
462 | * This method is used to retrieve an overlay record of a given record. |
||
463 | * |
||
464 | * @param $languageId |
||
465 | * @param $onlyUid |
||
466 | * @return array |
||
467 | */ |
||
468 | public function getOverLayRecordForCertainLanguage($languageId, $onlyUid = FALSE) { |
||
485 | |||
486 | /** |
||
487 | * Check the records enableColumns |
||
488 | * |
||
489 | * @param $row |
||
490 | * @return bool |
||
491 | */ |
||
492 | protected function getEnableFieldResult($row) { |
||
519 | |||
520 | /** |
||
521 | * Abstract method to determine if there exsists any translation in any workspace. |
||
522 | * |
||
523 | * @return boolean |
||
524 | */ |
||
525 | abstract public function hasOverLayRecordForAnyLanguageInAnyWorkspace(); |
||
526 | |||
527 | /** |
||
528 | * This method should provide the implementation to get the overlay of an element for a |
||
529 | * certain language. The result is cached be the method getOverLayRecordForCertainLanguage. |
||
530 | * |
||
531 | * @param int $languageId |
||
532 | * @return |
||
533 | */ |
||
534 | abstract protected function getOverLayRecordForCertainLanguageImplementation($languageId); |
||
535 | } |
||
536 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.