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:
Complex classes like BaseEntity often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseEntity, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | abstract class BaseEntity implements BaseEntityInterface, EntityTimestampInterface |
||
|
|||
17 | { |
||
18 | /** |
||
19 | * @ODM\Id |
||
20 | */ |
||
21 | protected $id; |
||
22 | |||
23 | /** |
||
24 | * @var \DateTime |
||
25 | * |
||
26 | * @ODM\Field(type="timestamp", name="createdAt") |
||
27 | */ |
||
28 | protected $createdAt; |
||
29 | |||
30 | /** |
||
31 | * @var \DateTime |
||
32 | * |
||
33 | * @ODM\Field(type="timestamp", name="updatedAt") |
||
34 | */ |
||
35 | protected $updatedAt; |
||
36 | |||
37 | /** |
||
38 | * @ODM\Field(type="timestamp", nullable=true, name="deletedAt") |
||
39 | */ |
||
40 | protected $deletedAt; |
||
41 | |||
42 | public function getCreatedAt() |
||
46 | |||
47 | public function getUpdatedAt() |
||
51 | |||
52 | public function getDeletedAt() |
||
56 | |||
57 | public function setDeletedAt($deletedAt) |
||
61 | |||
62 | /** |
||
63 | * Altera o campo updatedAt para forçar o persist da entity. |
||
64 | */ |
||
65 | public function forcePersist() |
||
71 | |||
72 | /** |
||
73 | * @ODM\PrePersist |
||
74 | */ |
||
75 | View Code Duplication | public function prePersist() |
|
87 | |||
88 | /** |
||
89 | * @ODM\PostPersist |
||
90 | */ |
||
91 | View Code Duplication | public function postPersist() |
|
103 | |||
104 | /** |
||
105 | * @ODM\PreUpdate |
||
106 | */ |
||
107 | View Code Duplication | public function preUpdate() |
|
119 | |||
120 | /** |
||
121 | * @ODM\PostUpdate |
||
122 | */ |
||
123 | View Code Duplication | public function postUpdate() |
|
135 | |||
136 | /** |
||
137 | * @ORM\PreFlush |
||
138 | */ |
||
139 | public function preFlush() |
||
147 | |||
148 | public function getRepository() |
||
152 | |||
153 | public function save($flush = false) |
||
159 | |||
160 | public function remove() |
||
166 | |||
167 | public function flush($all = true) |
||
173 | |||
174 | public function getId() |
||
178 | |||
179 | protected function getFillable() |
||
183 | |||
184 | /** |
||
185 | * Retona um array com o nome das propriedade que o cliente pode setar para realizar o store |
||
186 | * É usado principalmente em $this->setPropertiesEntity e nos Controllers. |
||
187 | * Este método não evita que uma propriedade seja alterada caso tenha seu método set(). |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | abstract public function getOnlyStore(); |
||
192 | |||
193 | /** |
||
194 | * Retona um array com o nome das propriedade que o cliente pode setar para realizar o update. |
||
195 | * Por padrão retorna os mesmos valores de $this->getOnlyStore(). |
||
196 | * Este método pode ser sobrescrito nas classes filhas. |
||
197 | * É usado principalmente em $this->setPropertiesEntity e nos Controllers. |
||
198 | * Este método não evita que uma propriedade seja alterada caso tenha seu método set(). |
||
199 | * |
||
200 | * @return array |
||
201 | */ |
||
202 | public function getOnlyUpdate() |
||
206 | |||
207 | public function setPropertiesEntity(array $data) |
||
229 | |||
230 | View Code Duplication | final protected function checkOnyExceptInArray($key, array $options = null) |
|
246 | |||
247 | public function toArray(array $options = null) |
||
284 | } |
||
285 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.