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