| Conditions | 7 |
| Paths | 7 |
| Total Lines | 52 |
| Code Lines | 23 |
| 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 |
||
| 28 | public function getConfiguration(string $configurationFilePath): Configuration |
||
| 29 | { |
||
| 30 | $configurationFilePath = PathUtils::getAbsolutePath($configurationFilePath); |
||
| 31 | |||
| 32 | if (!is_file($configurationFilePath) || !is_readable($configurationFilePath)) |
||
| 33 | { |
||
| 34 | throw new Exception("Configuration file {$configurationFilePath} is not a readable file."); |
||
| 35 | } |
||
| 36 | |||
| 37 | if (($json = file_get_contents($configurationFilePath)) === false) |
||
| 38 | { |
||
| 39 | throw new Exception("Failed to read config file {$configurationFilePath}."); |
||
| 40 | } |
||
| 41 | |||
| 42 | if (($array = json_decode($json, true)) === null) |
||
| 43 | { |
||
| 44 | throw new Exception("Malformed configuration file: {$configurationFilePath}."); |
||
| 45 | } |
||
| 46 | |||
| 47 | |||
| 48 | // default values |
||
| 49 | $array = ArrayUtils::merge([ |
||
| 50 | 'path' => dirname($configurationFilePath), |
||
| 51 | 'identity' => sprintf('%s@%s', get_current_user(), gethostname()), |
||
| 52 | ], $array); |
||
| 53 | |||
| 54 | |||
| 55 | try |
||
| 56 | { |
||
| 57 | $className = static::CONFIG_CLASS; |
||
| 58 | |||
| 59 | /** @var Configuration $configuration */ |
||
| 60 | $configuration = new $className(); |
||
| 61 | $configuration->exchangeArray($array); |
||
| 62 | } |
||
| 63 | catch (\InvalidArgumentException $exception) |
||
| 64 | { |
||
| 65 | throw new ConfigurationException("In file {$configurationFilePath}", 0, $exception); |
||
| 66 | } |
||
| 67 | |||
| 68 | |||
| 69 | // validate configuration |
||
| 70 | $constraintViolations = $this->getValidator()->validate($configuration); |
||
| 71 | if ($constraintViolations->count()) |
||
| 72 | { |
||
| 73 | $violation = $constraintViolations->get(0); |
||
| 74 | |||
| 75 | throw new ConfigurationException("{$configurationFilePath}: {$violation->getPropertyPath()} - {$violation->getMessage()}"); |
||
| 76 | } |
||
| 77 | |||
| 78 | return $configuration; |
||
| 79 | } |
||
| 80 | |||
| 89 |