| Conditions | 4 |
| Paths | 4 |
| Total Lines | 78 |
| Code Lines | 18 |
| Lines | 34 |
| Ratio | 43.59 % |
| 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 |
||
| 46 | public function up(Schema $schema) |
||
| 47 | { |
||
| 48 | /* |
||
| 49 | * Migration is not critical |
||
| 50 | * Old format for storing image files to the new format |
||
| 51 | * |
||
| 52 | * Old format(date added image): |
||
| 53 | * media/{Y}/{m}/ |
||
| 54 | * |
||
| 55 | * New Format(date added item): |
||
| 56 | * media/{Y}/{m}/{d}/{His}/ |
||
| 57 | */ |
||
| 58 | |||
| 59 | // move covers |
||
| 60 | $items = $this->connection->fetchAll(' |
||
| 61 | SELECT |
||
| 62 | `id`, |
||
| 63 | `cover`, |
||
| 64 | `date_add` |
||
| 65 | FROM |
||
| 66 | `item` |
||
| 67 | WHERE |
||
| 68 | `cover` IS NOT NULL AND |
||
| 69 | `cover` != "" AND |
||
| 70 | `cover` NOT LIKE "example/%"' |
||
| 71 | ); |
||
| 72 | View Code Duplication | foreach ($items as $item) { |
|
| 73 | $path = date('Y/m/d/His/', strtotime($item['date_add'])); |
||
| 74 | $file = new File($this->media_dir.$item['cover']); |
||
| 75 | $file->move($this->media_dir.$path); |
||
| 76 | $this->addSql(' |
||
| 77 | UPDATE |
||
| 78 | `item` |
||
| 79 | SET |
||
| 80 | `cover` = ? |
||
| 81 | WHERE |
||
| 82 | `id` = ?', |
||
| 83 | [ |
||
| 84 | $path.$file->getBasename(), |
||
| 85 | $item['id'], |
||
| 86 | ] |
||
| 87 | ); |
||
| 88 | } |
||
| 89 | |||
| 90 | // move images |
||
| 91 | $images = $this->connection->fetchAll(' |
||
| 92 | SELECT |
||
| 93 | im.`id`, |
||
| 94 | im.`source`, |
||
| 95 | i.`date_add` |
||
| 96 | FROM |
||
| 97 | `item` AS `i` |
||
| 98 | INNER JOIN |
||
| 99 | `image` AS `im` |
||
| 100 | ON |
||
| 101 | im.`item` = i.`id`' |
||
| 102 | ); |
||
| 103 | View Code Duplication | foreach ($images as $image) { |
|
| 104 | $path = date('Y/m/d/His/', strtotime($image['date_add'])); |
||
| 105 | $file = new File($this->media_dir.$image['source']); |
||
| 106 | $file->move($this->media_dir.$path); |
||
| 107 | $this->addSql(' |
||
| 108 | UPDATE |
||
| 109 | `image` |
||
| 110 | SET |
||
| 111 | `source` = ? |
||
| 112 | WHERE |
||
| 113 | `id` = ?', |
||
| 114 | [ |
||
| 115 | $path.$file->getBasename(), |
||
| 116 | $image['id'], |
||
| 117 | ] |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | |||
| 121 | // skip if no data |
||
| 122 | $this->skipIf(!($items && $images), 'No data to migrate'); |
||
| 123 | } |
||
| 124 | |||
| 136 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: