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 |
||
36 | class AttributeOptionRepository extends AbstractRepository implements AttributeOptionRepositoryInterface |
||
37 | { |
||
38 | |||
39 | /** |
||
40 | * The prepared statement to load an existing EAV attribute option by its entity type ID, attribute code, store ID and value. |
||
41 | * |
||
42 | * @var \PDOStatement |
||
43 | */ |
||
44 | protected $attributeOptionByEntityTypeIdAndAttributeCodeAndStoreIdAndValueStmt; |
||
45 | |||
46 | /** |
||
47 | * The prepared statement to load an existing EAV attribute option by its entity type ID, attribute code, store ID and swatch. |
||
48 | * |
||
49 | * @var \PDOStatement |
||
50 | */ |
||
51 | protected $attributeOptionByEntityTypeIdAndAttributeCodeAndStoreIdAndSwatchAndTypeStmt; |
||
52 | |||
53 | /** |
||
54 | * The prepared statement to load the EAV attribute option with the given attribute ID and the highest sort order. |
||
55 | * |
||
56 | * @var \PDOStatement |
||
57 | */ |
||
58 | protected $attributeOptionByAttributeIdOrderBySortOrderDescStmt; |
||
59 | |||
60 | /** |
||
61 | * Initializes the repository's prepared statements. |
||
62 | * |
||
63 | * @return void |
||
64 | */ |
||
65 | public function init() |
||
76 | |||
77 | /** |
||
78 | * Load's and return's the EAV attribute option with the passed entity type ID and code, store ID and value. |
||
79 | * |
||
80 | * @param string $entityTypeId The entity type ID of the EAV attribute to load the option for |
||
81 | * @param string $attributeCode The code of the EAV attribute option to load |
||
82 | * @param integer $storeId The store ID of the attribute option to load |
||
83 | * @param string $value The value of the attribute option to load |
||
84 | * |
||
85 | * @return array The EAV attribute option |
||
86 | */ |
||
87 | public function findOneByEntityTypeIdAndAttributeCodeAndStoreIdAndValue($entityTypeId, $attributeCode, $storeId, $value) |
||
102 | |||
103 | /** |
||
104 | * Load's and return's the EAV attribute option with the passed entity type ID and code, store ID, swatch and type. |
||
105 | * |
||
106 | * @param string $entityTypeId The entity type ID of the EAV attribute to load the option for |
||
107 | * @param string $attributeCode The code of the EAV attribute option to load |
||
108 | * @param integer $storeId The store ID of the attribute option to load |
||
109 | * @param string $swatch The swatch of the attribute option to load |
||
110 | * @param string $type The swatch type of the attribute option to load |
||
111 | * |
||
112 | * @return array The EAV attribute option |
||
113 | */ |
||
114 | View Code Duplication | public function findOneByEntityTypeIdAndAttributeCodeAndStoreIdAndSwatchAndType($entityTypeId, $attributeCode, $storeId, $swatch, $type) |
|
130 | |||
131 | /** |
||
132 | * Returns the EAV attribute option of attribute with the passed ID with the highest sort order. |
||
133 | * |
||
134 | * @param integer $attributeId The ID of the attribute to return the EAV option with the highest sort order for |
||
135 | * |
||
136 | * @return array|null The EAV attribute option with the highest sort order |
||
137 | */ |
||
138 | public function findOneByAttributeIdAndHighestSortOrder($attributeId) |
||
145 | } |
||
146 |
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.