| Conditions | 12 |
| Paths | 222 |
| Total Lines | 75 |
| Code Lines | 46 |
| 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; |
||
| 65 | public function run(array $params = []) |
||
| 66 | { |
||
| 67 | helper(['inflector', 'filesystem']); |
||
| 68 | $file = new FileHandler(); |
||
| 69 | |||
| 70 | $name = array_shift($params); |
||
| 71 | $ns = $params['-n'] ?? CLI::getOption('n'); |
||
| 72 | $table = $params['-t'] ?? CLI::getOption('t'); |
||
| 73 | |||
| 74 | if (empty($name)) { |
||
| 75 | $name = CLI::prompt(lang('Recharge.modelName'), null, 'required|string'); |
||
| 76 | } |
||
| 77 | |||
| 78 | if (empty($name)) { |
||
| 79 | CLI::error(lang('Recharge.badName')); |
||
| 80 | return; |
||
| 81 | } |
||
| 82 | |||
| 83 | //namespace locator |
||
| 84 | $nsinfo = $file->getNamespaceInfo($ns, 'App'); |
||
| 85 | |||
| 86 | //class & file name |
||
| 87 | $ns = $nsinfo['ns']; |
||
| 88 | $targetDir = $nsinfo['path'] . '/Models/'; |
||
| 89 | $name = singular(pascalize($name)) . (stripos($name, 'Model') === FALSE ? 'Model' : ''); |
||
| 90 | $filepath = $targetDir . $name . '.php'; |
||
| 91 | |||
| 92 | if ($file->verifyDirectory($filepath)) { |
||
| 93 | //do we have to add table info |
||
| 94 | if (!empty($table)) { |
||
| 95 | $db = new DBHandler(); |
||
| 96 | if ($db->checkTableExist($table)) { |
||
|
|
|||
| 97 | $properties = $db->getModelProperties($table); |
||
| 98 | extract($properties); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | //for soft deleted option |
||
| 103 | $softDelete = 'false'; |
||
| 104 | $deleteField = ''; |
||
| 105 | $validationRules = '[]'; |
||
| 106 | |||
| 107 | if (isset($attributes)) { |
||
| 108 | if (stripos($attributes, 'deleted_at') !== false) { |
||
| 109 | $softDelete = 'true'; |
||
| 110 | $deleteField = "protected \$deletedField = 'deleted_at';"; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | if (isset($rules)) { |
||
| 114 | $validationRules = $rules; |
||
| 115 | } |
||
| 116 | |||
| 117 | $data = [ |
||
| 118 | '{namespace}' => $ns, |
||
| 119 | '{name}' => $name, |
||
| 120 | '{created_at}' => date("d F, Y h:i:s A"), |
||
| 121 | '{attributes}' => $attributes ?? NULL, |
||
| 122 | '{table}' => $table ?? NULL, |
||
| 123 | '{primary_id}' => $primary_id ?? NULL, |
||
| 124 | '{delete_field}' => $deleteField, |
||
| 125 | '{soft_delete}' => $softDelete, |
||
| 126 | '{rules}' => $validationRules, |
||
| 127 | ]; |
||
| 128 | |||
| 129 | //check a directory exist |
||
| 130 | if ($file->checkFileExist($filepath) == true) { |
||
| 131 | $template = $file->renderTemplate('model', $data); |
||
| 132 | |||
| 133 | |||
| 134 | if (!write_file($filepath, $template)) { |
||
| 135 | CLI::error(lang('Recharge.writeError', [$filepath])); |
||
| 136 | return; |
||
| 137 | } |
||
| 138 | |||
| 139 | CLI::write('Created file: ' . CLI::color($filepath, 'green')); |
||
| 140 | } |
||
| 144 |