| Conditions | 7 |
| Paths | 7 |
| Total Lines | 53 |
| Code Lines | 25 |
| 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 |
||
| 11 | public function getConfiguration(string $configurationFilePath) |
||
| 12 | { |
||
| 13 | $configurationFilePath = PathUtils::getAbsolutePath($configurationFilePath); |
||
| 14 | |||
| 15 | if (!is_file($configurationFilePath) || !is_readable($configurationFilePath)) |
||
| 16 | { |
||
| 17 | throw new Exception("Configuration file {$configurationFilePath} is not a readable file."); |
||
| 18 | } |
||
| 19 | |||
| 20 | if (($json = file_get_contents($configurationFilePath)) === false) |
||
| 21 | { |
||
| 22 | throw new Exception("Failed to read config file {$configurationFilePath}."); |
||
| 23 | } |
||
| 24 | |||
| 25 | if (($array = json_decode($json, true)) === null) |
||
| 26 | { |
||
| 27 | throw new Exception("Malformed configuration file: {$configurationFilePath}."); |
||
| 28 | } |
||
| 29 | |||
| 30 | |||
| 31 | // default values |
||
| 32 | $array = ArrayUtils::merge([ |
||
| 33 | 'path' => dirname($configurationFilePath), |
||
| 34 | 'identity' => sprintf('%s@%s', get_current_user(), gethostname()), |
||
| 35 | ], $array); |
||
| 36 | |||
| 37 | |||
| 38 | try |
||
| 39 | { |
||
| 40 | $configuration = new Configuration(); |
||
| 41 | $configuration->exchangeArray($array); |
||
| 42 | } |
||
| 43 | catch (\InvalidArgumentException $exception) |
||
| 44 | { |
||
| 45 | throw new ConfigurationException('', 0, $exception); |
||
| 46 | } |
||
| 47 | |||
| 48 | |||
| 49 | // validate configuration |
||
| 50 | $validator = Validation::createValidatorBuilder() |
||
| 51 | ->addMethodMapping('loadValidatorMetadata') |
||
| 52 | ->getValidator(); |
||
| 53 | |||
| 54 | $constraintViolations = $validator->validate($configuration); |
||
| 55 | if ($constraintViolations->count()) |
||
| 56 | { |
||
| 57 | $violation = $constraintViolations->get(0); |
||
| 58 | |||
| 59 | throw new ConfigurationException("{$violation->getPropertyPath()} - {$violation->getMessage()}"); |
||
| 60 | } |
||
| 61 | |||
| 62 | return $configuration; |
||
| 63 | } |
||
| 64 | } |
||
| 65 |