| Total Complexity | 117 |
| Total Lines | 662 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Item 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 Item, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Item extends \App\Base |
||
| 18 | { |
||
| 19 | /** @var int Item ID */ |
||
| 20 | protected $id; |
||
| 21 | /** @var string Item name */ |
||
| 22 | protected $name; |
||
| 23 | /** @var int Permission level */ |
||
| 24 | protected $presence = 1; |
||
| 25 | /** @var string Description */ |
||
| 26 | protected $description; |
||
| 27 | /** @var string Prefix for numbering */ |
||
| 28 | protected $prefix; |
||
| 29 | /** @var string Icon */ |
||
| 30 | protected $icon; |
||
| 31 | /** @var string Color */ |
||
| 32 | protected $color; |
||
| 33 | /** @var int Sort ID */ |
||
| 34 | protected $sortorderid; |
||
| 35 | /** @var int Item ID for role */ |
||
| 36 | protected $valueid; |
||
| 37 | /** @var int Record state */ |
||
| 38 | protected $record_state; |
||
| 39 | /** @var int Time counting */ |
||
| 40 | protected $time_counting; |
||
| 41 | /** @var int State for the record */ |
||
| 42 | protected $close_state; |
||
| 43 | /** @var \Settings_Picklist_Field_Model */ |
||
| 44 | protected $fieldModel; |
||
| 45 | /** @var array Changes */ |
||
| 46 | protected $changes = []; |
||
| 47 | /** @var string[] Role IDs */ |
||
| 48 | protected $roles; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get instance. |
||
| 52 | * |
||
| 53 | * @param \Vtiger_Field_Model $fieldModel |
||
| 54 | * @param int|null $id |
||
| 55 | * |
||
| 56 | * @return self |
||
| 57 | */ |
||
| 58 | public static function getInstance(\Vtiger_Field_Model $fieldModel, ?int $id): self |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Function to get the Id. |
||
| 83 | * |
||
| 84 | * @return int |
||
| 85 | */ |
||
| 86 | public function getId(): int |
||
| 87 | { |
||
| 88 | return (int) $this->id; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** {@inheritdoc} */ |
||
| 92 | public function set($key, $value) |
||
| 93 | { |
||
| 94 | $propertyExists = \property_exists($this, $key); |
||
| 95 | if ($this->getId() && !\in_array($key, ['id']) && ($propertyExists && $this->{$key} !== $value && (null !== $this->{$key} || '' !== $value)) && !\array_key_exists($key, $this->changes)) { |
||
| 96 | $this->changes[$key] = $this->get($key); |
||
| 97 | } |
||
| 98 | return $propertyExists ? $this->{$key} = $value : parent::set($key, $value); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** {@inheritdoc} */ |
||
| 102 | public function get($key) |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Save. |
||
| 109 | * |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public function save(): bool |
||
| 113 | { |
||
| 114 | try { |
||
| 115 | $this->validate(); |
||
| 116 | $result = $this->saveToDb(); |
||
| 117 | if ($this->getPreviousValue('name')) { |
||
| 118 | $this->rename(); |
||
| 119 | } |
||
| 120 | } catch (\Throwable $e) { |
||
| 121 | \App\Log::error($e->__toString()); |
||
| 122 | throw $e; |
||
| 123 | } |
||
| 124 | \App\Fields\Picklist::clearCache($this->fieldModel->getName(), $this->fieldModel->getModuleName()); |
||
| 125 | return $result; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Save data to database. |
||
| 130 | * |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | public function saveToDb(): bool |
||
| 134 | { |
||
| 135 | $db = \App\Db::getInstance(); |
||
| 136 | $result = false; |
||
| 137 | $fieldName = $this->fieldModel->getName(); |
||
| 138 | $primaryKey = \App\Fields\Picklist::getPickListId($fieldName); |
||
| 139 | $baseTable = $this->getTableName(); |
||
| 140 | |||
| 141 | $dataForSave = $this->getValuesToSave(); |
||
| 142 | foreach (array_keys(\App\Fields\Picklist::COLUMN_DB_TYPES) as $column) { |
||
| 143 | if (isset($dataForSave[$baseTable][$column])) { |
||
| 144 | \App\Fields\Picklist::addColumn($column, $baseTable); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | $transaction = $db->beginTransaction(); |
||
| 149 | try { |
||
| 150 | foreach ($dataForSave as $tableName => $tableData) { |
||
| 151 | if (!$this->getId() && $baseTable === $tableName) { |
||
| 152 | if ($this->fieldModel->isRoleBased()) { |
||
| 153 | $vId = $db->getUniqueID('vtiger_picklistvalues'); |
||
| 154 | $tableData['picklist_valueid'] = $vId; |
||
| 155 | $this->set('valueid', $vId); |
||
| 156 | } |
||
| 157 | $result = $db->createCommand()->insert($tableName, $tableData)->execute(); |
||
| 158 | $this->id = $db->getLastInsertID($tableName . '_' . $primaryKey . '_seq'); |
||
| 159 | } elseif ($baseTable === $tableName) { |
||
| 160 | $db->createCommand()->update($tableName, $tableData, [$primaryKey => $this->getId()])->execute(); |
||
| 161 | } elseif ('u_#__picklist_close_state' === $tableName) { |
||
| 162 | $db->createCommand()->delete($tableName, ['fieldid' => $this->fieldModel->getId(), 'valueid' => $this->valueid])->execute(); |
||
| 163 | if ($this->close_state) { |
||
| 164 | $db->createCommand()->insert($tableName, ['fieldid' => $this->fieldModel->getId(), 'valueid' => $this->valueid, 'value' => $this->name])->execute(); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | $this->updateRolePermissions(); |
||
| 169 | |||
| 170 | $transaction->commit(); |
||
| 171 | } catch (\Throwable $ex) { |
||
| 172 | $transaction->rollBack(); |
||
| 173 | \App\Log::error($ex->__toString()); |
||
| 174 | throw $ex; |
||
| 175 | } |
||
| 176 | return (bool) $result; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Update role permissions. |
||
| 181 | * |
||
| 182 | * @return void |
||
| 183 | */ |
||
| 184 | public function updateRolePermissions() |
||
| 185 | { |
||
| 186 | if ($this->valueid && null !== $this->roles) { |
||
| 187 | $dbCommand = \App\Db::getInstance()->createCommand(); |
||
| 188 | $dbCommand->delete('vtiger_role2picklist', ['picklistvalueid' => $this->valueid])->execute(); |
||
| 189 | if ($this->roles) { |
||
| 190 | $picklistId = \App\Fields\Picklist::getPicklistIdNr($this->fieldModel->getName()); |
||
| 191 | if ($insertValueList = array_map(fn ($roleid) => [$roleid, $this->valueid, $picklistId], $this->roles)) { |
||
| 192 | $dbCommand->batchInsert('vtiger_role2picklist', ['roleid', 'picklistvalueid', 'picklistid'], $insertValueList)->execute(); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get table name. |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public function getTableName(): string |
||
| 204 | { |
||
| 205 | return \App\Fields\Picklist::getPickListTableName($this->fieldModel->getName()); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get next sequence number. |
||
| 210 | * |
||
| 211 | * @return int |
||
| 212 | */ |
||
| 213 | public function getNextSeq(): int |
||
| 214 | { |
||
| 215 | return (int) (new \App\Db\Query())->from($this->getTableName())->max('sortorderid') + 1; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get writable fields. |
||
| 220 | * |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | public function getWritableFields(): array |
||
| 224 | { |
||
| 225 | return ['name', 'presence', 'sortorderid', 'presence', 'description', 'prefix', 'icon', 'record_state', 'time_counting', 'close_state']; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get field model. |
||
| 230 | * |
||
| 231 | * @return \Settings_Picklist_Field_Model |
||
| 232 | */ |
||
| 233 | public function getFieldModel(): \Settings_Picklist_Field_Model |
||
| 234 | { |
||
| 235 | return $this->fieldModel; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Check if item is deletable. |
||
| 240 | * |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | public function isDeletable(): bool |
||
| 244 | { |
||
| 245 | return 1 === $this->presence && $this->fieldModel->isEditable(); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Rename item value in other data. |
||
| 250 | * |
||
| 251 | * @return void |
||
| 252 | */ |
||
| 253 | public function rename() |
||
| 254 | { |
||
| 255 | $newValue = $this->name; |
||
| 256 | $previousValue = $this->getPreviousValue('name'); |
||
| 257 | $fieldName = $this->fieldModel->getName(); |
||
| 258 | |||
| 259 | $dbCommand = \App\Db::getInstance()->createCommand(); |
||
| 260 | $dataReader = (new \App\Db\Query())->select(['tablename', 'columnname', 'fieldid', 'tabid']) |
||
| 261 | ->from('vtiger_field') |
||
| 262 | ->where(['fieldname' => $fieldName, 'uitype' => [15, 16, 33]]) |
||
| 263 | ->createCommand()->query(); |
||
| 264 | while ($row = $dataReader->read()) { |
||
| 265 | $tableName = $row['tablename']; |
||
| 266 | $columnName = $row['columnname']; |
||
| 267 | $dbCommand->update($tableName, [$columnName => $newValue], [$columnName => $previousValue]) |
||
| 268 | ->execute(); |
||
| 269 | $dbCommand->update('vtiger_field', ['defaultvalue' => $newValue], ['defaultvalue' => $previousValue, 'fieldid' => $row['fieldid']]) |
||
| 270 | ->execute(); |
||
| 271 | $moduleName = \App\Module::getModuleName($row['tabid']); |
||
| 272 | |||
| 273 | \App\Fields\Picklist::clearCache($fieldName, $moduleName); |
||
| 274 | $eventHandler = new \App\EventHandler(); |
||
| 275 | $eventHandler->setParams([ |
||
| 276 | 'fieldname' => $fieldName, |
||
| 277 | 'oldvalue' => $previousValue, |
||
| 278 | 'newvalue' => $newValue, |
||
| 279 | 'module' => $moduleName, |
||
| 280 | 'id' => $this->getId(), |
||
| 281 | ]); |
||
| 282 | $eventHandler->trigger('PicklistAfterRename'); |
||
| 283 | } |
||
| 284 | \App\Fields\Picklist::clearCache($fieldName, $this->fieldModel->getModuleName()); |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Delete item. |
||
| 289 | * |
||
| 290 | * @param int $replaceId Item ID |
||
| 291 | * |
||
| 292 | * @return void |
||
| 293 | */ |
||
| 294 | public function delete(int $replaceId) |
||
| 295 | { |
||
| 296 | $db = \App\Db::getInstance(); |
||
| 297 | $fieldName = $this->fieldModel->getName(); |
||
| 298 | $transaction = $db->beginTransaction(); |
||
| 299 | try { |
||
| 300 | $dbCommand = $db->createCommand(); |
||
| 301 | |||
| 302 | $primaryKey = \App\Fields\Picklist::getPickListId($this->fieldModel->getName()); |
||
| 303 | $replaceValue = \App\Purifier::decodeHtml((new \App\Db\Query())->select([$this->fieldModel->getName()]) |
||
| 304 | ->from($this->getTableName()) |
||
| 305 | ->where([$primaryKey => $replaceId]) |
||
| 306 | ->scalar()); |
||
| 307 | |||
| 308 | if ($this->fieldModel->isRoleBased()) { |
||
| 309 | $dbCommand->delete('vtiger_role2picklist', ['picklistvalueid' => $this->valueid])->execute(); |
||
| 310 | $dbCommand->delete('u_#__picklist_close_state', ['valueid' => $this->valueid])->execute(); |
||
| 311 | } |
||
| 312 | $dbCommand->delete($this->getTableName(), [$primaryKey => $this->getId()])->execute(); |
||
| 313 | $dependencyId = (new \App\Db\Query())->select(['s_#__picklist_dependency.id'])->from('s_#__picklist_dependency')->innerJoin('s_#__picklist_dependency_data', 's_#__picklist_dependency_data.id = s_#__picklist_dependency.id') |
||
| 314 | ->where(['source_field' => $this->fieldModel->getId(), 'source_id' => $this->getId()])->column(); |
||
| 315 | if ($dependencyId) { |
||
| 316 | $dbCommand->delete('s_#__picklist_dependency_data', ['id' => $dependencyId, 'source_id' => $this->getId()])->execute(); |
||
| 317 | } |
||
| 318 | |||
| 319 | $dataReader = (new \App\Db\Query())->select(['tablename', 'columnname', 'fieldid', 'tabid']) |
||
| 320 | ->from('vtiger_field') |
||
| 321 | ->where(['fieldname' => $fieldName, 'uitype' => [15, 16, 33]]) |
||
| 322 | ->createCommand()->query(); |
||
| 323 | while ($row = $dataReader->read()) { |
||
| 324 | $moduleName = \App\Module::getModuleName($row['tabid']); |
||
| 325 | $tableName = $row['tablename']; |
||
| 326 | $columnName = $row['columnname']; |
||
| 327 | $dbCommand->update($tableName, [$columnName => $replaceValue], [$columnName => $this->name]) |
||
| 328 | ->execute(); |
||
| 329 | $dbCommand->update('vtiger_field', ['defaultvalue' => $replaceValue], ['defaultvalue' => $this->name, 'fieldid' => $row['fieldid']]) |
||
| 330 | ->execute(); |
||
| 331 | |||
| 332 | \App\Fields\Picklist::clearCache($fieldName, $moduleName); |
||
| 333 | $eventHandler = new \App\EventHandler(); |
||
| 334 | $eventHandler->setParams([ |
||
| 335 | 'fieldname' => $fieldName, |
||
| 336 | 'valuetodelete' => [$this->name], |
||
| 337 | 'replacevalue' => $replaceValue, |
||
| 338 | 'module' => $moduleName, |
||
| 339 | ]); |
||
| 340 | $eventHandler->trigger('PicklistAfterDelete'); |
||
| 341 | } |
||
| 342 | $dataReader->close(); |
||
| 343 | |||
| 344 | $transaction->commit(); |
||
| 345 | } catch (\Throwable $ex) { |
||
| 346 | $transaction->rollBack(); |
||
| 347 | \App\Log::error($ex->__toString()); |
||
| 348 | throw $ex; |
||
| 349 | } |
||
| 350 | |||
| 351 | \App\Fields\Picklist::clearCache($fieldName, $this->fieldModel->getModuleName()); |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Function formats data for saving. |
||
| 356 | * |
||
| 357 | * @return array |
||
| 358 | */ |
||
| 359 | private function getValuesToSave(): array |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Get pervious value by field. |
||
| 386 | * |
||
| 387 | * @param string $fieldName |
||
| 388 | * |
||
| 389 | * @return mixed |
||
| 390 | */ |
||
| 391 | public function getPreviousValue(?string $fieldName = '') |
||
| 392 | { |
||
| 393 | return $fieldName ? ($this->changes[$fieldName] ?? null) : $this->changes; |
||
| 394 | } |
||
| 395 | |||
| 396 | /** {@inheritdoc} */ |
||
| 397 | public function getData() |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Get fields for edit. |
||
| 415 | * |
||
| 416 | * @return array |
||
| 417 | */ |
||
| 418 | public function getEditFields(): array |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Basic validation. |
||
| 455 | * |
||
| 456 | * @return array |
||
| 457 | */ |
||
| 458 | public function validate(): array |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Check if picklist value exists. |
||
| 472 | * |
||
| 473 | * @return bool |
||
| 474 | */ |
||
| 475 | public function isDuplicateValue(): bool |
||
| 476 | { |
||
| 477 | $picklistValues = \App\Fields\Picklist::getValuesName($this->fieldModel->getName()); |
||
| 478 | if ($this->id) { |
||
| 479 | unset($picklistValues[$this->id]); |
||
| 480 | } |
||
| 481 | |||
| 482 | return \in_array(strtolower($this->name), array_map('strtolower', $picklistValues)); |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Validate item data. |
||
| 487 | * |
||
| 488 | * @param string $fieldName |
||
| 489 | * @param mixed $value |
||
| 490 | * |
||
| 491 | * @return void |
||
| 492 | */ |
||
| 493 | public function validateValue(string $fieldName, $value) |
||
| 534 | } |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Get fields instance by name. |
||
| 539 | * |
||
| 540 | * @param string $name |
||
| 541 | * |
||
| 542 | * @return Vtiger_Field_Model |
||
| 543 | */ |
||
| 544 | public function getFieldInstanceByName($name) |
||
| 679 | } |
||
| 680 | } |
||
| 681 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.