| Total Complexity | 51 |
| Total Lines | 423 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
Complex classes like CheckExtraFieldAuthorsCompanyPlugin 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 CheckExtraFieldAuthorsCompanyPlugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class CheckExtraFieldAuthorsCompanyPlugin extends Plugin |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @var string |
||
| 9 | */ |
||
| 10 | protected $tblExtraFieldOption; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | protected $tblExtraField; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var bool |
||
| 19 | */ |
||
| 20 | protected $authorsExist; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | protected $companyExist; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $authorsField; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $companyField; |
||
| 36 | |||
| 37 | public function __construct() |
||
| 38 | { |
||
| 39 | parent::__construct( |
||
| 40 | '1.2', |
||
| 41 | 'Carlos Alvarado, Julio Montoya' |
||
| 42 | ); |
||
| 43 | $this->tblExtraField = Database::get_main_table(TABLE_EXTRA_FIELD); |
||
| 44 | $this->tblExtraFieldOption = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS); |
||
| 45 | $field = new ExtraField('user'); |
||
| 46 | $companyField = $field->get_handler_field_info_by_field_variable('company'); |
||
| 47 | $this->companyExist = false; |
||
| 48 | if (empty($companyField)) { |
||
| 49 | $this->companyExist = true; |
||
| 50 | $this->companyField = $companyField; |
||
| 51 | } else { |
||
| 52 | $this->companyField = [ |
||
| 53 | 'field_type' => ExtraField::FIELD_TYPE_RADIO, |
||
| 54 | 'variable' => 'company', |
||
| 55 | 'display_text' => 'Company', |
||
| 56 | 'default_value' => '', |
||
| 57 | 'field_order' => 0, |
||
| 58 | 'visible_to_self' => 0, |
||
| 59 | 'visible_to_others' => 0, |
||
| 60 | 'changeable' => 1, |
||
| 61 | 'filter' => 1, |
||
| 62 | ]; |
||
| 63 | } |
||
| 64 | $field = new ExtraField('lp'); |
||
| 65 | $authorsField = $field->get_handler_field_info_by_field_variable('authors'); |
||
| 66 | |||
| 67 | $this->authorsExist = false; |
||
| 68 | if (empty($authorsField)) { |
||
| 69 | $this->authorsExist = true; |
||
| 70 | $this->authorsField = $authorsField; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Create a new instance of CheckExtraFieldAuthorsCompanyPlugin. |
||
| 76 | * |
||
| 77 | * @return CheckExtraFieldAuthorsCompanyPlugin |
||
| 78 | */ |
||
| 79 | public static function create() |
||
| 80 | { |
||
| 81 | static $result = null; |
||
| 82 | |||
| 83 | return $result ? $result : $result = new self(); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Perform the plugin installation. |
||
| 88 | */ |
||
| 89 | public function install() |
||
| 90 | { |
||
| 91 | $this->saveCompanyField(); |
||
| 92 | $this->setCompanyExtrafieldData(); |
||
| 93 | $this->saveAuthorsField(); |
||
| 94 | $this->savePrice(); |
||
| 95 | $this->saveAuthorLPItem(); |
||
| 96 | $this->saveAuthorLp(); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Save the arrangement for company, it is adjusted internally so that the values match the necessary ones. |
||
| 101 | */ |
||
| 102 | public function saveCompanyField() |
||
| 103 | { |
||
| 104 | $data = $this->companyField; |
||
| 105 | $data['field_type'] = (int) $data['field_type']; |
||
| 106 | $data['field_order'] = (int) $data['field_order']; |
||
| 107 | $data['visible_to_self'] = (int) $data['visible_to_self']; |
||
| 108 | $data['visible_to_others'] = (int) $data['visible_to_others']; |
||
| 109 | $data['changeable'] = (int) $data['changeable']; |
||
| 110 | $data['filter'] = (int) $data['filter']; |
||
| 111 | $data['default_value'] = ''; |
||
| 112 | $data['variable'] = 'company'; |
||
| 113 | $data['visible'] = 1; |
||
| 114 | $data['display_text'] = strtolower(Database::escape_string($data['display_text'])); |
||
| 115 | $schedule = new ExtraField('user'); |
||
| 116 | $this->companyField['id'] = $schedule->save($data); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Insert the option fields for company with the generic values Company 1, company 2 and company 3. |
||
| 121 | */ |
||
| 122 | public function setCompanyExtrafieldData() |
||
| 123 | { |
||
| 124 | $companies = [ |
||
| 125 | 0 => 'Company 1', |
||
| 126 | 1 => 'Company 2', |
||
| 127 | 2 => 'Company 3', |
||
| 128 | ]; |
||
| 129 | $companyId = (int) $this->companyField['id']; |
||
| 130 | if ($companyId != 0) { |
||
| 131 | for ($i = 0; $i < count($companies); $i++) { |
||
|
|
|||
| 132 | $order = $i + 1; |
||
| 133 | $extraFieldOptionValue = $companies[$i]; |
||
| 134 | if ($companyId != null) { |
||
| 135 | $query = "SELECT * |
||
| 136 | FROM ".$this->tblExtraFieldOption." |
||
| 137 | WHERE |
||
| 138 | option_value = '$extraFieldOptionValue' AND |
||
| 139 | field_id = $companyId"; |
||
| 140 | $extraFieldOption = Database::fetch_assoc(Database::query($query)); |
||
| 141 | if (isset($extraFieldOption['id']) && $extraFieldOption['id'] && $extraFieldOption['field_id'] == $companyId) { |
||
| 142 | // Update? |
||
| 143 | } else { |
||
| 144 | $query = " |
||
| 145 | INSERT INTO ".$this->tblExtraFieldOption." |
||
| 146 | (`field_id`, `option_value`, `display_text`, `priority`, `priority_message`, `option_order`) VALUES |
||
| 147 | ( '$companyId', '$extraFieldOptionValue', '$extraFieldOptionValue', NULL, NULL, '$order'); |
||
| 148 | "; |
||
| 149 | Database::query($query); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Save the arrangement for authors, it is adjusted internally so that the values match the necessary ones. |
||
| 158 | */ |
||
| 159 | public function saveAuthorsField() |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Save the arrangement for price, it is adjusted internally so that the values match the necessary ones. |
||
| 178 | */ |
||
| 179 | public function savePrice() |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Save the arrangement for AuthorLPItem, it is adjusted internally so that the values match the necessary ones. |
||
| 196 | */ |
||
| 197 | public function saveAuthorLPItem() |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Save the arrangement for authorlp, it is adjusted internally so that the values match the necessary ones. |
||
| 213 | */ |
||
| 214 | public function saveAuthorLp() |
||
| 215 | { |
||
| 216 | $schedule = new ExtraField('user'); |
||
| 217 | $data = []; |
||
| 218 | $data['variable'] = 'authorlp'; |
||
| 219 | $data['display_text'] = 'authors'; |
||
| 220 | $data['changeable'] = 1; |
||
| 221 | $data['visible_to_self'] = 1; |
||
| 222 | $data['visible_to_others'] = 0; |
||
| 223 | $data['filter'] = 0; |
||
| 224 | $data['field_type'] = ExtraField::FIELD_TYPE_RADIO; |
||
| 225 | $id = $schedule->save($data); |
||
| 226 | $this->setYesNoToAuthor($id); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Set Yes or Not selector for authorlp field. |
||
| 231 | * |
||
| 232 | * @param $authorLpId |
||
| 233 | */ |
||
| 234 | public function setYesNoToAuthor($authorLpId) |
||
| 235 | { |
||
| 236 | $options = [ |
||
| 237 | 0 => 'No', |
||
| 238 | 1 => 'Yes', |
||
| 239 | ]; |
||
| 240 | $order = 0; |
||
| 241 | $authorId = (int) $authorLpId; |
||
| 242 | if ($authorId != 0) { |
||
| 243 | $extraFieldValueUser = new ExtraFieldOption('user'); |
||
| 244 | $items = $extraFieldValueUser->get_field_options_by_field($authorLpId); |
||
| 245 | if (!empty($items)) { |
||
| 246 | foreach ($items as $item) { |
||
| 247 | if (isset($options[0]) && |
||
| 248 | (isset($item['option_value']) == $options[0] || isset($item['option_value']) == $options[1]) |
||
| 249 | ) { |
||
| 250 | unset($options[$item['option_value']]); |
||
| 251 | $order++; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | for ($i = 0; $i < count($options); $i++) { |
||
| 257 | if (isset($options[$i])) { |
||
| 258 | $extraFieldOptionValue = $options[$i]; |
||
| 259 | $fieldOption = new ExtraFieldOption('user'); |
||
| 260 | $fieldOption->saveOptions( |
||
| 261 | [ |
||
| 262 | 'field_id' => $authorLpId, |
||
| 263 | 'option_value' => $order, |
||
| 264 | 'display_text' => $extraFieldOptionValue, |
||
| 265 | 'option_order' => $order, |
||
| 266 | ] |
||
| 267 | ); |
||
| 268 | } |
||
| 269 | $order++; |
||
| 270 | } |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Remove the extra fields set by the plugin. |
||
| 276 | */ |
||
| 277 | public function uninstall() |
||
| 285 | // $this->removeAuthorsField(); |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Verify that the "company" field exists in the database. |
||
| 291 | * |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | public function companyFieldExist() |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Returns the content of the extra field "company" if it exists in the database, if not, it returns an arrangement |
||
| 304 | * with the basic elements for its operation. |
||
| 305 | * |
||
| 306 | * @return array |
||
| 307 | */ |
||
| 308 | public function getCompanyField() |
||
| 309 | { |
||
| 310 | $companyField = $this->getInfoExtrafield('company'); |
||
| 311 | if (count($companyField) > 1) { |
||
| 312 | $this->companyField = $companyField; |
||
| 313 | } else { |
||
| 314 | $companyField = $this->companyField; |
||
| 315 | } |
||
| 316 | |||
| 317 | return $companyField; |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Returns the array of an element in the database that matches the variable. |
||
| 322 | * |
||
| 323 | * @param string $variableName |
||
| 324 | * |
||
| 325 | * @return array |
||
| 326 | */ |
||
| 327 | protected function getInfoExtrafield($variableName = null) |
||
| 328 | { |
||
| 329 | if ($variableName == null) { |
||
| 330 | return []; |
||
| 331 | } |
||
| 332 | $variableName = strtolower(Database::escape_string($variableName)); |
||
| 333 | $tblExtraField = $this->tblExtraField; |
||
| 334 | $query = "SELECT * FROM $tblExtraField WHERE variable = '$variableName'"; |
||
| 335 | $data = Database::fetch_assoc(Database::query($query)); |
||
| 336 | if ($data == false || !isset($data['display_text'])) { |
||
| 337 | return []; |
||
| 338 | } |
||
| 339 | |||
| 340 | return $data; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Verify that the "authors" field exists in the database. |
||
| 345 | * |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | public function authorsFieldExist() |
||
| 349 | { |
||
| 350 | $this->getAuthorsField(); |
||
| 351 | $this->authorsExist = (isset($this->authorsField['id'])) ? true : false; |
||
| 352 | |||
| 353 | return $this->authorsExist; |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Returns the content of the extra field "authors" if it exists in the database, if not, it returns an arrangement |
||
| 358 | * with the basic elements for its operation. |
||
| 359 | * |
||
| 360 | * @return array |
||
| 361 | */ |
||
| 362 | public function getAuthorsField() |
||
| 363 | { |
||
| 364 | $schedule = new ExtraField('lp'); |
||
| 365 | $data = $schedule->get_handler_field_info_by_field_variable('authors'); |
||
| 366 | if (empty($data)) { |
||
| 367 | $this->authorsField = $data; |
||
| 368 | } else { |
||
| 369 | $data = $this->authorsField; |
||
| 370 | } |
||
| 371 | |||
| 372 | return $data; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Remove the extra fields "company". |
||
| 377 | */ |
||
| 378 | public function removeCompanyField() |
||
| 379 | { |
||
| 380 | $data = $this->getCompanyField(); |
||
| 381 | // $this->deleteQuery($data); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Remove the extra fields "authors". |
||
| 386 | */ |
||
| 387 | public function removeAuthorsField() |
||
| 388 | { |
||
| 389 | $data = $this->getAuthorsField(); |
||
| 390 | // $this->deleteQuery($data); |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Executes fix removal for authors or company. |
||
| 395 | * |
||
| 396 | * @param $data |
||
| 397 | */ |
||
| 398 | protected function deleteQuery($data) |
||
| 428 | } |
||
| 429 | } |
||
| 430 | } |
||
| 431 | } |
||
| 432 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: