Total Complexity | 40 |
Total Lines | 326 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like PBXInstaller 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 PBXInstaller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class PBXInstaller extends Injectable |
||
34 | { |
||
35 | /** |
||
36 | * Access to the /etc/inc/mikopbx-settings.json values |
||
37 | * |
||
38 | * @var \Phalcon\Config\Config |
||
39 | */ |
||
40 | private Config $config; |
||
41 | |||
42 | private array $valid_disks = []; |
||
43 | private array $selected_disk = ['size' => 0, 'id' => '']; |
||
44 | private string $target_disk = ''; |
||
45 | |||
46 | // File pointer |
||
47 | private $fp; |
||
48 | |||
49 | /** |
||
50 | * PBXInstaller constructor. |
||
51 | * Initiates the installation process. |
||
52 | */ |
||
53 | public function __construct() |
||
54 | { |
||
55 | |||
56 | $this->config = Di::getDefault()->getShared(ConfigProvider::SERVICE_NAME); |
||
57 | |||
58 | $this->fp = fopen('php://stdin', 'rb'); |
||
59 | |||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Initiates the installation steps. |
||
64 | */ |
||
65 | public function run(): void |
||
66 | { |
||
67 | $this->scanAllHdd(); |
||
68 | if ($this->processValidDisks()){ |
||
69 | $this->promptForTargetDisk(); |
||
70 | if ($this->confirmInstallation()) { |
||
71 | $this->proceedInstallation(); |
||
72 | } |
||
73 | } |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Scans all connected HDDs. |
||
78 | */ |
||
79 | private function scanAllHdd(): void |
||
80 | { |
||
81 | $storage = new Storage(); |
||
82 | $all_hdd = $storage->getAllHdd(); |
||
83 | foreach ($all_hdd as $disk) { |
||
84 | $this->processDisk($disk); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Process each disk and save valid ones. |
||
90 | * |
||
91 | * @param array $disk Information about the disk |
||
92 | */ |
||
93 | private function processDisk(array $disk): void |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Process the valid disks and select one for installation. |
||
127 | */ |
||
128 | private function processValidDisks(): bool |
||
129 | { |
||
130 | // If no valid disks were found, print a message and sleep for 3 seconds, then return 0 |
||
131 | if (count($this->valid_disks) === 0) { |
||
132 | SystemMessages::echoWithSyslog(PHP_EOL." " . Util::translate('Valid disks not found...') . " ".PHP_EOL); |
||
133 | sleep(3); |
||
134 | return false; |
||
135 | } |
||
136 | |||
137 | // If valid disks were found, print prompts for the user to select a disk |
||
138 | echo "\n " . Util::translate('Select the drive to install the system.') . ' '; |
||
139 | echo "\n " . Util::translate('Selected disk:') . "\033[33;1m [{$this->selected_disk['id']}] \033[0m \n\n"; |
||
140 | echo "\n " . Util::translate('Valid disks are:') . " \n\n"; |
||
141 | |||
142 | // Print each valid disk |
||
143 | foreach ($this->valid_disks as $disk) { |
||
144 | echo $disk; |
||
145 | } |
||
146 | echo "\n"; |
||
147 | return true; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Prompt the user to select a target disk. |
||
152 | * |
||
153 | * @return void The selected disk id |
||
154 | */ |
||
155 | private function promptForTargetDisk(): void |
||
165 | |||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Confirm the installation from the user. |
||
170 | */ |
||
171 | private function confirmInstallation(): bool |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Start the installation process. |
||
198 | */ |
||
199 | private function proceedInstallation(): void |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Converting the disk layout |
||
220 | * @param $disk |
||
221 | * @return void |
||
222 | */ |
||
223 | private function convertDiscLayout($disk):void |
||
224 | { |
||
225 | if (!file_exists($disk)) { |
||
226 | $disk = "/dev/$disk"; |
||
227 | } |
||
228 | if (!file_exists($disk)) { |
||
229 | return; |
||
230 | } |
||
231 | $gDiskPath = Util::which('gdisk'); |
||
232 | if (empty($gDiskPath)) { |
||
233 | return; |
||
234 | } |
||
235 | $partedPath = Util::which('parted'); |
||
236 | $command = "$partedPath $disk print | grep 'Partition Table' | awk '{print $3}'"; |
||
237 | exec($command, $partitionTypeOutput, $partedStatus); |
||
238 | if ($partedStatus !== 0 || empty($partitionTypeOutput)) { |
||
239 | return; |
||
240 | } |
||
241 | $partitionType = trim($partitionTypeOutput[0]); |
||
242 | if ($partitionType === "msdos") { |
||
243 | echo " - Converting from MBR to GPT...\n"; |
||
244 | $echoPath = Util::which('echo'); |
||
245 | $gDiskCommand = "$echoPath -e \"w\\nY\\n\" | $gDiskPath $disk > /dev/null 2>&1"; |
||
246 | exec($gDiskCommand, $gDiskOutput, $gDiskStatus); |
||
247 | if ($gDiskStatus === 0) { |
||
248 | echo " - The conversion to GPT has been completed successfully.\n"; |
||
249 | } else { |
||
250 | echo " - Error converting to GPT.\n"; |
||
251 | } |
||
252 | } |
||
253 | } |
||
254 | |||
255 | |||
256 | /** |
||
257 | * Unmount the partitions of the selected disk. |
||
258 | */ |
||
259 | private function unmountPartitions(): void |
||
260 | { |
||
261 | echo Util::translate(" - Unmounting partitions...").PHP_EOL; |
||
262 | $grep = Util::which('grep'); |
||
263 | $awk = Util::which('awk'); |
||
264 | $mount = Util::which('mount'); |
||
265 | $umount = Util::which('umount'); |
||
266 | |||
267 | // Get all mounted partitions |
||
268 | $mnt_dirs = []; |
||
269 | Processes::mwExec("$mount | $grep '^/dev/$this->target_disk' | $awk '{print $3}'", $mnt_dirs); |
||
270 | foreach ($mnt_dirs as $mnt) { |
||
271 | // Terminate all related processes. |
||
272 | Processes::mwExec("/sbin/shell_functions.sh killprocesses '$mnt' -TERM 0;"); |
||
273 | // Unmount. |
||
274 | Processes::mwExec("$umount $mnt"); |
||
275 | } |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Unpack the image to the target disk. |
||
280 | */ |
||
281 | private function unpackImage(): void |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Mount the storage partition. |
||
295 | */ |
||
296 | private function mountStorage(): void |
||
297 | { |
||
298 | // Connect the disk for data storage. |
||
299 | Storage::selectAndConfigureStorageDisk(false,true); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Copy the configuration to the target disk. |
||
304 | */ |
||
305 | private function copyConfiguration():void |
||
359 | } |
||
360 | } |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.