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 DropForeignKey extends Action |
|
|
|||
32 | { |
||
33 | |||
34 | /** |
||
35 | * The foreing key to remove |
||
36 | * |
||
37 | * @var ForeignKey |
||
38 | */ |
||
39 | protected $foreignKey; |
||
40 | |||
41 | /** |
||
42 | * Constructor |
||
43 | * |
||
44 | * @param Table $table The table to remove the constraint from |
||
45 | * @param ForeignKey $foreignKey The foreign key to remove |
||
46 | */ |
||
47 | public function __construct(Table $table, ForeignKey $foreignKey) |
||
52 | |||
53 | /** |
||
54 | * Creates a new DropForeignKey object after building the ForeignKey |
||
55 | * definition out of the passed arguments. |
||
56 | * |
||
57 | * @param Table $table The table to dele the foreign key from |
||
58 | * @param string|string[] $columns The columns participating in the foreign key |
||
59 | * @param string|null $constraint The constraint name |
||
60 | * @return DropForeignKey |
||
61 | */ |
||
62 | public static function build(Table $table, $columns, $constraint = null) |
||
77 | |||
78 | /** |
||
79 | * Returns the foreign key to remove |
||
80 | * |
||
81 | * @return ForeignKey |
||
82 | */ |
||
83 | public function getForeignKey() |
||
87 | } |
||
88 |
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.