| Conditions | 8 |
| Paths | 7 |
| Total Lines | 55 |
| Code Lines | 30 |
| 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 |
||
| 54 | public function execute(CapsuleInterface $capsule): void |
||
| 55 | { |
||
| 56 | $schema = $capsule->getSchema($this->getTable()); |
||
| 57 | |||
| 58 | if ($schema->hasForeignKey($this->columns)) { |
||
| 59 | throw new ForeignKeyException( |
||
| 60 | "Unable to add foreign key '{$schema->getName()}'.({$this->columnNames()}), " |
||
| 61 | . 'foreign key already exists' |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | |||
| 65 | $foreignSchema = $capsule->getSchema($this->foreignTable); |
||
| 66 | |||
| 67 | if ($this->foreignTable !== $this->table && !$foreignSchema->exists()) { |
||
| 68 | throw new ForeignKeyException( |
||
| 69 | "Unable to add foreign key '{$schema->getName()}'.'{$this->columnNames()}', " |
||
| 70 | . "foreign table '{$this->foreignTable}' does not exists" |
||
| 71 | ); |
||
| 72 | } |
||
| 73 | |||
| 74 | foreach ($this->foreignKeys as $fk) { |
||
| 75 | if ($this->foreignTable !== $this->table && !$foreignSchema->hasColumn($fk)) { |
||
| 76 | throw new ForeignKeyException( |
||
| 77 | "Unable to add foreign key '{$schema->getName()}'.'{$this->columnNames()}'," |
||
| 78 | . " foreign column '{$this->foreignTable}'.'{$fk}' does not exists" |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | $foreignKey = $schema->foreignKey($this->columns)->references( |
||
| 84 | $this->foreignTable, |
||
| 85 | $this->foreignKeys |
||
| 86 | ); |
||
| 87 | |||
| 88 | if ($this->hasOption('name')) { |
||
| 89 | $foreignKey->setName($this->getOption('name')); |
||
|
|
|||
| 90 | } |
||
| 91 | |||
| 92 | /* |
||
| 93 | * We are allowing both formats "NO_ACTION" and "NO ACTION". |
||
| 94 | */ |
||
| 95 | |||
| 96 | $foreignKey->onDelete( |
||
| 97 | str_replace( |
||
| 98 | '_', |
||
| 99 | ' ', |
||
| 100 | $this->getOption('delete', ForeignKeyInterface::NO_ACTION) |
||
| 101 | ) |
||
| 102 | ); |
||
| 103 | |||
| 104 | $foreignKey->onUpdate( |
||
| 105 | str_replace( |
||
| 106 | '_', |
||
| 107 | ' ', |
||
| 108 | $this->getOption('update', ForeignKeyInterface::NO_ACTION) |
||
| 109 | ) |
||
| 113 |