| Conditions | 8 |
| Paths | 9 |
| Total Lines | 61 |
| 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 |
||
| 35 | public function __invoke($type, $classname) |
||
| 36 | { |
||
| 37 | if ($classname === null) { |
||
| 38 | $this->stdio->errln( |
||
| 39 | '<<red>>Classname is needed<<reset>>' |
||
| 40 | ); |
||
| 41 | $this->stdio->errln( |
||
| 42 | ' eg, generate migration CreateUserTable' |
||
| 43 | ); |
||
| 44 | return Status::USAGE; |
||
| 45 | } |
||
| 46 | |||
| 47 | $migration_path = $this->config->item('migration_path'); |
||
| 48 | $migration_type = $this->config->item('migration_type'); |
||
| 49 | |||
| 50 | $file_path = $this->generateFilename( |
||
| 51 | $migration_path, $migration_type, $classname |
||
| 52 | ); |
||
| 53 | |||
| 54 | // check file exist |
||
| 55 | if (file_exists($file_path)) { |
||
| 56 | $this->stdio->errln( |
||
| 57 | "<<red>>The file \"$file_path\" already exists<<reset>>" |
||
| 58 | ); |
||
| 59 | return Status::FAILURE; |
||
| 60 | } |
||
| 61 | |||
| 62 | // check class exist |
||
| 63 | foreach (glob($migration_path . '*_*.php') as $file) { |
||
| 64 | $name = basename($file, '.php'); |
||
| 65 | if (preg_match($migration_type === 'timestamp' ? '/^\d{14}_(\w+)$/' : '/^\d{3}_(\w+)$/', $name, $match)) { |
||
| 66 | if (strtolower($match[1]) === strtolower($classname)) { |
||
| 67 | $this->stdio->errln( |
||
| 68 | "<<red>>The Class \"$match[1]\" already exists<<reset>>" |
||
| 69 | ); |
||
| 70 | return Status::FAILURE; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | $template = file_get_contents(__DIR__ . '/templates/Migration.txt'); |
||
| 76 | $search = [ |
||
| 77 | '@@classname@@', |
||
| 78 | '@@date@@', |
||
| 79 | ]; |
||
| 80 | $replace = [ |
||
| 81 | $classname, |
||
| 82 | date('Y/m/d H:i:s'), |
||
| 83 | ]; |
||
| 84 | $output = str_replace($search, $replace, $template); |
||
| 85 | $generated = @file_put_contents($file_path, $output, LOCK_EX); |
||
| 86 | |||
| 87 | if ($generated !== false) { |
||
| 88 | $this->stdio->outln('<<green>>Generated: ' . $file_path . '<<reset>>'); |
||
| 89 | } else { |
||
| 90 | $this->stdio->errln( |
||
| 91 | "<<red>>Can't write to \"$file_path\"<<reset>>" |
||
| 92 | ); |
||
| 93 | return Status::FAILURE; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 123 |