| Conditions | 13 |
| Paths | 162 |
| Total Lines | 215 |
| Code Lines | 64 |
| 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\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 = <<<EOD |
||
| 149 | <?php namespace $ns\Controllers; |
||
| 150 | |||
| 151 | $baseNameSpace |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @class $name |
||
| 155 | * @author CI-Recharge |
||
| 156 | * @package $package |
||
| 157 | * @extend $base |
||
| 158 | * @created $date |
||
| 159 | */ |
||
| 160 | |||
| 161 | |||
| 162 | class $name extends $base |
||
| 163 | { |
||
| 164 | /** |
||
| 165 | * $name constructor |
||
| 166 | */ |
||
| 167 | public function __construct() |
||
| 168 | { |
||
| 169 | |||
| 170 | } |
||
| 171 | |||
| 172 | public function index() |
||
| 173 | { |
||
| 174 | echo 'Hello World!'; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | EOD; |
||
| 179 | |||
| 180 | //REST Controller Template |
||
| 181 | $restTemplate = <<<EOD |
||
| 182 | <?php namespace $ns\Controllers; |
||
| 183 | |||
| 184 | $baseNameSpace |
||
| 185 | use CodeIgniter\RESTful\ResourceController; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @class $name |
||
| 189 | * @author CI-Recharge |
||
| 190 | * @package $package |
||
| 191 | * @extends ResourceController |
||
| 192 | * @created $date |
||
| 193 | */ |
||
| 194 | |||
| 195 | |||
| 196 | class $name extends ResourceController |
||
| 197 | { |
||
| 198 | /** |
||
| 199 | * $name constructor |
||
| 200 | */ |
||
| 201 | public function __construct() |
||
| 202 | { |
||
| 203 | |||
| 204 | } |
||
| 205 | /** |
||
| 206 | * @return array|string |
||
| 207 | */ |
||
| 208 | public function index() |
||
| 209 | { |
||
| 210 | |||
| 211 | return view(''); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return array|string |
||
| 216 | */ |
||
| 217 | public function create() |
||
| 218 | { |
||
| 219 | |||
| 220 | return view(''); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return array|string |
||
| 225 | */ |
||
| 226 | public function store() |
||
| 227 | { |
||
| 228 | |||
| 229 | return ; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param int|null \$id |
||
| 234 | * @return array|string |
||
| 235 | */ |
||
| 236 | public function show(int \$id = null){ |
||
| 237 | |||
| 238 | return view(''); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param int|null \$id |
||
| 243 | * @return array|string |
||
| 244 | */ |
||
| 245 | public function edit(int \$id = null) |
||
| 246 | { |
||
| 247 | |||
| 248 | return view(''); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param int|null \$id |
||
| 253 | * @return array|string |
||
| 254 | */ |
||
| 255 | public function update(int \$id = null) |
||
| 256 | { |
||
| 257 | |||
| 258 | return ; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param int|null \$id |
||
| 263 | * @return array|string |
||
| 264 | */ |
||
| 265 | public function delete(int \$id = null) |
||
| 266 | { |
||
| 267 | |||
| 268 | return ; |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | EOD; |
||
| 273 | |||
| 274 | $template = ($is_rest == true) ? $restTemplate : $basicTemplate; |
||
| 275 | |||
| 276 | if (!write_file($path, $template)) { |
||
| 277 | CLI::error(lang('Recharge.writeError', [$path])); |
||
| 278 | return; |
||
| 279 | } |
||
| 280 | |||
| 281 | CLI::write('Created file: ' . CLI::color(str_replace($homepath, $ns, $path), 'green')); |
||
| 282 | } |
||
| 285 |