| Conditions | 6 |
| Paths | 7 |
| Total Lines | 74 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 60 | public function handle(): int |
||
| 61 | { |
||
| 62 | $model = $this->resolveModel(); |
||
| 63 | $model = $this->sanitizeName($model); |
||
| 64 | $foreignColumn = 'love_reacter_id'; |
||
| 65 | $isForeignColumnNullable = boolval($this->option('nullable')); |
||
| 66 | |||
| 67 | if (!class_exists($model)) { |
||
| 68 | $this->error(sprintf( |
||
| 69 | 'Model `%s` not exists.', |
||
| 70 | $model |
||
| 71 | )); |
||
| 72 | |||
| 73 | return 1; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** @var \Illuminate\Database\Eloquent\Model $model */ |
||
| 77 | $model = new $model(); |
||
| 78 | |||
| 79 | if ($this->isModelInvalid($model)) { |
||
| 80 | $this->error(sprintf( |
||
| 81 | 'Model `%s` does not implements Reacterable interface.', |
||
| 82 | get_class($model) |
||
| 83 | )); |
||
| 84 | |||
| 85 | return 1; |
||
| 86 | } |
||
| 87 | |||
| 88 | $table = $model->getTable(); |
||
| 89 | $referencedModel = new Reacter(); |
||
| 90 | $referencedTable = $referencedModel->getTable(); |
||
| 91 | $referencedColumn = $referencedModel->getKeyName(); |
||
| 92 | |||
| 93 | if (!Schema::hasTable($referencedTable)) { |
||
| 94 | $this->error(sprintf( |
||
| 95 | 'Referenced table `%s` does not exists in database.', |
||
| 96 | $referencedTable |
||
| 97 | )); |
||
| 98 | |||
| 99 | return 1; |
||
| 100 | } |
||
| 101 | |||
| 102 | if (Schema::hasColumn($table, $foreignColumn)) { |
||
| 103 | $this->error(sprintf( |
||
| 104 | 'Foreign column `%s` already exists in `%s` database table.', |
||
| 105 | $foreignColumn, |
||
| 106 | $table |
||
| 107 | )); |
||
| 108 | |||
| 109 | return 1; |
||
| 110 | } |
||
| 111 | |||
| 112 | try { |
||
| 113 | $stub = new AddForeignColumnStub( |
||
| 114 | $this->files, |
||
| 115 | $table, |
||
| 116 | $referencedTable, |
||
| 117 | $foreignColumn, |
||
| 118 | $referencedColumn, |
||
| 119 | $isForeignColumnNullable |
||
| 120 | ); |
||
| 121 | |||
| 122 | $this->creator->create($this->getMigrationsPath(), $stub); |
||
| 123 | } catch (FileNotFoundException $exception) { |
||
| 124 | $this->error($exception->getMessage()); |
||
| 125 | |||
| 126 | return 1; |
||
| 127 | } |
||
| 128 | |||
| 129 | $this->info('Migration created successfully!'); |
||
| 130 | |||
| 131 | $this->composer->dumpAutoloads(); |
||
| 132 | |||
| 133 | return 0; |
||
| 134 | } |
||
| 169 |