| Conditions | 8 |
| Paths | 22 |
| Total Lines | 53 |
| Code Lines | 29 |
| 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 namespace Hafiz\Commands; |
||
| 68 | public function run(array $params = []) |
||
| 69 | { |
||
| 70 | helper(['inflector', 'filesystem']); |
||
| 71 | $file = new FileHandler(); |
||
| 72 | |||
| 73 | $name = array_shift($params); |
||
| 74 | $ns = $params['-n'] ?? CLI::getOption('n'); |
||
| 75 | $table = $params['-t'] ?? CLI::getOption('t'); |
||
| 76 | |||
| 77 | if (empty($name)) { |
||
| 78 | $name = CLI::prompt(lang('Recharge.nameEntity'), null, 'required|string'); |
||
| 79 | } |
||
| 80 | |||
| 81 | if (empty($name)) { |
||
| 82 | CLI::error(lang('Recharge.badName')); |
||
| 83 | return; |
||
| 84 | } |
||
| 85 | |||
| 86 | //namespace locator |
||
| 87 | $nsinfo = $file->getNamespaceInfo($ns, 'App'); |
||
| 88 | |||
| 89 | //class & file name |
||
| 90 | $name = singular(pascalize($name)); |
||
| 91 | $ns = $nsinfo['ns']; |
||
| 92 | $targetDir = $nsinfo['path'] . '/Entities/'; |
||
| 93 | $filepath = $targetDir . $name . '.php'; |
||
| 94 | |||
| 95 | if ($file->verifyDirectory($filepath)) { |
||
| 96 | |||
| 97 | //do we have to add table info |
||
| 98 | if (!empty($table)) { |
||
| 99 | $db = new DBHandler(); |
||
| 100 | |||
| 101 | if ($db->checkTableExist($table)) { |
||
|
|
|||
| 102 | $properties = $db->getEntityProperties($table); |
||
| 103 | extract($properties); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | $data = ['{namespace}' => $ns, '{name}' => $name, '{created_at}' => date("d F, Y h:i:s A"), |
||
| 108 | '{attributes}' => $attributes ?? NULL, '{dates}' => $dates ?? NULL, '{casts}' => $casts ?? NULL]; |
||
| 109 | |||
| 110 | //check a directory exist |
||
| 111 | if ($file->checkFileExist($filepath) == true) { |
||
| 112 | $template = $file->renderTemplate('entity', $data); |
||
| 113 | |||
| 114 | |||
| 115 | if (!write_file($filepath, $template)) { |
||
| 116 | CLI::error(lang('Recharge.writeError', [$filepath])); |
||
| 117 | return; |
||
| 118 | } |
||
| 119 | |||
| 120 | CLI::write('Created file: ' . CLI::color($filepath, 'green')); |
||
| 121 | } |
||
| 125 |