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 |
||
31 | View Code Duplication | class AddIndex extends Action |
|
|
|||
32 | { |
||
33 | /** |
||
34 | * The index to add to the table |
||
35 | * |
||
36 | * @var Index |
||
37 | */ |
||
38 | protected $index; |
||
39 | |||
40 | /** |
||
41 | * Constructor |
||
42 | * |
||
43 | * @param Table $table The table to add the index to |
||
44 | * @param Index $index The index to be added |
||
45 | */ |
||
46 | public function __construct(Table $table, Index $index) |
||
51 | |||
52 | /** |
||
53 | * Creates a new AddIndex object after building the index object with the |
||
54 | * provided arguments |
||
55 | * |
||
56 | * @param Table $table The table to add the index to |
||
57 | * @param mixed $columns The columns to index |
||
58 | * @param array $options Additional options for the index creation |
||
59 | * @return AddIndex |
||
60 | */ |
||
61 | public static function build(Table $table, $columns, array $options = []) |
||
79 | |||
80 | /** |
||
81 | * Returns the index to be added |
||
82 | * |
||
83 | * @return Index |
||
84 | */ |
||
85 | public function getIndex() |
||
89 | } |
||
90 |
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.