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 namespace Limoncello\Passport\Repositories; |
||
30 | abstract class BaseRepository |
||
31 | { |
||
32 | /** |
||
33 | * @return string |
||
34 | */ |
||
35 | abstract protected function getTableNameForReading(): string; |
||
36 | |||
37 | /** |
||
38 | * @return string |
||
39 | */ |
||
40 | abstract protected function getTableNameForWriting(): string; |
||
41 | |||
42 | /** |
||
43 | * @return string |
||
44 | */ |
||
45 | abstract protected function getClassName(): string; |
||
46 | |||
47 | /** |
||
48 | * @return string |
||
49 | */ |
||
50 | abstract protected function getPrimaryKeyName(): string; |
||
51 | |||
52 | /** |
||
53 | * @var Connection |
||
54 | */ |
||
55 | private $connection; |
||
56 | |||
57 | /** |
||
58 | * @var DatabaseSchemaInterface |
||
59 | */ |
||
60 | private $databaseSchema; |
||
61 | |||
62 | /** |
||
63 | * @param Closure $closure |
||
64 | * |
||
65 | * @return void |
||
|
|||
66 | */ |
||
67 | 18 | public function inTransaction(Closure $closure): void |
|
77 | |||
78 | /** |
||
79 | * @return Connection |
||
80 | */ |
||
81 | 26 | protected function getConnection(): Connection |
|
85 | |||
86 | /** |
||
87 | * @param Connection $connection |
||
88 | * |
||
89 | * @return self |
||
90 | */ |
||
91 | 32 | protected function setConnection(Connection $connection): self |
|
97 | |||
98 | /** |
||
99 | * @param array $columns |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | 2 | protected function indexResources(array $columns = ['*']): array |
|
117 | |||
118 | /** |
||
119 | * @param array $values |
||
120 | * |
||
121 | * @return int |
||
122 | */ |
||
123 | 25 | View Code Duplication | protected function createResource(array $values): int |
139 | |||
140 | /** |
||
141 | * @param string|int $identifier |
||
142 | * @param array $columns |
||
143 | * |
||
144 | * @return mixed |
||
145 | */ |
||
146 | 22 | protected function readResource($identifier, array $columns = ['*']) |
|
150 | |||
151 | /** |
||
152 | * @param string|int $identifier |
||
153 | * @param string $column |
||
154 | * @param array $columns |
||
155 | * |
||
156 | * @return mixed |
||
157 | */ |
||
158 | 22 | protected function readResourceByColumn($identifier, string $column, array $columns = ['*']) |
|
173 | |||
174 | /** |
||
175 | * @param string|int $identifier |
||
176 | * @param array $values |
||
177 | * |
||
178 | * @return int |
||
179 | */ |
||
180 | 4 | View Code Duplication | protected function updateResource($identifier, array $values): int |
196 | |||
197 | /** |
||
198 | * @param string|int $identifier |
||
199 | * |
||
200 | * @return int |
||
201 | */ |
||
202 | 4 | protected function deleteResource($identifier): int |
|
215 | |||
216 | /** |
||
217 | * @param string|int $primaryKey |
||
218 | * @param array $foreignKeys |
||
219 | * @param string $intTableName |
||
220 | * @param string $intPrimaryKeyName |
||
221 | * @param string $intForeignKeyName |
||
222 | * |
||
223 | * @return void |
||
224 | */ |
||
225 | 18 | protected function createBelongsToManyRelationship( |
|
244 | |||
245 | /** |
||
246 | * @param string|int $identifier |
||
247 | * @param string $intTableName |
||
248 | * @param string $intPrimaryKeyName |
||
249 | * @param string $intForeignKeyName |
||
250 | * |
||
251 | * @return string[] |
||
252 | */ |
||
253 | 20 | View Code Duplication | protected function readBelongsToManyRelationshipIdentifiers( |
273 | |||
274 | /** |
||
275 | * @param string|int $identifier |
||
276 | * @param string $hasManyTableName |
||
277 | * @param string $hasManyColumn |
||
278 | * @param string $hasManyFkName |
||
279 | * |
||
280 | * @return string[] |
||
281 | */ |
||
282 | 15 | View Code Duplication | protected function readHasManyRelationshipColumn( |
302 | |||
303 | /** |
||
304 | * @param string $intTableName |
||
305 | * @param string $intPrimaryKeyName |
||
306 | * @param string|int $identifier |
||
307 | * |
||
308 | * @return int |
||
309 | */ |
||
310 | 3 | protected function deleteBelongsToManyRelationshipIdentifiers( |
|
327 | |||
328 | /** |
||
329 | * @param DateTimeInterface $dateTime |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | 26 | protected function getDateTimeForDb(DateTimeInterface $dateTime): string |
|
338 | |||
339 | /** |
||
340 | * @return DatabaseSchemaInterface |
||
341 | */ |
||
342 | 28 | protected function getDatabaseSchema(): DatabaseSchemaInterface |
|
343 | { |
||
344 | 28 | return $this->databaseSchema; |
|
345 | } |
||
346 | |||
347 | /** |
||
348 | * @param DatabaseSchemaInterface $databaseSchema |
||
349 | * |
||
350 | * @return self |
||
351 | */ |
||
352 | 32 | protected function setDatabaseSchema(DatabaseSchemaInterface $databaseSchema): self |
|
353 | { |
||
354 | 32 | $this->databaseSchema = $databaseSchema; |
|
355 | |||
356 | 32 | return $this; |
|
357 | } |
||
358 | |||
359 | /** |
||
360 | * @param QueryBuilder $query |
||
361 | * @param mixed $value |
||
362 | * |
||
363 | * @return string |
||
364 | * |
||
365 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
366 | */ |
||
367 | 26 | protected function createTypedParameter(QueryBuilder $query, $value): string |
|
384 | } |
||
385 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.