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 |
||
7 | abstract class AbstractDoctrineRepository implements RepositoryInterface |
||
8 | { |
||
9 | /** |
||
10 | * @var Connection |
||
11 | */ |
||
12 | private $connection; |
||
13 | |||
14 | /** |
||
15 | * @param Connection $connection |
||
16 | */ |
||
17 | public function __construct(Connection $connection) |
||
21 | |||
22 | /** |
||
23 | * @param string $id |
||
24 | * |
||
25 | * @return ModelInterface|null |
||
26 | */ |
||
27 | public function find(string $id) |
||
42 | |||
43 | /** |
||
44 | * @param array $criteria |
||
45 | * |
||
46 | * @return null|ModelInterface |
||
47 | */ |
||
48 | public function findOneBy(array $criteria = []) |
||
68 | |||
69 | /** |
||
70 | * @param array $criteria |
||
71 | * |
||
72 | * @return ModelInterface[]|array |
||
73 | */ |
||
74 | public function findBy(array $criteria = []): array |
||
100 | |||
101 | /** |
||
102 | * @param ModelInterface $model |
||
103 | */ |
||
104 | public function insert(ModelInterface $model) |
||
108 | |||
109 | /** |
||
110 | * @param ModelInterface $model |
||
111 | */ |
||
112 | public function update(ModelInterface $model) |
||
116 | |||
117 | /** |
||
118 | * @param ModelInterface $model |
||
119 | */ |
||
120 | public function delete(ModelInterface $model) |
||
124 | |||
125 | /** |
||
126 | * @return string |
||
127 | */ |
||
128 | abstract protected function getTablename(): string; |
||
129 | } |
||
130 |
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.