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 |
||
5 | class NewsCategoryStatusColumnConversion extends AbstractMigration |
||
6 | { |
||
7 | View Code Duplication | public function up() |
|
|
|||
8 | { |
||
9 | $newsCategoryTable = $this->table('news_categories'); |
||
10 | $newsCategoryTable |
||
11 | ->addColumn('is_read_only', 'boolean', [ |
||
12 | 'after' => 'protected', |
||
13 | 'null' => false, |
||
14 | 'default' => false, |
||
15 | 'comment' => 'When set to true, no new articles should be able to use this category' |
||
16 | ]) |
||
17 | ->addColumn('is_deleted', 'boolean', [ |
||
18 | 'after' => 'is_read_only', |
||
19 | 'null' => false, |
||
20 | 'default' => false, |
||
21 | 'comment' => 'Whether or not the news category has been soft deleted', |
||
22 | ]) |
||
23 | ->changeColumn('protected', 'boolean', [ |
||
24 | 'default' => false, |
||
25 | 'null' => false, |
||
26 | 'comment' => 'When set to true, prevents the category from being deleted from the UI', |
||
27 | ]) |
||
28 | ->update() |
||
29 | ; |
||
30 | |||
31 | $this->query("UPDATE news_categories SET is_deleted = 1 WHERE status = 'deleted';"); |
||
32 | |||
33 | $newsCategoryTable |
||
34 | ->removeColumn('status') |
||
35 | ->renameColumn('protected', 'is_protected') |
||
36 | ->update() |
||
37 | ; |
||
38 | } |
||
39 | |||
40 | public function down() |
||
63 | } |
||
64 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.