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 |
||
6 | class NewsStatusColumnConversion extends AbstractMigration |
||
7 | { |
||
8 | View Code Duplication | public function up() |
|
|
|||
9 | { |
||
10 | $newsTable = $this->table('news'); |
||
11 | $newsTable |
||
12 | ->addColumn('is_draft', 'boolean', [ |
||
13 | 'after' => 'editor', |
||
14 | 'null' => false, |
||
15 | 'default' => false, |
||
16 | 'comment' => 'Whether or not the news article is a draft', |
||
17 | ]) |
||
18 | ->addColumn('is_deleted', 'boolean', [ |
||
19 | 'after' => 'is_draft', |
||
20 | 'null' => false, |
||
21 | 'default' => false, |
||
22 | 'comment' => 'Whether or not the news article has been soft deleted', |
||
23 | ]) |
||
24 | ->update() |
||
25 | ; |
||
26 | |||
27 | $this->query("UPDATE news SET is_draft = 1 WHERE status = 'revision' OR status ='draft';"); |
||
28 | $this->query("UPDATE news SET is_deleted = 1 WHERE status = 'deleted' OR status = 'disabled';"); |
||
29 | |||
30 | $newsTable |
||
31 | ->removeColumn('parent_id') |
||
32 | ->removeColumn('status') |
||
33 | ->update() |
||
34 | ; |
||
35 | } |
||
36 | |||
37 | public function down() |
||
67 | } |
||
68 |
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.