| Conditions | 11 |
| Paths | 18 |
| Total Lines | 51 |
| Code Lines | 37 |
| 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 |
||
| 50 | public function enableApp($parameters) { |
||
| 51 | $app = $parameters['appid']; |
||
| 52 | if (!in_array($app, $this->apps)) { |
||
| 53 | throw new \InvalidArgumentException("this app cannot be installed"); |
||
| 54 | } |
||
| 55 | $download = $this->request->getParam("download", ""); |
||
| 56 | $branch = $this->request->getParam("branch", ""); |
||
| 57 | $branch = filter_var( |
||
| 58 | $branch, FILTER_SANITIZE_STRING, |
||
| 59 | FILTER_FLAG_STRIP_LOW | |
||
| 60 | FILTER_FLAG_STRIP_HIGH | |
||
| 61 | FILTER_FLAG_STRIP_BACKTICK |
||
| 62 | ); |
||
| 63 | if ($branch === false) { |
||
| 64 | throw new \InvalidArgumentException("invalid branch name"); |
||
| 65 | } |
||
| 66 | if ($branch === "") { |
||
| 67 | $branch = "master"; |
||
| 68 | } |
||
| 69 | if ($download === "true") { |
||
| 70 | $downloadedFile = tempnam(sys_get_temp_dir(), "download-"); |
||
| 71 | if ($downloadedFile === false) { |
||
| 72 | throw new \Exception("could not create temporary file name"); |
||
| 73 | } |
||
| 74 | $downloadedFile .= ".zip"; |
||
| 75 | |||
| 76 | $downloadUrl="https://github.com/owncloud/$app/archive/$branch.zip"; |
||
| 77 | $remoteRessource = fopen($downloadUrl, 'r'); |
||
| 78 | if ($remoteRessource === false) { |
||
| 79 | throw new \Exception("could not download from $downloadUrl"); |
||
| 80 | } |
||
| 81 | |||
| 82 | if (file_put_contents($downloadedFile, $remoteRessource) === false) { |
||
| 83 | throw new \Exception("could not download from $downloadUrl"); |
||
| 84 | } |
||
| 85 | $zip = new \ZipArchive; |
||
| 86 | $res = $zip->open($downloadedFile); |
||
| 87 | if ($res !== true) { |
||
| 88 | throw new \Exception("could not app ZIP archive: $res"); |
||
| 89 | } |
||
| 90 | $appPath = \OC_App::getInstallPath(); |
||
| 91 | if (!is_string($appPath)) { |
||
| 92 | throw new \Exception("no valid installation path found"); |
||
| 93 | } |
||
| 94 | if (!$zip->extractTo("$appPath/$app")) { |
||
| 95 | throw new \Exception("could not extract app ZIP archive"); |
||
| 96 | } |
||
| 97 | $zip->close(); |
||
| 98 | } |
||
| 99 | \OC_App::enable($app); |
||
| 100 | } |
||
| 101 | } |
||
| 102 |