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 |
||
| 8 | View Code Duplication | class Registry |
|
|
|
|||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Actions registry. |
||
| 12 | * |
||
| 13 | * @var ActionInterface[] |
||
| 14 | */ |
||
| 15 | protected $actions = []; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Add an Action to the registry. |
||
| 19 | * |
||
| 20 | * @param $id |
||
| 21 | * @param ActionInterface $action |
||
| 22 | * |
||
| 23 | * @throws Exception |
||
| 24 | */ |
||
| 25 | 1 | public function add($id, ActionInterface $action) |
|
| 32 | |||
| 33 | /** |
||
| 34 | * Return an Action from the registry. |
||
| 35 | * |
||
| 36 | * @param string $id |
||
| 37 | * @return ActionInterface |
||
| 38 | * @throws Exception |
||
| 39 | */ |
||
| 40 | 1 | public function get($id) |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Return true if an Action with the name $id has been registered. |
||
| 51 | * |
||
| 52 | * @param string $id |
||
| 53 | * @return bool |
||
| 54 | */ |
||
| 55 | 1 | public function has($id) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Return all the registered Actions. |
||
| 62 | * |
||
| 63 | * @return ActionInterface[] |
||
| 64 | */ |
||
| 65 | 1 | public function all() |
|
| 69 | } |
||
| 70 |
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.