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 |
||
43 | class UrlRewriteRepository extends AbstractFinderRepository implements UrlRewriteRepositoryInterface |
||
44 | { |
||
45 | |||
46 | /** |
||
47 | * The registry processor instance. |
||
48 | * |
||
49 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
50 | */ |
||
51 | protected $registryProcessor; |
||
52 | |||
53 | /** |
||
54 | * The prepared statement to load the existing URL rewrites. |
||
55 | * |
||
56 | * @var \PDOStatement |
||
57 | */ |
||
58 | protected $urlRewritesStmt; |
||
59 | |||
60 | /** |
||
61 | * The prepared statement to load the existing URL rewrites by their entity type and ID. |
||
62 | * |
||
63 | * @var \PDOStatement |
||
64 | */ |
||
65 | protected $urlRewritesByEntityTypeAndEntityIdStmt; |
||
66 | |||
67 | /** |
||
68 | * The prepared statement to load the existing URL rewrites by their entity type, entity and store ID. |
||
69 | * |
||
70 | * @var \PDOStatement |
||
71 | */ |
||
72 | protected $urlRewritesByEntityTypeAndEntityIdAndStoreIdStmt; |
||
73 | |||
74 | /** |
||
75 | * The prefix to load the URL rewrites with the given request path and store ID from the registry. |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $urlRewritesByRequestPathAndStoreIdPrefix; |
||
80 | |||
81 | /** |
||
82 | * The core config data loader instance. |
||
83 | * |
||
84 | * @var \TechDivision\Import\Loaders\LoaderInterface |
||
85 | */ |
||
86 | protected $coreConfigDataLoader; |
||
87 | |||
88 | |||
89 | /** |
||
90 | * The array with the entity type > configuration key mapping. |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $entityTypeConfigKeyMapping = array( |
||
95 | 'product' => CoreConfigDataKeys::CATALOG_SEO_PRODUCT_URL_SUFFIX, |
||
96 | 'category' => CoreConfigDataKeys::CATALOG_SEO_CATEGORY_URL_SUFFIX |
||
97 | ); |
||
98 | |||
99 | /** |
||
100 | * The array with the entity type and store view specific suffixes. |
||
101 | * |
||
102 | * @var array |
||
103 | */ |
||
104 | protected $suffixes = array(); |
||
105 | |||
106 | /** |
||
107 | * Initialize the repository with the passed connection and utility class name. |
||
108 | * . |
||
109 | * @param \TechDivision\Import\Connection\ConnectionInterface $connection The connection instance |
||
110 | * @param \TechDivision\Import\Repositories\SqlStatementRepositoryInterface $sqlStatementRepository The SQL repository instance |
||
111 | * @param \TechDivision\Import\Repositories\Finders\FinderFactoryInterface $finderFactory The finder factory instance |
||
112 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
113 | * @param \TechDivision\Import\Loaders\LoaderInterface $coreConfigDataLoader The core config data loader instance |
||
114 | */ |
||
115 | public function __construct( |
||
130 | |||
131 | /** |
||
132 | * Initializes the repository's prepared statements. |
||
133 | * |
||
134 | * @return void |
||
135 | */ |
||
136 | public function init() |
||
165 | |||
166 | /** |
||
167 | * Return's the finder's entity name. |
||
168 | * |
||
169 | * @return string The finder's entity name |
||
170 | */ |
||
171 | public function getEntityName() |
||
175 | |||
176 | /** |
||
177 | * Return's the primary key name of the entity. |
||
178 | * |
||
179 | * @return string The name of the entity's primary key |
||
180 | */ |
||
181 | public function getPrimaryKeyName() |
||
185 | |||
186 | /** |
||
187 | * Return's an array with the available URL rewrites. |
||
188 | * |
||
189 | * @return array The available URL rewrites |
||
190 | */ |
||
191 | public function findAll() |
||
197 | |||
198 | /** |
||
199 | * Return's an array with the available URL rewrites |
||
200 | * |
||
201 | * @return array The array with the rewrites, grouped by request path and store ID |
||
202 | */ |
||
203 | public function findAllGroupedByRequestPathAndStoreId() |
||
237 | |||
238 | /** |
||
239 | * Return's an array with the URL rewrites for the passed entity type and ID. |
||
240 | * |
||
241 | * @param string $entityType The entity type to load the URL rewrites for |
||
242 | * @param integer $entityId The entity ID to load the URL rewrites for |
||
243 | * |
||
244 | * @return array The URL rewrites |
||
245 | */ |
||
246 | View Code Duplication | public function findAllByEntityTypeAndEntityId($entityType, $entityId) |
|
260 | |||
261 | /** |
||
262 | * Return's an array with the URL rewrites for the passed entity type, entity and store ID. |
||
263 | * |
||
264 | * @param string $entityType The entity type to load the URL rewrites for |
||
265 | * @param integer $entityId The entity ID to load the URL rewrites for |
||
266 | * @param integer $storeId The store ID to load the URL rewrites for |
||
267 | * |
||
268 | * @return array The URL rewrites |
||
269 | */ |
||
270 | View Code Duplication | public function findAllByEntityTypeAndEntityIdAndStoreId($entityType, $entityId, $storeId) |
|
285 | |||
286 | /** |
||
287 | * Load's and return's the URL rewrite for the given request path and store ID. |
||
288 | * |
||
289 | * ATTENTION: This method access the registry to make sure the parallel processes will access |
||
290 | * the same URL rewrites. The initial data the will be added the registry will be loaded with |
||
291 | * the method `UrlRewriteRepository::findAllGroupedByRequestPathAndStoreId()` |
||
292 | * |
||
293 | * @param string $requestPath The request path to load the URL rewrite for |
||
294 | * @param int $storeId The store ID to load the URL rewrite for |
||
295 | * |
||
296 | * @return array|null The URL rewrite found for the given request path and store ID |
||
297 | * @see \TechDivision\Import\Repositories\UrlRewriteRepository::findAllGroupedByRequestPathAndStoreId() |
||
298 | */ |
||
299 | public function findOneByUrlRewriteByRequestPathAndStoreId(string $requestPath, int $storeId) |
||
308 | } |
||
309 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.