Conditions | 11 |
Paths | 26 |
Total Lines | 41 |
Code Lines | 34 |
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 |
||
23 | public function loadConfig(string $path, array $extra =[]):?array |
||
24 | { |
||
25 | $data=null; |
||
26 | if (!file_exists($path)) { |
||
27 | $path =$this->resolve($path); |
||
28 | } |
||
29 | if ($path) { |
||
30 | $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION)); |
||
31 | switch ($ext) { |
||
32 | case 'yml': |
||
33 | case 'yaml': |
||
34 | if (function_exists('yaml_parse')) { |
||
35 | $name = 'yaml_parse'; |
||
36 | } elseif (class_exists('Spyc')) { |
||
37 | $name = 'Spyc::YAMLLoadString'; |
||
38 | } else { |
||
39 | throw new YamlException("parse yaml config error : missing yaml extension or spyc", 1); |
||
40 | } |
||
41 | $content = file_get_contents($path); |
||
42 | $content =$this->parseValue($content, $extra); |
||
43 | $data = \call_user_func_array($name, [$content]); |
||
44 | break; |
||
45 | case 'php': |
||
46 | $data = include $path; |
||
47 | break; |
||
48 | case 'ini': |
||
49 | $content = file_get_contents($path); |
||
50 | $content =$this->parseValue($content, $extra); |
||
51 | $data = \parse_ini_string($content, true); |
||
52 | break; |
||
53 | case 'json': |
||
54 | default: |
||
55 | $content = file_get_contents($path); |
||
56 | $content =$this->parseValue($content, $extra); |
||
57 | $data = json_decode($content, true); |
||
58 | if (json_last_error()!==JSON_ERROR_NONE) { |
||
59 | throw new JSONException(json_last_error()); |
||
60 | } |
||
61 | } |
||
62 | } |
||
63 | return $data; |
||
64 | } |
||
136 |