Conditions | 10 |
Paths | 7 |
Total Lines | 46 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
71 | protected function evaluateConfigValue( |
||
72 | string $configOption, |
||
73 | array $config, |
||
74 | array $configValidation |
||
75 | ) { |
||
76 | |||
77 | if (!array_key_exists($configOption, $configValidation)) { |
||
78 | throw new Exception(sprintf('Unknown configuration option "%s"', $configOption)); |
||
79 | } |
||
80 | |||
81 | $optionValidation = $configValidation[$configOption]; |
||
82 | if (!array_key_exists($configOption, $config)) { |
||
83 | if (array_key_exists('required', $optionValidation) && $optionValidation['required']) { |
||
84 | throw new Exception(sprintf( |
||
85 | 'Configuration is missing required option "%s"', |
||
86 | $configOption |
||
87 | )); |
||
88 | } |
||
89 | if (array_key_exists('default', $optionValidation)) { |
||
90 | return $optionValidation['default']; |
||
91 | } |
||
92 | return ''; |
||
93 | } |
||
94 | |||
95 | $optionValue = $config[$configOption]; |
||
96 | if (array_key_exists('type', $optionValidation) |
||
97 | && gettype($optionValue) !== $optionValidation['type']) { |
||
98 | throw new Exception(sprintf( |
||
99 | 'Validation of option "%s" failed: expected value of type "%s", but found "%s"!', |
||
100 | $configOption, |
||
101 | $optionValidation['type'], |
||
102 | gettype($optionValue) |
||
103 | )); |
||
104 | } |
||
105 | |||
106 | if (array_key_exists('allowedValues', $optionValidation) |
||
107 | && !in_array($optionValue, $optionValidation['allowedValues'], true)) { |
||
108 | throw new Exception(sprintf( |
||
109 | 'Validation of option "%s" failed: value "%s" is not one of the allowed values "%s"!', |
||
110 | $configOption, |
||
111 | $optionValue, |
||
112 | implode('", "', $optionValidation['allowedValues']) |
||
113 | )); |
||
114 | } |
||
115 | |||
116 | return $optionValue; |
||
117 | } |
||
139 |