| Total Complexity | 63 |
| Total Lines | 332 |
| 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() |
||
| 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 | //check user submit or rate or broken, but table not for user side |
||
| 212 | foreach (array_keys($this->tables) as $t) { |
||
| 213 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
|
|
|||
| 214 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
| 215 | if ((0 == $this->tables[$t]->getVar('table_user')) && (1 == $this->tables[$t]->getVar('table_submit') || 1 == $this->tables[$t]->getVar('table_broken') || 1 == $this->tables[$t]->getVar('table_rate'))) { |
||
| 216 | $info = str_replace(['%t'], [$tableName], _AM_MODULEBUILDER_BUILDING_CHECK_USERPAGE1); |
||
| 217 | $this->infos[] = ['icon' => 'error', 'info' => $info]; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | return true; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @private function getCheckBlock |
||
| 225 | * |
||
| 226 | * @return array|bool |
||
| 227 | */ |
||
| 228 | private function getCheckBlock() |
||
| 229 | { |
||
| 230 | //use in block but no field selected |
||
| 231 | foreach (array_keys($this->tables) as $t) { |
||
| 232 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
| 233 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
| 234 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
| 235 | $count = 0; |
||
| 236 | if (1 == $this->tables[$t]->getVar('table_blocks')) { |
||
| 237 | foreach (array_keys($fields) as $f) { |
||
| 238 | if (1 == $fields[$f]->getVar('field_block')) { |
||
| 239 | $count++; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | if (0 == $count) { |
||
| 243 | $info = str_replace(['%t'], [$tableName], _AM_MODULEBUILDER_BUILDING_CHECK_BLOCK1); |
||
| 244 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
| 245 | } |
||
| 246 | } |
||
| 247 | } |
||
| 248 | //use in block but no field date |
||
| 249 | foreach (array_keys($this->tables) as $t) { |
||
| 250 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
| 251 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
| 252 | |||
| 253 | $count = 0; |
||
| 254 | if (1 == $this->tables[$t]->getVar('table_blocks')) { |
||
| 255 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
| 256 | foreach (array_keys($fields) as $f) { |
||
| 257 | if (15 == $fields[$f]->getVar('field_element') || 21 == $fields[$f]->getVar('field_element')) { |
||
| 258 | $count++; |
||
| 259 | } |
||
| 260 | } |
||
| 261 | if (0 == $count) { |
||
| 262 | $info = str_replace(['%t'], [$tableName], _AM_MODULEBUILDER_BUILDING_CHECK_BLOCK2); |
||
| 263 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 | return true; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @private function getCheckComments |
||
| 272 | * |
||
| 273 | * @return array|bool |
||
| 274 | */ |
||
| 275 | private function getCheckComments() |
||
| 276 | { |
||
| 277 | //use comments in multiple tables |
||
| 278 | $count = 0; |
||
| 279 | $tableComments = []; |
||
| 280 | foreach (array_keys($this->tables) as $t) { |
||
| 281 | if (1 == $this->tables[$t]->getVar('table_comments')) { |
||
| 282 | $count++; |
||
| 283 | $tableComments[] = $this->tables[$t]->getVar('table_name'); |
||
| 284 | } |
||
| 285 | } |
||
| 286 | if ($count > 1) { |
||
| 287 | $tablesComments = implode(', ', $tableComments); |
||
| 288 | $info = str_replace('%t', $tablesComments, _AM_MODULEBUILDER_BUILDING_CHECK_COMMENTS1); |
||
| 289 | $this->infos[] = ['icon' => 'error', 'info' => $info]; |
||
| 290 | } |
||
| 291 | |||
| 292 | foreach (array_keys($this->tables) as $t) { |
||
| 293 | if (1 == $this->tables[$t]->getVar('table_comments')) { |
||
| 294 | $tableId = $this->tables[$t]->getVar('table_id'); |
||
| 295 | $tableName = $this->tables[$t]->getVar('table_name'); |
||
| 296 | $fields = $this->cf->getTableFields($this->modId, $tableId); |
||
| 297 | $fieldComments = ''; |
||
| 298 | |||
| 299 | foreach (array_keys($fields) as $f) { |
||
| 300 | $fieldName = $fields[$f]->getVar('field_name'); |
||
| 301 | if ($fieldName == $this->tables[$t]->getVar('table_fieldname') . '_comments') { |
||
| 302 | $fieldComments = $fieldName; |
||
| 303 | } |
||
| 304 | } |
||
| 305 | // check whether each table with handling "comments" has also a field "comments" |
||
| 306 | if ('' == $fieldComments) { |
||
| 307 | $info = str_replace(['%f', '%t'], [$this->tables[$t]->getVar('table_fieldname') . '_comments', $tableName], _AM_MODULEBUILDER_BUILDING_CHECK_COMMENTS2); |
||
| 308 | $this->infos[] = ['icon' => 'warning', 'info' => $info]; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | return true; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @private function getCheckComments |
||
| 318 | * |
||
| 319 | * @return array|bool |
||
| 320 | */ |
||
| 321 | private function getCheckRating() |
||
| 365 | } |
||
| 366 | } |
||
| 367 |