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 |
||
26 | class Table extends BaseTable |
||
27 | { |
||
28 | /** |
||
29 | * Add a Created Timestamp Column |
||
30 | * |
||
31 | * This function is a simple accessor to allow a universal design for the created timestamp. |
||
32 | * |
||
33 | * @return BaseTable The table object with a created column added. |
||
34 | */ |
||
35 | View Code Duplication | public function addCreated() |
|
47 | |||
48 | /** |
||
49 | * Add a Modified Timestamp Column |
||
50 | * |
||
51 | * This function is a simple accessor to allow a universal design for the modified timestamp. |
||
52 | * |
||
53 | * @return BaseTable The table object with a modified column added. |
||
54 | */ |
||
55 | View Code Duplication | public function addModified() |
|
67 | |||
68 | /** |
||
69 | * Add a varchar column to the table |
||
70 | * |
||
71 | * This function will be used to add a different styles of varchar type columns to the table. It can be called |
||
72 | * directly but it is recommended to use the specific column creation methods. |
||
73 | * |
||
74 | * @param string $name Name of the column to add |
||
75 | * @param int $size Character limit of the new column |
||
76 | * @param bool $nullable Is the field be nullable |
||
77 | * @param string $default The default value for the field |
||
78 | * @return BaseTable The table object with the varchar column added. |
||
79 | */ |
||
80 | public function addVarChar($name, $size, $nullable = true, $default = null) |
||
92 | |||
93 | /** |
||
94 | * Add a tiny varchar column to the table |
||
95 | * |
||
96 | * This function adds a 25 character length varchar column to the table. |
||
97 | * + **Type:** varchar |
||
98 | * + **Size:** 25 |
||
99 | * + **Nullable:** false |
||
100 | * + **Default:** null |
||
101 | * |
||
102 | * @param string $name Name of the column to add |
||
103 | * @param bool $nullable Is the field be nullable |
||
104 | * @param string $default The default value for the field |
||
105 | * @return BaseTable The table object with a 25 character varchar column added |
||
106 | */ |
||
107 | public function addTinyText($name, $nullable = true, $default = null) |
||
111 | } |
||
112 |
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.