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 |
||
30 | class ChangeColumn extends Action |
||
31 | { |
||
32 | /** |
||
33 | * The column definition |
||
34 | * |
||
35 | * @var Column |
||
36 | */ |
||
37 | protected $column; |
||
38 | |||
39 | /** |
||
40 | * The name of the column to be changed |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $columnName; |
||
45 | |||
46 | /** |
||
47 | * Constructor |
||
48 | * |
||
49 | * @param Table $table The table to alter |
||
50 | * @param mixed $columnName The name fo the column to change |
||
51 | * @param Column $column The column definition |
||
52 | */ |
||
53 | public function __construct(Table $table, $columnName, Column $column) |
||
64 | |||
65 | /** |
||
66 | * Creates a new ChangeColumn object after building the column definition |
||
67 | * out of the provided arguments |
||
68 | * |
||
69 | * @param Table $table The table to alter |
||
70 | * @param mixed $columnName The name of the column to change |
||
71 | * @param mixed $type The type of the column |
||
72 | * @param mixed $options Addiotional options for the column |
||
73 | * @return ChangeColumn |
||
74 | */ |
||
75 | View Code Duplication | public static function build(Table $table, $columnName, $type = null, $options = []) |
|
84 | |||
85 | /** |
||
86 | * Returns the name of the column to change |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | public function getColumnName() |
||
94 | |||
95 | /** |
||
96 | * Returns the column definition |
||
97 | * |
||
98 | * @return Column |
||
99 | */ |
||
100 | public function getColumn() |
||
104 | } |
||
105 |
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.