| Total Complexity | 48 |
| Total Lines | 265 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Base 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 Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Base |
||
| 19 | { |
||
| 20 | /** @var string Module name. */ |
||
| 21 | public $moduleName; |
||
| 22 | |||
| 23 | /** @var string[] Allowed modules. */ |
||
| 24 | public $allowedModules; |
||
| 25 | |||
| 26 | /** @var string Icon. */ |
||
| 27 | public $icon; |
||
| 28 | |||
| 29 | /** @var string Label. */ |
||
| 30 | public $label; |
||
| 31 | |||
| 32 | /** @var string Additional description, visible in the modal window. */ |
||
| 33 | public $description; |
||
| 34 | |||
| 35 | /** @var string Search results display type. */ |
||
| 36 | public $displayType; |
||
| 37 | |||
| 38 | /** @var array Configuration field list. */ |
||
| 39 | public $settingsFields = []; |
||
| 40 | |||
| 41 | /** @var string Url to Documentation API */ |
||
| 42 | public $docUrl; |
||
| 43 | |||
| 44 | /** @var string Record collector name. */ |
||
| 45 | protected $name; |
||
| 46 | |||
| 47 | /** var array List of fields for the modal search window. */ |
||
| 48 | protected $fields = []; |
||
| 49 | |||
| 50 | /** @var array Data from record collector source. */ |
||
| 51 | protected $data = []; |
||
| 52 | |||
| 53 | /** @var array Response data. */ |
||
| 54 | protected $response = []; |
||
| 55 | |||
| 56 | /** @var \App\Request Request instance. */ |
||
| 57 | protected $request; |
||
| 58 | |||
| 59 | /** @var array Fields mapping for loading record data. */ |
||
| 60 | protected $modulesFieldsMap = []; |
||
| 61 | |||
| 62 | /** @var bool Requires subscription. */ |
||
| 63 | protected bool $paid = true; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Constructor. |
||
| 67 | */ |
||
| 68 | public function __construct() |
||
| 69 | { |
||
| 70 | $name = basename(str_replace('\\', '/', static::class)); |
||
| 71 | $this->name = $name; |
||
| 72 | |||
| 73 | $class = '\\Config\\Components\\RecordCollectors\\' . $name; |
||
| 74 | if (!class_exists($class)) { |
||
| 75 | return; |
||
| 76 | } |
||
| 77 | $config = (new \ReflectionClass($class))->getStaticProperties(); |
||
| 78 | if (isset($config['allowedModules'])) { |
||
| 79 | $this->allowedModules = $config['allowedModules']; |
||
| 80 | unset($config['allowedModules']); |
||
| 81 | } |
||
| 82 | foreach ($config as $key => $value) { |
||
| 83 | $this->{$key} = $value; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get record collector name. |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public function getName(): string |
||
| 93 | { |
||
| 94 | return $this->name; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Set request. |
||
| 99 | * |
||
| 100 | * @param \App\Request $request |
||
| 101 | * |
||
| 102 | * @return void |
||
| 103 | */ |
||
| 104 | public function setRequest(\App\Request $request): void |
||
| 105 | { |
||
| 106 | $this->request = $request; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get fields for the modal search window. |
||
| 111 | * |
||
| 112 | * @return \Vtiger_Field_Model[] |
||
| 113 | */ |
||
| 114 | public function getFields(): array |
||
| 115 | { |
||
| 116 | $fieldsModel = []; |
||
| 117 | foreach ($this->fields as $fieldName => $data) { |
||
| 118 | if (isset($data['picklistValuesFunction'])) { |
||
| 119 | $data['picklistValues'] = $this->{$data['picklistValuesFunction']}($data); |
||
| 120 | } elseif (isset($data['picklistValues']) && false !== $data['picklistModule']) { |
||
| 121 | $picklistModule = $data['picklistModule'] ?? $this->moduleName; |
||
| 122 | foreach ($data['picklistValues'] as $picklistKey => $value) { |
||
| 123 | $data['picklistValues'][$picklistKey] = \App\Language::translate($value, $picklistModule); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | $fieldModel = \Vtiger_Field_Model::init($this->moduleName, $data, $fieldName); |
||
| 127 | if (isset($this->modulesFieldsMap[$this->moduleName][$fieldName]) && $this->request->has($this->modulesFieldsMap[$this->moduleName][$fieldName])) { |
||
| 128 | try { |
||
| 129 | $uitypeModel = $fieldModel->getUITypeModel(); |
||
| 130 | $value = $this->request->getByType($this->modulesFieldsMap[$this->moduleName][$fieldName], 'Text'); |
||
| 131 | $uitypeModel->validate($value, true); |
||
| 132 | $fieldModel->set('fieldvalue', $uitypeModel->getDBValue($value)); |
||
| 133 | } catch (\Throwable $th) { |
||
| 134 | \App\Log::error($th->__toString(), 'RecordCollectors'); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | $fieldsModel[$fieldName] = $fieldModel; |
||
| 138 | } |
||
| 139 | return $fieldsModel; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get fields for the module name. |
||
| 144 | * |
||
| 145 | * @param string $moduleName |
||
| 146 | * |
||
| 147 | * @return string[] |
||
| 148 | */ |
||
| 149 | public function getFieldsModule(string $moduleName): array |
||
| 150 | { |
||
| 151 | return $this->modulesFieldsMap[$moduleName]; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Check whether it is active. |
||
| 156 | * |
||
| 157 | * @return bool |
||
| 158 | */ |
||
| 159 | public function isActive(): bool |
||
| 160 | { |
||
| 161 | return \in_array($this->moduleName, $this->allowedModules) && $this->isAvailable(); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Check if product is available. |
||
| 166 | * |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function isAvailable(): bool |
||
| 170 | { |
||
| 171 | return !$this->paid || \App\YetiForce\Shop::check($this->getName()); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Search data function. |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | public function search(): array |
||
| 180 | { |
||
| 181 | throw new \Api\Core\Exception('no search function'); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Load data. |
||
| 186 | * |
||
| 187 | * @return void |
||
| 188 | */ |
||
| 189 | public function loadData(): void |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get fields labels for the module name. |
||
| 255 | * |
||
| 256 | * @param string $moduleName |
||
| 257 | * |
||
| 258 | * @return string[] |
||
| 259 | */ |
||
| 260 | public function getFieldsLabelsByModule(string $moduleName): array |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get params of collector. |
||
| 274 | * |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | protected function getParams(): array |
||
| 283 | } |
||
| 284 | } |
||
| 285 |