Conditions | 6 |
Paths | 14 |
Total Lines | 53 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 namespace Hafiz\Commands; |
||
62 | public function run(array $params = []) |
||
63 | { |
||
64 | /** |
||
65 | * Calling all Library and helpers |
||
66 | */ |
||
67 | helper(['inflector', 'filesystem']); |
||
68 | $file = new FileHandler(); |
||
69 | |||
70 | /** |
||
71 | * Input Configuration name from input |
||
72 | */ |
||
73 | $name = array_shift($params); |
||
74 | |||
75 | //if namespace is given |
||
76 | $ns = $params['-n'] ?? CLI::getOption('n'); |
||
77 | |||
78 | if (empty($name)) |
||
79 | $name = CLI::prompt(lang('Recharge.configName'), null, 'required|string'); |
||
80 | |||
81 | if (empty($name)) { |
||
82 | CLI::error(lang('Recharge.badName')); |
||
83 | return; |
||
84 | } |
||
85 | |||
86 | //namespace locator |
||
87 | $nsinfo = $file->getNamespaceInfo($ns, 'Config'); |
||
88 | |||
89 | //class & file name |
||
90 | $name = pascalize($name); |
||
91 | |||
92 | //target Dir |
||
93 | if ($nsinfo['default']) { |
||
94 | $targetDir = $nsinfo['path'] . '/'; |
||
95 | $ns = $nsinfo['ns']; |
||
96 | } else { |
||
97 | $targetDir = $nsinfo['path'] . '/Config/'; |
||
98 | $ns = $nsinfo['ns'] . '\Config'; |
||
99 | } |
||
100 | |||
101 | $data = ['{namespace}' => $ns, '{name}' => $name, '{created_at}' => date("d F, Y h:i:s A")]; |
||
102 | |||
103 | $filepath = $targetDir . $name . '.php'; |
||
104 | |||
105 | //check a directory exist |
||
106 | if ($file->checkFileExist($filepath) == true) { |
||
|
|||
107 | $template = $file->renderTemplate('config', $data); |
||
108 | |||
109 | if (!write_file($filepath, $template)) { |
||
110 | CLI::error(lang('Recharge.writeError', [$filepath])); |
||
111 | return; |
||
112 | } |
||
113 | |||
114 | CLI::write('Created file: ' . CLI::color($filepath, 'green')); |
||
115 | } |
||
118 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.