Total Complexity | 48 |
Total Lines | 268 |
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 | /** @var string The name of Add-on. */ |
||
66 | protected string $addOnName = ''; |
||
67 | |||
68 | /** |
||
69 | * Constructor. |
||
70 | */ |
||
71 | public function __construct() |
||
72 | { |
||
73 | $name = basename(str_replace('\\', '/', static::class)); |
||
74 | $this->name = $name; |
||
75 | |||
76 | $class = '\\Config\\Components\\RecordCollectors\\' . $name; |
||
77 | if (!class_exists($class)) { |
||
78 | return; |
||
79 | } |
||
80 | $config = (new \ReflectionClass($class))->getStaticProperties(); |
||
81 | if (isset($config['allowedModules'])) { |
||
82 | $this->allowedModules = $config['allowedModules']; |
||
83 | unset($config['allowedModules']); |
||
84 | } |
||
85 | foreach ($config as $key => $value) { |
||
86 | $this->{$key} = $value; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Get record collector name. |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | public function getName(): string |
||
96 | { |
||
97 | return $this->name; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Set request. |
||
102 | * |
||
103 | * @param \App\Request $request |
||
104 | * |
||
105 | * @return void |
||
106 | */ |
||
107 | public function setRequest(\App\Request $request): void |
||
108 | { |
||
109 | $this->request = $request; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Get fields for the modal search window. |
||
114 | * |
||
115 | * @return \Vtiger_Field_Model[] |
||
116 | */ |
||
117 | public function getFields(): array |
||
118 | { |
||
119 | $fieldsModel = []; |
||
120 | foreach ($this->fields as $fieldName => $data) { |
||
121 | if (isset($data['picklistValuesFunction'])) { |
||
122 | $data['picklistValues'] = $this->{$data['picklistValuesFunction']}($data); |
||
123 | } elseif (isset($data['picklistValues']) && false !== $data['picklistModule']) { |
||
124 | $picklistModule = $data['picklistModule'] ?? $this->moduleName; |
||
125 | foreach ($data['picklistValues'] as $picklistKey => $value) { |
||
126 | $data['picklistValues'][$picklistKey] = \App\Language::translate($value, $picklistModule); |
||
127 | } |
||
128 | } |
||
129 | $fieldModel = \Vtiger_Field_Model::init($this->moduleName, $data, $fieldName); |
||
130 | if (isset($this->modulesFieldsMap[$this->moduleName][$fieldName]) && $this->request->has($this->modulesFieldsMap[$this->moduleName][$fieldName])) { |
||
131 | try { |
||
132 | $uitypeModel = $fieldModel->getUITypeModel(); |
||
133 | $value = $this->request->getByType($this->modulesFieldsMap[$this->moduleName][$fieldName], 'Text'); |
||
134 | $uitypeModel->validate($value, true); |
||
135 | $fieldModel->set('fieldvalue', $uitypeModel->getDBValue($value)); |
||
136 | } catch (\Throwable $th) { |
||
137 | \App\Log::error($th->__toString(), 'RecordCollectors'); |
||
138 | } |
||
139 | } |
||
140 | $fieldsModel[$fieldName] = $fieldModel; |
||
141 | } |
||
142 | return $fieldsModel; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Get fields for the module name. |
||
147 | * |
||
148 | * @param string $moduleName |
||
149 | * |
||
150 | * @return string[] |
||
151 | */ |
||
152 | public function getFieldsModule(string $moduleName): array |
||
153 | { |
||
154 | return $this->modulesFieldsMap[$moduleName]; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Check whether it is active. |
||
159 | * |
||
160 | * @return bool |
||
161 | */ |
||
162 | public function isActive(): bool |
||
163 | { |
||
164 | return \in_array($this->moduleName, $this->allowedModules) && $this->isAvailable(); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Check if product is available. |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function isAvailable(): bool |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Search data function. |
||
179 | * |
||
180 | * @return array |
||
181 | */ |
||
182 | public function search(): array |
||
183 | { |
||
184 | throw new \Api\Core\Exception('no search function'); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Load data. |
||
189 | * |
||
190 | * @return void |
||
191 | */ |
||
192 | public function loadData(): void |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Get fields labels for the module name. |
||
258 | * |
||
259 | * @param string $moduleName |
||
260 | * |
||
261 | * @return string[] |
||
262 | */ |
||
263 | public function getFieldsLabelsByModule(string $moduleName): array |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Get params of collector. |
||
277 | * |
||
278 | * @return array |
||
279 | */ |
||
280 | protected function getParams(): array |
||
286 | } |
||
287 | } |
||
288 |