Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php | ||
| 9 | class ArticleEvents extends AbstractMigration | ||
| 10 | { | ||
| 11 | public function up() | ||
| 12 |     { | ||
| 13 |         $this->table('article_events', ['id' => false]) | ||
| 14 |             ->addColumn('article_uuid', 'binary', ['limit' => 16]) | ||
| 15 |             ->addColumn('title', 'text') | ||
| 16 |             ->addColumn('sub_title', 'text', ['null' => true]) | ||
| 17 |             ->addColumn('place_name', 'text') | ||
| 18 |             ->addColumn('body', 'text') | ||
| 19 |             ->addColumn('longitude', 'text') | ||
| 20 |             ->addColumn('latitude', 'text') | ||
| 21 |             ->addColumn('featured_img', 'text', ['null' => true]) | ||
| 22 |             ->addColumn('main_img', 'text', ['null' => true]) | ||
| 23 |             ->addColumn('start_at', 'datetime') | ||
| 24 |             ->addColumn('end_at', 'datetime') | ||
| 25 |             ->addColumn('event_url', 'text', ['null' => true]) | ||
| 26 |             ->addForeignKey('article_uuid', 'articles', 'article_uuid', ['delete' => 'NO_ACTION', 'update' => 'NO_ACTION']) | ||
| 27 | ->create(); | ||
| 28 | |||
| 29 | // $this->insertDummyData(); | ||
| 30 | } | ||
| 31 | |||
| 32 | public function down() | ||
| 33 |     { | ||
| 34 |         $this->dropTable('article_events'); | ||
| 35 | } | ||
| 36 | |||
| 37 | private function insertDummyData() | ||
| 38 |     { | ||
| 39 | $ids = []; | ||
| 40 |         $rows = $this->fetchAll('select admin_user_uuid from admin_users;'); | ||
| 41 |         foreach ($rows as $r) { | ||
| 42 | $ids[] = $r['admin_user_uuid']; | ||
| 43 | } | ||
| 44 | |||
| 45 | $faker = Faker\Factory::create(); | ||
| 46 |         $upload = new Upload('/var/www/unfinished/public/uploads/', '/var/www/unfinished/data/uploads/'); | ||
| 47 | $count = rand(25, 300); | ||
| 48 | |||
| 49 | // Download N images and set it randomly to the events | ||
| 50 |         for ($i = 0; $i < 5; $i++) { | ||
| 51 | $image = $faker->image(); | ||
| 52 | $destination = $upload->getPath(basename($image)); | ||
| 53 | rename($image, $destination); | ||
| 54 |             $mainImg[] = substr($destination, strlen('/var/www/unfinished/public')); | ||
| 55 | |||
| 56 | $image = $faker->image(); | ||
| 57 | $destination = $upload->getPath(basename($image)); | ||
| 58 | rename($image, $destination); | ||
| 59 |             $featuredImg[] = substr($destination, strlen('/var/www/unfinished/public')); | ||
| 60 | } | ||
| 61 | |||
| 62 |         for ($i = 0; $i < $count; $i++) { | ||
| 63 | $start = rand(1, 20); | ||
| 64 | $id = $faker->uuid; | ||
| 65 | $mysqluuid = (new Uuid($id))->toFormat(new Binary()); | ||
| 66 | $title = $faker->sentence(5, 15); | ||
| 67 | |||
| 68 | $article = [ | ||
| 69 | 'article_uuid' => $mysqluuid, | ||
| 70 | 'article_id' => $id, | ||
| 71 |                 'slug'            => strtolower(trim(preg_replace('~[^\pL\d]+~u', '-', $title), '-')), | ||
| 72 | 'status' => 1, | ||
| 73 | 'admin_user_uuid' => $ids[array_rand($ids)], | ||
| 74 | 'type' => ArticleType::EVENT, | ||
| 75 | ]; | ||
| 76 | |||
| 77 | $post = [ | ||
| 78 | 'article_uuid' => $mysqluuid, | ||
| 79 | 'title' => 'Event: '.$title, | ||
| 80 | 'body' => $faker->paragraph(15), | ||
| 81 | 'longitude' => $faker->longitude, | ||
| 82 | 'latitude' => $faker->latitude, | ||
| 83 | 'place_name' => $faker->sentence(1), | ||
| 84 | 'main_img' => $mainImg[array_rand($mainImg)], | ||
| 85 | 'featured_img' => $featuredImg[array_rand($featuredImg)], | ||
| 86 |                 'start_at'     => date('Y-m-d H:i:s', strtotime("+$start  day +$start hours")), | ||
| 87 |                 'end_at'       => date('Y-m-d H:i:s', strtotime("+$start  day +".($start + rand(1, 5)).' hours')), | ||
| 88 | ]; | ||
| 89 | |||
| 90 |             $this->insert('articles', $article); | ||
| 91 |             $this->insert('article_events', $post); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 |