| Conditions | 11 |
| Paths | 23 |
| Total Lines | 50 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 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 |
||
| 53 | private function initRepositories(): void |
||
| 54 | { |
||
| 55 | foreach ($this->config[self::CONFIG_ELEMENT_REPOSITORIES] as $repoName => $repoConfig) { |
||
| 56 | if (!array_key_exists('type', $repoConfig)) { |
||
| 57 | $repoType = Repository::TYPE_FILE; |
||
| 58 | } else { |
||
| 59 | $repoType = $repoConfig['type']; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** @todo these three checks can be condensed */ |
||
| 63 | if (!array_key_exists(self::CONFIG_ELEMENT_NAME, $repoConfig)) { |
||
| 64 | throw new \RuntimeException('No field for repository "' . $repoName . '" with value ' . self::CONFIG_ELEMENT_NAME . ' found. Fields given are: ' . implode(', ', array_keys($repoConfig)) . '.'); |
||
| 65 | } |
||
| 66 | |||
| 67 | if (!array_key_exists(self::CONFIG_ELEMENT_DESCRIPTION, $repoConfig)) { |
||
| 68 | throw new \RuntimeException('No field for repository "' . $repoName . '" with value ' . self::CONFIG_ELEMENT_DESCRIPTION . ' found. Fields given are: ' . implode(', ', array_keys($repoConfig)) . '.'); |
||
| 69 | } |
||
| 70 | |||
| 71 | if (!array_key_exists(self::CONFIG_ELEMENT_CONFIG, $repoConfig)) { |
||
| 72 | throw new \RuntimeException('No field for repository "' . $repoName . '" with value ' . self::CONFIG_ELEMENT_CONFIG . ' found. Fields given are: ' . implode(', ', array_keys($repoConfig)) . '.'); |
||
| 73 | } |
||
| 74 | |||
| 75 | // @todo this should be moved to a repository factory |
||
| 76 | |||
| 77 | if ($repoType == Repository::TYPE_API) { |
||
| 78 | $repoConfig['config']['endpoint'] = str_replace('forrest.startwind.io/api', 'api.forrestcli.com', $repoConfig['config']['endpoint']); |
||
| 79 | if (array_key_exists('password', $repoConfig['config'])) { |
||
| 80 | $newRepo = new EditableApiRepository($repoConfig['config']['endpoint'], $repoConfig['name'], $repoConfig['description'], $this->client); |
||
| 81 | $newRepo->setPassword($repoConfig['config']['password']); |
||
| 82 | } else { |
||
| 83 | $newRepo = new ApiRepository($repoConfig['config']['endpoint'], $repoConfig['name'], $repoConfig['description'], $this->client); |
||
| 84 | } |
||
| 85 | } else { |
||
| 86 | $adapterIdentifier = $repoConfig[self::CONFIG_ELEMENT_ADAPTER]; |
||
| 87 | $adapter = AdapterFactory::getAdapter($adapterIdentifier, $repoConfig['config'], $this->client); |
||
| 88 | if ($adapter instanceof EditableAdapter && $adapter->isEditable()) { |
||
| 89 | $newRepo = new EditableFileRepository($adapter, $repoConfig['name'], $repoConfig['description']); |
||
| 90 | } else { |
||
| 91 | $newRepo = new FileRepository($adapter, $repoConfig['name'], $repoConfig['description']); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | try { |
||
| 96 | $newRepo->assertHealth(); |
||
| 97 | } catch (\Exception $exception) { |
||
| 98 | ForrestLogger::error("Unable to load repository " . $repoName . ": " . $exception->getMessage() . '.'); |
||
| 99 | continue; |
||
| 100 | } |
||
| 101 | |||
| 102 | $this->repositories[$repoName] = $newRepo; |
||
| 103 | } |
||
| 126 |