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 |
||
37 | class UrlRewriteRepository extends AbstractFinderRepository implements UrlRewriteRepositoryInterface |
||
38 | { |
||
39 | |||
40 | /** |
||
41 | * The registry processor instance. |
||
42 | * |
||
43 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
44 | */ |
||
45 | protected $registryProcessor; |
||
46 | |||
47 | /** |
||
48 | * The prepared statement to load the existing URL rewrites. |
||
49 | * |
||
50 | * @var \PDOStatement |
||
51 | */ |
||
52 | protected $urlRewritesStmt; |
||
53 | |||
54 | /** |
||
55 | * The prepared statement to load the existing URL rewrites by their entity type and ID. |
||
56 | * |
||
57 | * @var \PDOStatement |
||
58 | */ |
||
59 | protected $urlRewritesByEntityTypeAndEntityIdStmt; |
||
60 | |||
61 | /** |
||
62 | * The prepared statement to load the existing URL rewrites by their entity type, entity and store ID. |
||
63 | * |
||
64 | * @var \PDOStatement |
||
65 | */ |
||
66 | protected $urlRewritesByEntityTypeAndEntityIdAndStoreIdStmt; |
||
67 | |||
68 | /** |
||
69 | * The prefix to load the URL rewrites with the given request path and store ID from the registry. |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | protected $urlRewritesByRequestPathAndStoreIdPrefix; |
||
74 | |||
75 | /** |
||
76 | * Initializes the repository's prepared statements. |
||
77 | * |
||
78 | * @return void |
||
79 | */ |
||
80 | public function init() |
||
90 | |||
91 | /** |
||
92 | * Return's the finder's entity name. |
||
93 | * |
||
94 | * @return string The finder's entity name |
||
95 | */ |
||
96 | public function getEntityName() |
||
100 | |||
101 | /** |
||
102 | * Return's the primary key name of the entity. |
||
103 | * |
||
104 | * @return string The name of the entity's primary key |
||
105 | */ |
||
106 | public function getPrimaryKeyName() |
||
110 | |||
111 | /** |
||
112 | * Return's an array with the available URL rewrites. |
||
113 | * |
||
114 | * @return array The available URL rewrites |
||
115 | */ |
||
116 | public function findAll() |
||
122 | |||
123 | /** |
||
124 | * Return's an array with the available URL rewrites |
||
125 | * |
||
126 | * @return array The array with the rewrites, grouped by request path and store ID |
||
127 | * @todo Refactor to a yielded version also |
||
128 | */ |
||
129 | public function findAllGroupedByRequestPathAndStoreId() |
||
144 | |||
145 | /** |
||
146 | * Return's an array with the URL rewrites for the passed entity type and ID. |
||
147 | * |
||
148 | * @param string $entityType The entity type to load the URL rewrites for |
||
149 | * @param integer $entityId The entity ID to load the URL rewrites for |
||
150 | * |
||
151 | * @return array The URL rewrites |
||
152 | */ |
||
153 | View Code Duplication | public function findAllByEntityTypeAndEntityId($entityType, $entityId) |
|
167 | |||
168 | /** |
||
169 | * Return's an array with the URL rewrites for the passed entity type, entity and store ID. |
||
170 | * |
||
171 | * @param string $entityType The entity type to load the URL rewrites for |
||
172 | * @param integer $entityId The entity ID to load the URL rewrites for |
||
173 | * @param integer $storeId The store ID to load the URL rewrites for |
||
174 | * |
||
175 | * @return array The URL rewrites |
||
176 | */ |
||
177 | View Code Duplication | public function findAllByEntityTypeAndEntityIdAndStoreId($entityType, $entityId, $storeId) |
|
192 | |||
193 | /** |
||
194 | * Load's and return's the URL rewrite for the given request path and store ID. |
||
195 | * |
||
196 | * @param string $requestPath The request path to load the URL rewrite for |
||
197 | * @param int $storeId The store ID to load the URL rewrite for |
||
198 | * |
||
199 | * @return array|null The URL rewrite found for the given request path and store ID |
||
200 | */ |
||
201 | public function findOneByRequestPathAndStoreId(string $requestPath, int $storeId) |
||
213 | } |
||
214 |
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.