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 |
||
| 19 | View Code Duplication | class ItemParentFactory implements ItemFactoryInterface |
|
|
|
|||
| 20 | { |
||
| 21 | /** @var ItemBuilder */ |
||
| 22 | protected $itemBuilder; |
||
| 23 | |||
| 24 | /** @var AdminGroups */ |
||
| 25 | protected $adminGroups; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * ItemParentFactory constructor. |
||
| 29 | * |
||
| 30 | * @param ItemBuilder $itemBuilder |
||
| 31 | */ |
||
| 32 | public function __construct(AdminGroups $adminGroups, ItemBuilder $itemBuilder) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Check if item factory supports building a certain class. |
||
| 40 | * |
||
| 41 | * @param string $class |
||
| 42 | * |
||
| 43 | * @return boolean |
||
| 44 | */ |
||
| 45 | public function supports($class) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Creates a new Menu item |
||
| 52 | * |
||
| 53 | * @param string $class Class of the item. |
||
| 54 | * @param string $id Id of the item |
||
| 55 | * @param string $path Path of the item |
||
| 56 | * @param string $label |
||
| 57 | * @param string $permission |
||
| 58 | * @param array $options |
||
| 59 | * |
||
| 60 | * @return ItemInterface |
||
| 61 | */ |
||
| 62 | public function build($class, $id, $path, $label, $permission, $options = []) |
||
| 73 | } |
||
| 74 |
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.