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 |
||
11 | View Code Duplication | abstract class BaseRepository extends DocumentRepository implements BaseRepositoryInterface |
|
12 | { |
||
13 | /** |
||
14 | * Método executado nos eventos ORM\PrePersist e ORM\PreUpdate. |
||
15 | */ |
||
16 | public function preSave(BaseEntityInterface $entity) |
||
20 | |||
21 | /** |
||
22 | * Método executado nos eventos ORM\PostPersist e ORM\PostUpdate. |
||
23 | */ |
||
24 | public function postSave(BaseEntityInterface $entity) |
||
28 | |||
29 | /** |
||
30 | * Método executado no evento ORM\PreFlush. |
||
31 | */ |
||
32 | public function preFlush(BaseEntityInterface $entity) |
||
36 | |||
37 | abstract public function getMessageNotFound(); |
||
38 | |||
39 | public function validate(BaseEntityInterface $entity) |
||
59 | |||
60 | public function getClassMetadata() |
||
64 | |||
65 | public function getEntityName() |
||
69 | |||
70 | public function createEntity() |
||
74 | |||
75 | public function createQueryWorker() |
||
79 | |||
80 | public function query() |
||
84 | |||
85 | /** |
||
86 | * @return QueryWorker |
||
87 | */ |
||
88 | public function findAll() |
||
92 | |||
93 | public function findOneBy(array $filters, $abort = true) |
||
103 | |||
104 | public function find($id, $abort = true) |
||
108 | |||
109 | /** |
||
110 | * Inserir ou atualizar um registro. |
||
111 | * |
||
112 | * @param null | string | int | array |
||
113 | * |
||
114 | * @throws InvalidArgumentException Se $input não for null | string | int | array é lançada a exceção |
||
115 | */ |
||
116 | public function findOrCreate($input) |
||
142 | |||
143 | /** |
||
144 | * Marcar um registro como deletado. |
||
145 | * |
||
146 | * @param object | int $target |
||
147 | * |
||
148 | * @throws Symfony\Component\HttpKernel\Exception\NotFoundHttpException Se $target não for encontrado |
||
149 | * |
||
150 | * @return Bludata\Entities\BaseEntityInterface |
||
151 | */ |
||
152 | public function remove($target) |
||
160 | |||
161 | /** |
||
162 | * @param Bludata\Entities\BaseEntityInterface $entity |
||
163 | * |
||
164 | * @return Bludata\Repositories\QueryWorker |
||
165 | */ |
||
166 | public function save(BaseEntityInterface $entity) |
||
172 | |||
173 | /** |
||
174 | * @param Bludata\Entities\BaseEntityInterface $entity |
||
175 | * |
||
176 | * @return Bludata\Repositories\QueryWorker |
||
177 | */ |
||
178 | public function flush(BaseEntityInterface $entity = null) |
||
184 | |||
185 | public function em() |
||
189 | } |
||
190 |
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.