| Total Complexity | 42 |
| Total Lines | 377 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| 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_CHECKBOX; |
||
| 225 | $schedule->save($data); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Remove the extra fields set by the plugin. |
||
| 230 | */ |
||
| 231 | public function uninstall() |
||
| 239 | // $this->removeAuthorsField(); |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Verify that the "company" field exists in the database. |
||
| 245 | * |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | public function companyFieldExist() |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns the content of the extra field "company" if it exists in the database, if not, it returns an arrangement |
||
| 258 | * with the basic elements for its operation. |
||
| 259 | * |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function getCompanyField() |
||
| 263 | { |
||
| 264 | $companyField = $this->getInfoExtrafield('company'); |
||
| 265 | if (count($companyField) > 1) { |
||
| 266 | $this->companyField = $companyField; |
||
| 267 | } else { |
||
| 268 | $companyField = $this->companyField; |
||
| 269 | } |
||
| 270 | |||
| 271 | return $companyField; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Returns the array of an element in the database that matches the variable. |
||
| 276 | * |
||
| 277 | * @param string $variableName |
||
| 278 | * |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | protected function getInfoExtrafield($variableName = null) |
||
| 282 | { |
||
| 283 | if ($variableName == null) { |
||
| 284 | return []; |
||
| 285 | } |
||
| 286 | $variableName = strtolower(Database::escape_string($variableName)); |
||
| 287 | $tblExtraField = $this->tblExtraField; |
||
| 288 | $query = "SELECT * FROM $tblExtraField WHERE variable = '$variableName'"; |
||
| 289 | $data = Database::fetch_assoc(Database::query($query)); |
||
| 290 | if ($data == false || !isset($data['display_text'])) { |
||
| 291 | return []; |
||
| 292 | } |
||
| 293 | |||
| 294 | return $data; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Verify that the "authors" field exists in the database. |
||
| 299 | * |
||
| 300 | * @return bool |
||
| 301 | */ |
||
| 302 | public function authorsFieldExist() |
||
| 303 | { |
||
| 304 | $this->getAuthorsField(); |
||
| 305 | $this->authorsExist = (isset($this->authorsField['id'])) ? true : false; |
||
| 306 | |||
| 307 | return $this->authorsExist; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Returns the content of the extra field "authors" if it exists in the database, if not, it returns an arrangement |
||
| 312 | * with the basic elements for its operation. |
||
| 313 | * |
||
| 314 | * @return array |
||
| 315 | */ |
||
| 316 | public function getAuthorsField() |
||
| 317 | { |
||
| 318 | $schedule = new ExtraField('lp'); |
||
| 319 | $data = $schedule->get_handler_field_info_by_field_variable('authors'); |
||
| 320 | if (empty($data)) { |
||
| 321 | $this->authorsField = $data; |
||
| 322 | } else { |
||
| 323 | $data = $this->authorsField; |
||
| 324 | } |
||
| 325 | |||
| 326 | return $data; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Remove the extra fields "company". |
||
| 331 | */ |
||
| 332 | public function removeCompanyField() |
||
| 333 | { |
||
| 334 | $data = $this->getCompanyField(); |
||
| 335 | // $this->deleteQuery($data); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Remove the extra fields "authors". |
||
| 340 | */ |
||
| 341 | public function removeAuthorsField() |
||
| 342 | { |
||
| 343 | $data = $this->getAuthorsField(); |
||
| 344 | // $this->deleteQuery($data); |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Executes fix removal for authors or company. |
||
| 349 | * |
||
| 350 | * @param $data |
||
| 351 | */ |
||
| 352 | protected function deleteQuery($data) |
||
| 382 | } |
||
| 383 | } |
||
| 384 | } |
||
| 385 | } |
||
| 386 |
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: