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 ArticleVideos extends AbstractMigration |
||
| 10 | { |
||
| 11 | public function up() |
||
| 12 | { |
||
| 13 | $this->table('article_videos', ['id' => false]) |
||
| 14 | ->addColumn('article_uuid', 'binary', ['limit' => 16]) |
||
| 15 | ->addColumn('title', 'text') |
||
| 16 | ->addColumn('sub_title', 'text', ['null' => true]) |
||
| 17 | ->addColumn('body', 'text') |
||
| 18 | ->addColumn('lead', 'text') |
||
| 19 | ->addColumn('video_url', 'text') |
||
| 20 | ->addColumn('featured_img', 'text', ['null' => true]) |
||
| 21 | ->addColumn('main_img', 'text', ['null' => true]) |
||
| 22 | ->addForeignKey('article_uuid', 'articles', 'article_uuid', ['delete' => 'NO_ACTION', 'update' => 'NO_ACTION']) |
||
| 23 | ->create(); |
||
| 24 | |||
| 25 | // $this->insertDummyData(); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function down() |
||
| 29 | { |
||
| 30 | $this->dropTable('article_videos'); |
||
| 31 | } |
||
| 32 | |||
| 33 | private function insertDummyData() |
||
| 34 | { |
||
| 35 | $ids = []; |
||
| 36 | $rows = $this->fetchAll('select admin_user_uuid from admin_users;'); |
||
| 37 | foreach ($rows as $r) { |
||
| 38 | $ids[] = $r['admin_user_uuid']; |
||
| 39 | } |
||
| 40 | |||
| 41 | $faker = Faker\Factory::create(); |
||
| 42 | $upload = new Upload('/var/www/unfinished/public/uploads/', '/var/www/unfinished/data/uploads/'); |
||
| 43 | $count = rand(25, 300); |
||
| 44 | $embedCodes = [ |
||
| 45 | '<iframe width="560" height="315" src="https://www.youtube.com/embed/JQxbSh99te8" frameborder="0" allowfullscreen></iframe>', |
||
| 46 | '<iframe width="560" height="315" src="https://www.youtube.com/embed/oH46Cr33-h4" frameborder="0" allowfullscreen></iframe>', |
||
| 47 | '<iframe width="560" height="315" src="https://www.youtube.com/embed/JQxbSh99te8" frameborder="0" allowfullscreen></iframe>', |
||
| 48 | ]; |
||
| 49 | |||
| 50 | // Download N images and set it randomly to posts |
||
| 51 | for ($i = 0; $i < 5; $i++) { |
||
| 52 | $image = $faker->image(); |
||
| 53 | $destination = $upload->getPath(basename($image)); |
||
| 54 | rename($image, $destination); |
||
| 55 | $mainImg[] = substr($destination, strlen('/var/www/unfinished/public')); |
||
| 56 | |||
| 57 | $image = $faker->image(); |
||
| 58 | $destination = $upload->getPath(basename($image)); |
||
| 59 | rename($image, $destination); |
||
| 60 | $featuredImg[] = substr($destination, strlen('/var/www/unfinished/public')); |
||
| 61 | } |
||
| 62 | |||
| 63 | for ($i = 0; $i < $count; $i++) { |
||
| 64 | $id = $faker->uuid; |
||
| 65 | $mysqluuid = (new Uuid($id))->toFormat(new Binary()); |
||
| 66 | $title = $faker->sentence(7, 20); |
||
| 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::POST, |
||
| 75 | ]; |
||
| 76 | |||
| 77 | $video = [ |
||
| 78 | 'article_uuid' => $mysqluuid, |
||
| 79 | 'title' => $title, |
||
| 80 | 'body' => $faker->paragraph(15), |
||
| 81 | 'lead' => $faker->paragraph(5), |
||
| 82 | 'main_img' => $mainImg[array_rand($mainImg)], |
||
| 83 | 'featured_img' => $featuredImg[array_rand($featuredImg)], |
||
| 84 | 'video_url' => $embedCodes[rand(0, 2)], |
||
| 85 | ]; |
||
| 86 | |||
| 87 | $this->insert('articles', $article); |
||
| 88 | $this->insert('article_videos', $video); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 |