| Conditions | 17 |
| Paths | 64 |
| Total Lines | 59 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 2 |
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 |
||
| 41 | public function handle() |
||
| 42 | { |
||
| 43 | $extension = $this->argument('extension'); |
||
| 44 | $exception = $this->option('exception'); |
||
| 45 | $output = new BufferedOutput(); |
||
| 46 | if(is_dir(babel_path("Extension/$extension/"))) { |
||
| 47 | if(!$exception) $this->line("\n <bg=red;fg=white> Exception </> : <fg=yellow>An extension named <fg=green>$extension</> already took place, did you mean <fg=green>php artisan bable:update $extension</>?</>\n"); |
||
| 48 | else throw new Exception("\n <bg=red;fg=white> Exception </> : <fg=yellow>An extension named <fg=green>$extension</> already took place, did you mean <fg=green>php artisan bable:update $extension</>?</>\n"); |
||
| 49 | return; |
||
| 50 | } |
||
| 51 | $marketspaceRaw=json_decode(file_get_contents(env("BABEL_MIRROR","https://acm.njupt.edu.cn/babel")."/babel.json"),true); |
||
| 52 | $marketspacePackages=$marketspaceRaw["packages"]; |
||
| 53 | $marketspaceHash=$marketspaceRaw["content-hash"]; |
||
|
|
|||
| 54 | $packageCodeColumn=array_column($marketspacePackages, 'code'); |
||
| 55 | $targetPackage=$marketspacePackages[array_search($extension, $packageCodeColumn)]; |
||
| 56 | // if(!isset($targetPackage["downloadURL"]) || trim($targetPackage["downloadURL"])=="" || is_null($targetPackage["downloadURL"])){ |
||
| 57 | if(!isset($targetPackage["downloadURL"]) || !is_array($targetPackage["downloadURL"]) || is_null($targetPackage["downloadURL"]) || !isset($targetPackage["downloadURL"][0]["url"]) || trim($targetPackage["downloadURL"][0]["url"])=="" || is_null($targetPackage["downloadURL"][0]["url"])){ |
||
| 58 | if(!$exception) $this->line("\n <bg=red;fg=white> Exception </> : <fg=yellow>No available download link.</>\n"); |
||
| 59 | else throw new Exception("\n <bg=red;fg=white> Exception </> : <fg=yellow>No available download link.</>\n"); |
||
| 60 | return; |
||
| 61 | } |
||
| 62 | //todo: check requirements |
||
| 63 | $this->line("Downloading <fg=green>$extension</>(<fg=yellow>{$targetPackage['version']}</>)"); |
||
| 64 | $filename="$extension-".basename($targetPackage["downloadURL"][0]["url"]); |
||
| 65 | file_put_contents(babel_path("Tmp/$filename"),file_get_contents($targetPackage["downloadURL"][0]["url"])); |
||
| 66 | // unzip |
||
| 67 | if(!is_dir(babel_path("Tmp/$extension/"))) mkdir(babel_path("Tmp/$extension/")); |
||
| 68 | try { |
||
| 69 | $zipFile = new ZipFile(); |
||
| 70 | $zipFile->openFile(babel_path("Tmp/$filename"))->extractTo(babel_path("Tmp/$extension/"))->close(); |
||
| 71 | $babelPath=glob_recursive(babel_path("Tmp/$extension/babel.json")); |
||
| 72 | if(empty($babelPath)){ |
||
| 73 | if(!$exception) $this->line("\n <bg=red;fg=white> Exception </> : <fg=yellow>There exists no <fg=green>babel.json</> files.</>\n"); |
||
| 74 | else throw new Exception("\n <bg=red;fg=white> Exception </> : <fg=yellow>No available download link.</>\n"); |
||
| 75 | return; |
||
| 76 | } else { |
||
| 77 | $babelPath=dirname($babelPath[0]); |
||
| 78 | // if(is_dir(babel_path("Extension/$extension/"))) mkdir(babel_path("Extension/$extension/")); |
||
| 79 | rename($babelPath,babel_path("Extension/$extension/")); |
||
| 80 | } |
||
| 81 | } catch(\PhpZip\Exception\ZipException $e) { |
||
| 82 | $this->postProc($filename,$extension); |
||
| 83 | if(!$exception) $this->line("\n <bg=red;fg=white> Exception </> : <fg=yellow>An error occoured when extract <fg=green>$extension</>.</>\n"); |
||
| 84 | else { |
||
| 85 | throw new Exception("\n <bg=red;fg=white> Exception </> : <fg=yellow>An error occoured when extract <fg=green>$extension</>.</>\n"); |
||
| 86 | } |
||
| 87 | return; |
||
| 88 | } catch(Exception $e){ |
||
| 89 | $this->postProc($filename,$extension); |
||
| 90 | if(!$exception) $this->line("\n <bg=red;fg=white> Exception </> : <fg=yellow>An error occoured when extract <fg=green>$extension</>.</>\n"); |
||
| 91 | else { |
||
| 92 | throw new Exception("\n <bg=red;fg=white> Exception </> : <fg=yellow>An error occoured when extract <fg=green>$extension</>.</>\n"); |
||
| 93 | } |
||
| 94 | return; |
||
| 95 | } |
||
| 96 | $this->postProc($filename,$extension); |
||
| 97 | $this->line("Downloaded <fg=green>$extension</>(<fg=yellow>{$targetPackage['version']}</>)"); |
||
| 98 | $this->call("babel:install", ['extension' => $extension]); |
||
| 99 | $output->fetch(); |
||
| 100 | } |
||
| 122 |