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 |
||
12 | class CategoryResolver implements CategoryResolverInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var CategoryCollectionFactory |
||
16 | */ |
||
17 | private $categoryCollectionFactory; |
||
18 | |||
19 | /** |
||
20 | * @var StoreManagerInterface |
||
21 | */ |
||
22 | private $storeManager; |
||
23 | |||
24 | /** |
||
25 | * @param CategoryCollectionFactory $categoryCollectionFactory |
||
26 | * @param StoreManagerInterface $storeManager |
||
27 | */ |
||
28 | public function __construct( |
||
35 | |||
36 | /** |
||
37 | * @param string $urlKey |
||
38 | * @param int $storeId |
||
39 | * @param int $parentId |
||
40 | * |
||
41 | * @return EntityData |
||
42 | */ |
||
43 | public function resolveByUrlKey(string $urlKey, int $storeId, int $parentId) : EntityData |
||
58 | |||
59 | /** |
||
60 | * @param string $urlKey |
||
61 | * @param int $storeId |
||
62 | * @param int $parentId |
||
63 | * |
||
64 | * @return EntityData[] |
||
65 | */ |
||
66 | public function resolveAllByUrlKey(string $urlKey, int $storeId, int $parentId) : array |
||
81 | |||
82 | /** |
||
83 | * @param int $categoryId |
||
84 | * @param int $storeId |
||
85 | * |
||
86 | * @return EntityData |
||
87 | */ |
||
88 | View Code Duplication | public function resolveById(int $categoryId, int $storeId) : EntityData |
|
103 | |||
104 | /** |
||
105 | * @param int $categoryId |
||
106 | * |
||
107 | * @return int[] |
||
108 | */ |
||
109 | public function resolveParentIds(int $categoryId) : array |
||
132 | } |
||
133 |
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.