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 |
||
35 | View Code Duplication | class AttributeOptionAction extends AbstractAction |
|
|
|||
36 | { |
||
37 | |||
38 | /** |
||
39 | * Helper method that create/update the passed entity, depending on |
||
40 | * the entity's status. |
||
41 | * |
||
42 | * @param array $row The entity data to create/update |
||
43 | * |
||
44 | * @return string The last inserted ID |
||
45 | */ |
||
46 | public function persist(array $row) |
||
55 | |||
56 | /** |
||
57 | * Creates's the entity with the passed attributes. |
||
58 | * |
||
59 | * @param array $row The attributes of the entity to create |
||
60 | * @param string|null $name The name of the prepared statement that has to be executed |
||
61 | * |
||
62 | * @return string The last inserted ID |
||
63 | */ |
||
64 | public function create($row, $name = null) |
||
68 | |||
69 | /** |
||
70 | * Update's the entity with the passed attributes. |
||
71 | * |
||
72 | * @param array $row The attributes of the entity to update |
||
73 | * @param string|null $name The name of the prepared statement that has to be executed |
||
74 | * |
||
75 | * @return string The ID of the updated product |
||
76 | */ |
||
77 | public function update($row, $name = null) |
||
81 | } |
||
82 |
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.