| Conditions | 6 |
| Paths | 10 |
| Total Lines | 55 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 21 | public function handle(): void |
||
| 22 | { |
||
| 23 | // Check if package is listed in store |
||
| 24 | $name = $this->getArgument('name'); |
||
| 25 | $drivers = collect($this->kernel->getDrivers()); |
||
| 26 | |||
| 27 | $driver = $drivers->first(function ($item) use ($name) { |
||
| 28 | return $item['name'] === $name; |
||
| 29 | }); |
||
| 30 | |||
| 31 | if ($driver === null) { |
||
| 32 | $this->error('"'.$name.'" is not found in the official drivers list.'); |
||
| 33 | $package = $this->input('Type composer package name if you know which one you want to install'); |
||
| 34 | } else { |
||
| 35 | // If driver is not official we should ask user to confirm installation |
||
| 36 | if ($driver['official'] !== true) { |
||
| 37 | if (!$this->confirm('"'.$name.'" is not official. Still want to install?')) { |
||
| 38 | exit; |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | $package = $driver['package']; |
||
| 43 | } |
||
| 44 | |||
| 45 | // Determine if package is already installed |
||
| 46 | $composer = json_decode($this->filesystem()->read('composer.json'), true); |
||
| 47 | $installed = collect($composer['require']) |
||
| 48 | ->merge($composer['require-dev']) |
||
| 49 | ->search(function ($_, $item) use ($package) { |
||
| 50 | return hash_equals($item, $package); |
||
| 51 | }); |
||
| 52 | |||
| 53 | if ($installed !== false) { |
||
| 54 | $this->error('Driver is already installed.'); |
||
| 55 | |||
| 56 | return; |
||
| 57 | } |
||
| 58 | |||
| 59 | // Install driver |
||
| 60 | $this->info('Installing driver...'); |
||
| 61 | |||
| 62 | $process = new Process('composer require '.$package, $this->kernel->getPath()); |
||
| 63 | $output = ''; |
||
| 64 | $result = $process->run(function ($_, $line) use (&$output) { |
||
| 65 | $output .= $line; |
||
| 66 | }); |
||
| 67 | |||
| 68 | if ($result !== 0) { |
||
| 69 | $this->error($output); |
||
| 70 | $this->error('Installation failed.'); |
||
| 71 | exit($result); |
||
| 72 | } |
||
| 73 | |||
| 74 | $this->success('Driver installed.'); |
||
| 75 | } |
||
| 76 | } |
||
| 77 |