| Conditions | 5 |
| Paths | 4 |
| Total Lines | 61 |
| Code Lines | 43 |
| 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 |
||
| 37 | public function upgrade(): void |
||
| 38 | { |
||
| 39 | // These tables were created by webtrees 1.x, and may not exist if we first installed webtrees 2.x |
||
| 40 | DB::schema()->dropIfExists('site_access_rule'); |
||
| 41 | DB::schema()->dropIfExists('next_id'); |
||
| 42 | |||
| 43 | // Split the media table into media/media_file so that we can store multiple media |
||
| 44 | // files in each media object. |
||
| 45 | if (!DB::schema()->hasTable('media_file')) { |
||
| 46 | DB::schema()->create('media_file', static function (Blueprint $table): void { |
||
| 47 | $table->integer('id', true); |
||
| 48 | $table->string('m_id', 20); |
||
| 49 | $table->integer('m_file'); |
||
| 50 | $table->string('multimedia_file_refn', 248); // GEDCOM only allows 30 characters |
||
| 51 | $table->string('multimedia_format', 4); |
||
| 52 | $table->string('source_media_type', 15); |
||
| 53 | $table->string('descriptive_title', 248); |
||
| 54 | |||
| 55 | $table->index(['m_id', 'm_file']); |
||
| 56 | $table->index(['m_file', 'm_id']); |
||
| 57 | $table->index(['m_file', 'multimedia_file_refn']); |
||
| 58 | $table->index(['m_file', 'multimedia_format']); |
||
| 59 | $table->index(['m_file', 'source_media_type']); |
||
| 60 | $table->index(['m_file', 'descriptive_title']); |
||
| 61 | }); |
||
| 62 | } |
||
| 63 | |||
| 64 | if (DB::table('media_file')->count() === 0 && DB::schema()->hasColumn('media', 'm_filename')) { |
||
| 65 | (new Builder(DB::connection()))->from('media_file')->insertUsing([ |
||
| 66 | 'm_id', |
||
| 67 | 'm_file', |
||
| 68 | 'multimedia_file_refn', |
||
| 69 | 'multimedia_format', |
||
| 70 | 'source_media_type', |
||
| 71 | 'descriptive_title', |
||
| 72 | ], function (Builder $query): void { |
||
| 73 | // SQLite also supports SUBSTRING() from 3.34.0 (2020-12-01) |
||
| 74 | $substring_function = DB::connection()->getDriverName() === 'sqlite' ? 'SUBSTR' : 'SUBSTRING'; |
||
| 75 | |||
| 76 | $query->select([ |
||
| 77 | 'm_id', |
||
| 78 | 'm_file', |
||
| 79 | new Expression($substring_function . '(m_filename, 1, 248)'), |
||
| 80 | new Expression($substring_function . '(m_ext, 1, 4)'), |
||
| 81 | new Expression($substring_function . '(m_type, 1, 15)'), |
||
| 82 | new Expression($substring_function . '(m_titl, 1, 248)'), |
||
| 83 | ])->from('media'); |
||
| 84 | }); |
||
| 85 | |||
| 86 | // The Laravel database library for SQLite can only drop one column at a time. |
||
| 87 | DB::schema()->table('media', static function (Blueprint $table): void { |
||
| 88 | $table->dropColumn('m_filename'); |
||
| 89 | }); |
||
| 90 | DB::schema()->table('media', static function (Blueprint $table): void { |
||
| 91 | $table->dropColumn('m_ext'); |
||
| 92 | }); |
||
| 93 | DB::schema()->table('media', static function (Blueprint $table): void { |
||
| 94 | $table->dropColumn('m_type'); |
||
| 95 | }); |
||
| 96 | DB::schema()->table('media', static function (Blueprint $table): void { |
||
| 97 | $table->dropColumn('m_titl'); |
||
| 98 | }); |
||
| 102 |