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 |
||
17 | abstract class AbstractDoctrineRepository implements RepositoryInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var Connection |
||
21 | */ |
||
22 | protected $connection; |
||
23 | |||
24 | /** |
||
25 | * @var ModelCacheInterface |
||
26 | */ |
||
27 | protected $cache; |
||
28 | |||
29 | /** |
||
30 | * @var LoggerInterface |
||
31 | */ |
||
32 | protected $logger; |
||
33 | |||
34 | /** |
||
35 | * @param Connection $connection |
||
36 | * @param ModelCacheInterface|null $cache |
||
37 | * @param LoggerInterface|null $logger |
||
38 | */ |
||
39 | public function __construct( |
||
48 | |||
49 | /** |
||
50 | * @param string $id |
||
51 | * |
||
52 | * @return ModelInterface|null |
||
53 | */ |
||
54 | public function find(string $id) |
||
81 | |||
82 | /** |
||
83 | * @param array $criteria |
||
84 | * |
||
85 | * @return null|ModelInterface |
||
86 | */ |
||
87 | public function findOneBy(array $criteria) |
||
112 | |||
113 | /** |
||
114 | * @param array $criteria |
||
115 | * |
||
116 | * @return ModelInterface[]|array |
||
117 | */ |
||
118 | public function findBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null): array |
||
153 | |||
154 | /** |
||
155 | * @param array $criteria |
||
156 | * |
||
157 | * @return QueryBuilder |
||
158 | */ |
||
159 | private function getFindByQueryBuilder(array $criteria = []): QueryBuilder |
||
171 | |||
172 | /** |
||
173 | * @param ModelInterface $model |
||
174 | */ |
||
175 | public function persist(ModelInterface $model) |
||
176 | { |
||
177 | $this->logger->info( |
||
178 | 'model: persist model {model} with id {id}', |
||
179 | ['model' => get_class($model), 'id' => $model->getId()] |
||
180 | ); |
||
181 | |||
182 | $row = $model->toPersistence(); |
||
183 | foreach ($row as $field => $value) { |
||
184 | if ($value instanceof ModelCollectionInterface || $value instanceof ModelInterface) { |
||
185 | unset($row[$field]); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | if (null === $this->find($model->getId())) { |
||
190 | $this->connection->insert($this->getTable(), $row); |
||
191 | } else { |
||
192 | $this->connection->update($this->getTable(), $row, ['id' => $model->getId()]); |
||
193 | } |
||
194 | |||
195 | $this->cache->set($model->getId(), $row); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param ModelInterface $model |
||
200 | */ |
||
201 | public function remove(ModelInterface $model) |
||
212 | |||
213 | /** |
||
214 | * @param array $row |
||
215 | * |
||
216 | * @return ModelInterface |
||
217 | */ |
||
218 | protected function fromPersistence(array $row): ModelInterface |
||
219 | { |
||
220 | /** @var ModelInterface $modelClass */ |
||
221 | $modelClass = $this->getModelClass(); |
||
222 | |||
223 | return $modelClass::fromPersistence($row); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @return string |
||
228 | */ |
||
229 | abstract protected function getTable(): string; |
||
230 | } |
||
231 |
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.