Complex classes like FieldVisibility 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 FieldVisibility, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class FieldVisibility { |
||
36 | private $isNewElement = FALSE; |
||
37 | private $pageId = 0; |
||
38 | private $modTSconfig = array(); |
||
39 | private $calcPerms = FALSE; |
||
40 | |||
41 | /** |
||
42 | * @return void |
||
43 | */ |
||
44 | public function init() { |
||
47 | |||
48 | /** |
||
49 | * @param $PA |
||
50 | * @param $fobj |
||
51 | * @return string |
||
52 | */ |
||
53 | public function user_fieldvisibility($PA) { |
||
54 | $content = ''; |
||
55 | $this->init(); |
||
56 | |||
57 | //init some class attributes |
||
58 | $this->pageId = $PA['row']['pid']; |
||
59 | $uid = $PA['row']['uid']; |
||
60 | |||
61 | if (substr($uid, 0, 3) == 'NEW') { |
||
62 | $this->isNewElement = TRUE; |
||
63 | } |
||
64 | if ($PA['table'] == 'pages' && ! $this->isNewElement) { |
||
65 | $this->pageId = $PA['row']['uid']; |
||
66 | } |
||
67 | |||
68 | $_modTSconfig = $GLOBALS['BE_USER']->getTSConfig('mod.languagevisibility', \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($this->pageId)); |
||
69 | $this->modTSconfig = $_modTSconfig['properties']; |
||
70 | |||
71 | $languageRep = GeneralUtility::makeInstance('AOE\\Languagevisibility\\LanguageRepository'); |
||
72 | $dao = GeneralUtility::makeInstance('AOE\\Languagevisibility\\Dao\DaoCommon'); |
||
73 | |||
74 | $elementfactory = GeneralUtility::makeInstance('AOE\\Languagevisibility\\ElementFactory', $dao); |
||
75 | |||
76 | $value = $PA['row'][$PA['field']]; |
||
77 | $table = $PA['table']; |
||
78 | $isOverlay = BeServices::isOverlayRecord($PA['row'], $table); |
||
79 | |||
80 | $visivilitySetting = @unserialize($value); |
||
81 | if (! is_array($visivilitySetting) && $value != '') { |
||
82 | $content .= 'Visibility Settings seems to be corrupt:' . $value; |
||
83 | } |
||
84 | |||
85 | if ($isOverlay) { |
||
86 | $uid = BeServices::getOriginalUidOfTranslation($PA['row'], $table); |
||
87 | $table = BeServices::getOriginalTableOfTranslation($table); |
||
88 | |||
89 | //This element is an overlay therefore we need to render the visibility field just for the language of the overlay |
||
90 | $overlayRecordsLanguage = $languageRep->getLanguageById($PA['row']['sys_language_uid']); |
||
91 | |||
92 | try { |
||
93 | $originalElement = $elementfactory->getElementForTable($table, $uid); |
||
94 | } catch ( \Exception $e ) { |
||
95 | return ''; |
||
96 | } |
||
97 | |||
98 | $infosStruct = $this->_getLanguageInfoStructurListForElementAndLanguageList($originalElement, array($overlayRecordsLanguage ), $PA['itemFormElName'], TRUE); |
||
99 | } else { |
||
100 | //This element is an original element (no overlay) |
||
101 | try { |
||
102 | $originalElement = $elementfactory->getElementForTable($table, $uid); |
||
103 | } catch ( \Exception $e ) { |
||
104 | return 'sorry this element supports no visibility settings'; |
||
105 | } |
||
106 | |||
107 | $content .= $originalElement->getInformativeDescription(); |
||
108 | |||
109 | if ($originalElement->isMonolithicTranslated()) { |
||
110 | return $content; |
||
111 | } |
||
112 | |||
113 | $languageList = $languageRep->getLanguages(); |
||
114 | $infosStruct = $this->_getLanguageInfoStructurListForElementAndLanguageList($originalElement, $languageList, $PA['itemFormElName'], FALSE); |
||
115 | } |
||
116 | |||
117 | $content .= $this->renderLanguageInfos($infosStruct); |
||
118 | return '<div id="fieldvisibility">' . $content . '<a href="#" onclick="resetSelectboxes()">reset</a></div>' . $this->_javascript(); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * This methid is used to generate an infostructur array, which will be |
||
123 | * renderd as a Form |
||
124 | * |
||
125 | * @param tx_languagevisibility_element $changeableElement |
||
126 | * @param array $languageList |
||
127 | * @param string $itemFormElName |
||
128 | * @param boolean $isOverlay |
||
129 | * @return unknown |
||
130 | */ |
||
131 | public function _getLanguageInfoStructurListForElementAndLanguageList($changeableElement, $languageList, $itemFormElName, $isOverlay) { |
||
132 | |||
133 | $visibility = GeneralUtility::makeInstance('AOE\\Languagevisibility\\Services\\VisibilityService'); |
||
134 | $visibilityString = ''; |
||
135 | $infosStruct = array(); |
||
136 | |||
137 | foreach ( $languageList as $language ) { |
||
138 | |||
139 | $infoitem = array('visible' => $visibility->isVisible($language, $changeableElement), 'languageTitle' => $language->getTitle($this->pageId), 'languageFlag' => $language->getFlagImg($this->pageId), 'hasTranslation' => $changeableElement->hasTranslation($language->getUid()), 'isTranslation' => $isOverlay, 'isVisible' => $visibility->isVisible($language, $changeableElement), 'visibilityDescription' => $visibility->getVisibilityDescription($language, $changeableElement) ); |
||
140 | |||
141 | // if there is no access to language - and localsettings exist, then do not show select box |
||
142 | // this is to not be able as an translator to override languagesetting |
||
143 | $currentSetting = $changeableElement->getLocalVisibilitySetting($language->getUid()); |
||
144 | $currentOptionsForUserAndLanguage = BeServices::getAvailableOptionsForLanguage($language, $isOverlay, $changeableElement); |
||
145 | if ($currentSetting == '' || isset($currentOptionsForUserAndLanguage[$currentSetting])) { |
||
146 | |||
147 | if ($isOverlay) { |
||
148 | $defaultSelect = $changeableElement->getVisibilitySettingStoredInOverlayRecord($language->getUid()); |
||
149 | |||
150 | $visibilityValue = $changeableElement->getVisibilitySettingStoredInDefaultRecord($language->getUid()); |
||
151 | $visibilityString = $currentOptionsForUserAndLanguage[$visibilityValue]; |
||
152 | } else { |
||
153 | $defaultSelect = $changeableElement->getVisibilitySettingStoredInDefaultRecord($language->getUid()); |
||
154 | |||
155 | $visibilityValue = $changeableElement->getVisibilitySettingStoredInOverlayRecord($language->getUid()); |
||
156 | $visibilityString = $currentOptionsForUserAndLanguage[$visibilityValue]; |
||
157 | } |
||
158 | |||
159 | if ($this->isNewElement && $defaultSelect == '') { |
||
160 | if ($this->modTSconfig['language.'][$language->getUid() . '.']['defaultVisibilityOnCreate'] != '') { |
||
161 | $defaultSelect = $this->modTSconfig['language.'][$language->getUid() . '.']['defaultVisibilityOnCreate']; |
||
162 | } |
||
163 | } |
||
164 | $selectBox = $this->getSelectBox($language->getUid(), $currentOptionsForUserAndLanguage, $defaultSelect, $itemFormElName); |
||
165 | } else { |
||
166 | $selectBox = '<input type="hidden" name="' . $itemFormElName . '[' . $language->getUid() . ']" value="' . $currentSetting . '" ></input>(' . $currentSetting . ')'; |
||
167 | } |
||
168 | |||
169 | if ($isOverlay) { |
||
170 | $infoitem['overlayVisibility'] = $selectBox; |
||
171 | $infoitem['originalVisibility'] = $visibilityString; |
||
172 | } else { |
||
173 | $infoitem['overlayVisibility'] = $visibilityString; |
||
174 | $infoitem['originalVisibility'] = $selectBox; |
||
175 | } |
||
176 | |||
177 | $infosStruct[] = $infoitem; |
||
178 | } |
||
179 | |||
180 | return $infosStruct; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Generates the selectbox for the languagevisibility settings of an item |
||
185 | * |
||
186 | * @param int $languageid |
||
187 | * @param array $select |
||
188 | * @param string $current current selected item |
||
189 | * @param string $name |
||
190 | * @return string |
||
191 | */ |
||
192 | protected function getSelectBox($languageid, $select, $current, $name) { |
||
212 | |||
213 | /** |
||
214 | * This method is used to determine a css class for the diffrent visiblity modes |
||
215 | * |
||
216 | * @param string |
||
217 | * @return string |
||
218 | */ |
||
219 | protected function getCSSClassFromVisibilityKey($key) { |
||
235 | |||
236 | protected function renderLanguageInfos($infosStruct) { |
||
271 | |||
272 | /** |
||
273 | * @param $key |
||
274 | * @return mixed |
||
275 | */ |
||
276 | public function getLLL($key) { |
||
279 | |||
280 | /** |
||
281 | * Generated a little status icon |
||
282 | * |
||
283 | * @param boolean positive or negative state |
||
284 | * @param string $title |
||
285 | * @return html tag to include the state image |
||
286 | */ |
||
287 | protected function _getStatusImage($stat, $title = '') { |
||
294 | |||
295 | protected function _link_edit($table, $id) { |
||
300 | |||
301 | /******************************************* |
||
302 | * |
||
303 | * Link functions (protected) |
||
304 | * |
||
305 | *******************************************/ |
||
306 | |||
307 | /** |
||
308 | * Returns an HTML link for editing |
||
309 | * |
||
310 | * @param string $label The label (or image) |
||
311 | * @param string $table The table, fx. 'tt_content' |
||
312 | * @param integer $uid The uid of the element to be edited |
||
313 | * @param boolean $forced By default the link is not shown if translatorMode is set, but with this boolean it can be forced anyway. |
||
314 | * @return string HTML anchor tag containing the label and the correct link |
||
315 | * @access protected |
||
316 | */ |
||
317 | public function link_edit($label, $table, $uid, $forced = FALSE) { |
||
333 | |||
334 | /** |
||
335 | * @return string |
||
336 | */ |
||
337 | protected function _javascript() { |
||
375 | } |
||
376 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.