Conditions | 13 |
Paths | 162 |
Total Lines | 185 |
Code Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 namespace Hafiz\Commands\Controller; |
||
67 | public function run(array $params = []) |
||
68 | { |
||
69 | helper(['inflector', 'filesystem']); |
||
70 | |||
71 | $name = array_shift($params); |
||
72 | |||
73 | if (empty($name)) { |
||
74 | $name = CLI::prompt(lang('Recharge.nameController')); |
||
75 | } |
||
76 | |||
77 | if (empty($name)) { |
||
78 | CLI::error(lang('Recharge.badControllerName')); |
||
79 | return; |
||
80 | } |
||
81 | |||
82 | //Class Namespace |
||
83 | $ns = $params['-n'] ?? CLI::getOption('n'); |
||
84 | |||
85 | //Extends Base Class |
||
86 | $base = $params['-b'] ?? CLI::getOption('b'); |
||
87 | |||
88 | //Sub directory after Namespace |
||
89 | $sub = $params['-s'] ?? CLI::getOption('s'); |
||
|
|||
90 | |||
91 | //Rest Controller Style |
||
92 | $is_rest = CLI::getOption('rest'); |
||
93 | |||
94 | /** @var string real path $homepath */ |
||
95 | $homepath = APPPATH; |
||
96 | |||
97 | $package = "App"; |
||
98 | $baseNameSpace = 'use App\Controllers\BaseController;'; |
||
99 | $parentController = 'BaseController'; |
||
100 | |||
101 | //Finding Namespace Location |
||
102 | if (!empty($ns)) { |
||
103 | // Get all namespaces |
||
104 | $namespaces = Services::autoloader()->getNamespace(); |
||
105 | |||
106 | foreach ($namespaces as $namespace => $path) { |
||
107 | if ($namespace === $ns) { |
||
108 | $homepath = realpath(reset($path)); |
||
109 | $package = $ns; |
||
110 | break; |
||
111 | } |
||
112 | } |
||
113 | } else { |
||
114 | $ns = "App"; |
||
115 | } |
||
116 | |||
117 | //Finding Base Class |
||
118 | if (!empty($base)) { |
||
119 | // Get all namespaces |
||
120 | $namespaces = Services::autoloader()->getNamespace(); |
||
121 | |||
122 | foreach ($namespaces as $namespace => $path) { |
||
123 | if ($namespace == "App" || $namespace == $ns) { |
||
124 | $full_path = realpath(reset($path)) . "/Controllers/" . $base . ".php"; |
||
125 | if (file_exists($full_path)) { |
||
126 | $tempObj = new ReflectionClass($namespace . "\Controllers\\" . $base); |
||
127 | $baseNameSpace = 'use ' . $tempObj->getName() . ";"; |
||
128 | $package = $ns; |
||
129 | break; |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | } else { |
||
134 | $base = $parentController; |
||
135 | } |
||
136 | |||
137 | // Always use UTC/GMT so global teams can work together |
||
138 | $fileName = pascalize($name); |
||
139 | |||
140 | // full path |
||
141 | $path = $homepath . '/Controllers/' . $fileName . '.php'; |
||
142 | |||
143 | // Class name should be Pascal case |
||
144 | $name = pascalize($name); |
||
145 | $date = date("d F, Y h:i:s A"); |
||
146 | |||
147 | //Basic Controller Template |
||
148 | $basicTemplate = ''; |
||
149 | |||
150 | //REST Controller Template |
||
151 | $restTemplate = <<<EOD |
||
152 | <?php namespace $ns\Controllers; |
||
153 | |||
154 | $baseNameSpace |
||
155 | use CodeIgniter\RESTful\ResourceController; |
||
156 | |||
157 | /** |
||
158 | * @class $name |
||
159 | * @author CI-Recharge |
||
160 | * @package $package |
||
161 | * @extends ResourceController |
||
162 | * @created $date |
||
163 | */ |
||
164 | |||
165 | |||
166 | class $name extends ResourceController |
||
167 | { |
||
168 | /** |
||
169 | * $name constructor |
||
170 | */ |
||
171 | public function __construct() |
||
172 | { |
||
173 | |||
174 | } |
||
175 | /** |
||
176 | * @return array|string |
||
177 | */ |
||
178 | public function index() |
||
179 | { |
||
180 | |||
181 | return view(''); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @return array|string |
||
186 | */ |
||
187 | public function create() |
||
188 | { |
||
189 | |||
190 | return view(''); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @return array|string |
||
195 | */ |
||
196 | public function store() |
||
197 | { |
||
198 | |||
199 | return ; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param int|null \$id |
||
204 | * @return array|string |
||
205 | */ |
||
206 | public function show(int \$id = null){ |
||
207 | |||
208 | return view(''); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @param int|null \$id |
||
213 | * @return array|string |
||
214 | */ |
||
215 | public function edit(int \$id = null) |
||
216 | { |
||
217 | |||
218 | return view(''); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @param int|null \$id |
||
223 | * @return array|string |
||
224 | */ |
||
225 | public function update(int \$id = null) |
||
226 | { |
||
227 | |||
228 | return ; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @param int|null \$id |
||
233 | * @return array|string |
||
234 | */ |
||
235 | public function delete(int \$id = null) |
||
236 | { |
||
237 | |||
238 | return ; |
||
239 | } |
||
240 | } |
||
241 | |||
242 | EOD; |
||
243 | |||
244 | $template = ($is_rest == true) ? $restTemplate : $basicTemplate; |
||
245 | |||
246 | if (!write_file($path, $template)) { |
||
247 | CLI::error(lang('Recharge.writeError', [$path])); |
||
248 | return; |
||
249 | } |
||
250 | |||
251 | CLI::write('Created file: ' . CLI::color(str_replace($homepath, $ns, $path), 'green')); |
||
252 | } |
||
255 |