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 | public function prePersist() |
||
81 | |||
82 | /** |
||
83 | * @ODM\PreUpdate |
||
84 | */ |
||
85 | public function preUpdate() |
||
91 | |||
92 | public function getRepository() |
||
96 | |||
97 | public function save($flush = false) |
||
103 | |||
104 | public function remove() |
||
110 | |||
111 | public function flush($all = true) |
||
117 | |||
118 | public function getId() |
||
122 | |||
123 | protected function getFillable() |
||
127 | |||
128 | /** |
||
129 | * Retona um array com o nome das propriedade que o cliente pode setar para realizar o store |
||
130 | * É usado principalmente em $this->setPropertiesEntity e nos Controllers. |
||
131 | * Este método não evita que uma propriedade seja alterada caso tenha seu método set(). |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | abstract public function getOnlyStore(); |
||
136 | |||
137 | /** |
||
138 | * Retona um array com o nome das propriedade que o cliente pode setar para realizar o update. |
||
139 | * Por padrão retorna os mesmos valores de $this->getOnlyStore(). |
||
140 | * Este método pode ser sobrescrito nas classes filhas. |
||
141 | * É usado principalmente em $this->setPropertiesEntity e nos Controllers. |
||
142 | * Este método não evita que uma propriedade seja alterada caso tenha seu método set(). |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | public function getOnlyUpdate() |
||
150 | |||
151 | public function setPropertiesEntity(array $data) |
||
173 | |||
174 | View Code Duplication | final protected function checkOnyExceptInArray($key, array $options = null) |
|
190 | |||
191 | public function toArray(array $options = null) |
||
240 | } |
||
241 |
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.