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 |
||
| 11 | View Code Duplication | class Registry |
|
|
1 ignored issue
–
show
|
|||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Admins registry. |
||
| 15 | * |
||
| 16 | * @var AdminInterface[] |
||
| 17 | */ |
||
| 18 | protected $admins = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Add an Admin to the registry. |
||
| 22 | * |
||
| 23 | * @param AdminInterface $admin |
||
| 24 | * @throws Exception |
||
| 25 | */ |
||
| 26 | 1 | public function add(AdminInterface $admin) |
|
| 33 | |||
| 34 | /** |
||
| 35 | * Return an Admin from the registry. |
||
| 36 | * |
||
| 37 | * @param string $name |
||
| 38 | * @return AdminInterface |
||
| 39 | * @throws Exception |
||
| 40 | */ |
||
| 41 | 1 | public function get($name) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Return true if an Admin with the name $name has been registered. |
||
| 52 | * |
||
| 53 | * @param string $name |
||
| 54 | * @return bool |
||
| 55 | */ |
||
| 56 | 1 | public function has($name) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Return all the registered Admins. |
||
| 63 | * |
||
| 64 | * @return AdminInterface[] |
||
| 65 | */ |
||
| 66 | 1 | public function all() |
|
| 70 | } |
||
| 71 |
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.