Total Complexity | 58 |
Total Lines | 323 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Complex classes like CheckData 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.
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 CheckData, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class CheckData |
||
34 | { |
||
35 | /** |
||
36 | * @var mixed |
||
37 | */ |
||
38 | private $cf = null; |
||
39 | |||
40 | /** |
||
41 | * @var mixed |
||
42 | */ |
||
43 | private $modId = null; |
||
44 | |||
45 | /** |
||
46 | * @var mixed |
||
47 | */ |
||
48 | private $tables = null; |
||
49 | |||
50 | /** |
||
51 | * @var mixed |
||
52 | */ |
||
53 | private $infos = []; |
||
54 | |||
55 | |||
56 | |||
57 | /** |
||
58 | * @public function constructor |
||
59 | * @param null |
||
60 | */ |
||
61 | public function __construct() |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @static function getInstance |
||
68 | * |
||
69 | * @param null |
||
70 | * |
||
71 | * @return Modulebuilder\Files\CheckData |
||
72 | */ |
||
73 | public static function getInstance() |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @public function getCheckResult |
||
85 | * |
||
86 | * @param $module |
||
87 | * @return array |
||
88 | */ |
||
89 | public function getCheckPreBuilding($module) |
||
90 | { |
||
91 | $this->modId = $module->getVar('mod_id'); |
||
92 | $this->tables = $this->cf->getTableTables($this->modId); |
||
93 | $this->infos = []; |
||
94 | |||
95 | $this->getCheckBlock(); |
||
96 | $this->getCheckBroken(); |
||
97 | $this->getCheckComments(); |
||
98 | $this->getCheckUserpage(); |
||
99 | $this->getCheckRating(); |
||
100 | |||
101 | return $this->infos; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @public function getCheckBroken |
||
106 | * |
||
107 | * @return array|bool |
||
108 | */ |
||
109 | private function getCheckBroken() |
||
110 | { |
||
111 | foreach (array_keys($this->tables) as $t) { |
||
112 | if (1 == $this->tables[$t]->getVar('table_broken')) { |
||
113 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
114 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
115 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
116 | $fieldSatus = ''; |
||
117 | |||
118 | foreach (array_keys($fields) as $f) { |
||
119 | $fieldName = $fields[$f]->getVar('field_name'); |
||
120 | if (16 == $fields[$f]->getVar('field_element')) { |
||
121 | $fieldSatus = $fieldName; |
||
122 | } |
||
123 | } |
||
124 | // check whether each table with handling "broken" has also a field "status" |
||
125 | if ('' == $fieldSatus) { |
||
126 | $info = str_replace('%t', $tableName, _AM_MODULEBUILDER_BUILDING_CHECK_BROKEN1); |
||
127 | $this->infos[] = ['icon' => 'error', 'info' => $info]; |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | |||
132 | return true; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @private function getCheckUserpage |
||
137 | * |
||
138 | * @return array|bool |
||
139 | */ |
||
140 | private function getCheckUserpage() |
||
141 | { |
||
142 | foreach (array_keys($this->tables) as $t) { |
||
143 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
144 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
145 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
146 | |||
147 | foreach (array_keys($fields) as $f) { |
||
148 | $fieldName = $fields[$f]->getVar('field_name'); |
||
149 | // check fields for parameters |
||
150 | if ($f > 0) { |
||
151 | $fieldParams = (int)$fields[$f]->getVar('field_parent') + (int)$fields[$f]->getVar('field_admin') + (int)$fields[$f]->getVar('field_inlist') + (int)$fields[$f]->getVar('field_inform') |
||
152 | + (int)$fields[$f]->getVar('field_user') + (int)$fields[$f]->getVar('field_ihead') + (int)$fields[$f]->getVar('field_ibody') + (int)$fields[$f]->getVar('field_ifoot') |
||
153 | + (int)$fields[$f]->getVar('field_thead') + (int)$fields[$f]->getVar('field_tbody') + (int)$fields[$f]->getVar('field_tfoot') + (int)$fields[$f]->getVar('field_block') |
||
154 | + (int)$fields[$f]->getVar('field_main') + (int)$fields[$f]->getVar('field_search') + (int)$fields[$f]->getVar('field_required'); |
||
155 | if (0 == $fieldParams) { |
||
156 | $info = str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_FIELDS1); |
||
157 | $this->infos[] = ['icon' => 'error', 'info' => $info]; |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | |||
163 | //check user file no usage in index or item |
||
164 | foreach (array_keys($this->tables) as $t) { |
||
165 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
166 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
167 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
168 | |||
169 | foreach (array_keys($fields) as $f) { |
||
170 | $fieldName = $fields[$f]->getVar('field_name'); |
||
171 | if (1 == $fields[$f]->getVar('field_user')) { |
||
172 | // check fields for parameters |
||
173 | if ($f > 0) { |
||
174 | $fieldParams = (int)$fields[$f]->getVar('field_ihead') + (int)$fields[$f]->getVar('field_ibody') + (int)$fields[$f]->getVar('field_ifoot') |
||
175 | + (int)$fields[$f]->getVar('field_thead') + (int)$fields[$f]->getVar('field_tbody') + (int)$fields[$f]->getVar('field_tfoot'); |
||
176 | if (0 == $fieldParams) { |
||
177 | $info = str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_FIELDS2); |
||
178 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | //check user file index multiple usage |
||
185 | //check user file item multiple usage |
||
186 | foreach (array_keys($this->tables) as $t) { |
||
187 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
188 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
189 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
190 | |||
191 | foreach (array_keys($fields) as $f) { |
||
192 | $fieldName = $fields[$f]->getVar('field_name'); |
||
193 | if (1 == $fields[$f]->getVar('field_user')) { |
||
194 | // check fields for parameters |
||
195 | if ($f > 0) { |
||
196 | $fieldParams = (int)$fields[$f]->getVar('field_ihead') + (int)$fields[$f]->getVar('field_ibody') + (int)$fields[$f]->getVar('field_ifoot'); |
||
197 | if ($fieldParams > 1) { |
||
198 | $info = str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_FIELDS3); |
||
199 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
200 | } |
||
201 | $fieldParams = (int)$fields[$f]->getVar('field_thead') + (int)$fields[$f]->getVar('field_tbody') + (int)$fields[$f]->getVar('field_tfoot'); |
||
202 | if ($fieldParams > 1) { |
||
203 | $info = str_replace(['%f', '%t'], [$fieldName, $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_FIELDS3); |
||
204 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | } |
||
210 | |||
211 | return true; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @private function getCheckBlock |
||
216 | * |
||
217 | * @return array|bool |
||
218 | */ |
||
219 | private function getCheckBlock() |
||
220 | { |
||
221 | //use in block but not field selected |
||
222 | foreach (array_keys($this->tables) as $t) { |
||
223 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
224 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
225 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
226 | $count = 0; |
||
227 | if (1 == $this->tables[$t]->getVar('table_blocks')) { |
||
228 | foreach (array_keys($fields) as $f) { |
||
229 | if (1 == $fields[$f]->getVar('field_block')) { |
||
230 | $count++; |
||
231 | } |
||
232 | } |
||
233 | if (0 == $count) { |
||
234 | $info = str_replace(['%t'], [$tableName], _AM_MODULEBUILDER_BUILDING_CHECK_BLOCK1); |
||
235 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
236 | } |
||
237 | } |
||
238 | } |
||
239 | //use in block but not field date |
||
240 | foreach (array_keys($this->tables) as $t) { |
||
241 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
242 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
243 | |||
244 | $count = 0; |
||
245 | if (1 == $this->tables[$t]->getVar('table_blocks')) { |
||
246 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
247 | foreach (array_keys($fields) as $f) { |
||
248 | if (15 == $fields[$f]->getVar('field_element') || 21 == $fields[$f]->getVar('field_element')) { |
||
249 | $count++; |
||
250 | } |
||
251 | } |
||
252 | if (0 == $count) { |
||
253 | $info = str_replace(['%t'], [$tableName], _AM_MODULEBUILDER_BUILDING_CHECK_BLOCK2); |
||
254 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
255 | } |
||
256 | } |
||
257 | } |
||
258 | return true; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @private function getCheckComments |
||
263 | * |
||
264 | * @return array|bool |
||
265 | */ |
||
266 | private function getCheckComments() |
||
267 | { |
||
268 | //use comments in multiple tables |
||
269 | $count = 0; |
||
270 | $tableComments = []; |
||
271 | foreach (array_keys($this->tables) as $t) { |
||
272 | if (1 == $this->tables[$t]->getVar('table_comments')) { |
||
273 | $count++; |
||
274 | $tableComments[] = $this->tables[$t]->getVar('table_name'); |
||
275 | } |
||
276 | } |
||
277 | if ($count > 1) { |
||
278 | $tablesComments = implode(', ', $tableComments); |
||
279 | $info = str_replace('%t', $tablesComments, _AM_MODULEBUILDER_BUILDING_CHECK_COMMENTS1); |
||
280 | $this->infos[] = ['icon' => 'error', 'info' => $info]; |
||
281 | } |
||
282 | |||
283 | foreach (array_keys($this->tables) as $t) { |
||
284 | if (1 == $this->tables[$t]->getVar('table_comments')) { |
||
285 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
286 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
287 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
288 | $fieldComments = ''; |
||
289 | |||
290 | foreach (array_keys($fields) as $f) { |
||
291 | $fieldName = $fields[$f]->getVar('field_name'); |
||
292 | if ($fieldName == $this->tables[$t]->getVar('table_fieldname') . '_comments') { |
||
293 | $fieldComments = $fieldName; |
||
294 | } |
||
295 | } |
||
296 | // check whether each table with handling "comments" has also a field "comments" |
||
297 | if ('' == $fieldComments) { |
||
298 | $info = str_replace(['%f', '%t'], [$this->tables[$t]->getVar('table_fieldname') . '_comments', $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_COMMENTS2); |
||
299 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
300 | } |
||
301 | } |
||
302 | } |
||
303 | |||
304 | return true; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @private function getCheckComments |
||
309 | * |
||
310 | * @return array|bool |
||
311 | */ |
||
312 | private function getCheckRating() |
||
356 | } |
||
357 | } |
||
358 |