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 install_contentcategory_table extends Migration implements MigrationInterface |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Execute actions when migration is up |
||
| 13 | * @return void |
||
| 14 | */ |
||
| 15 | public function up() |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Seed created table via up() method with some data |
||
| 30 | * @return void |
||
| 31 | */ |
||
| 32 | public function seed() |
||
| 33 | { |
||
| 34 | // create categories |
||
| 35 | $cat = new stdClass(); |
||
| 36 | $cat->General = [ |
||
| 37 | 'title' => serialize([ |
||
| 38 | 'ru' => 'Главная', |
||
| 39 | 'en' => 'General' |
||
| 40 | ]) |
||
| 41 | ]; |
||
| 42 | |||
| 43 | $cat->News = [ |
||
| 44 | 'title' => serialize([ |
||
| 45 | 'ru' => 'Новости', |
||
| 46 | 'en' => 'News' |
||
| 47 | ]), |
||
| 48 | 'configs' => serialize([ |
||
| 49 | 'showDate' => '1', |
||
| 50 | 'showRating' => '1', |
||
| 51 | 'showCategory' => '1', |
||
| 52 | 'showAuthor' => '1', |
||
| 53 | 'showViews' => '1', |
||
| 54 | 'showComments' => '1', |
||
| 55 | 'showPoster' => '1', |
||
| 56 | 'showTags' => '1' |
||
| 57 | ]) |
||
| 58 | ]; |
||
| 59 | |||
| 60 | $cat->Page = [ |
||
| 61 | 'title' => serialize([ |
||
| 62 | 'ru' => 'Страницы', |
||
| 63 | 'en' => 'Pages' |
||
| 64 | ]) |
||
| 65 | ]; |
||
| 66 | |||
| 67 | $this->getConnection()->table('content_categories')->insert([ |
||
| 68 | ['id' => 1, 'path' => '', 'title' => $cat->General['title'], 'description' => '', 'configs' => '', 'created_at' => $this->now, 'updated_at' => $this->now], |
||
| 69 | ['id' => 2, 'path' => 'news', 'title' => $cat->News['title'], 'description' => '', 'configs' => $cat->News['configs'], 'created_at' => $this->now, 'updated_at' => $this->now], |
||
| 70 | ['id' => 3, 'path' => 'page', 'title' => $cat->Page['title'], 'description' => '', 'configs' => '', 'created_at' => $this->now, 'updated_at' => $this->now], |
||
| 71 | ]); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Execute actions when migration is down |
||
| 76 | * @return void |
||
| 77 | */ |
||
| 78 | public function down() |
||
| 82 | } |
||
| 83 | } |