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 |
||
20 | abstract class BaseEntity implements BaseEntityInterface, EntityTimestampInterface |
||
|
|||
21 | { |
||
22 | use SetPropertiesEntityTrait; |
||
23 | |||
24 | /** |
||
25 | * @ORM\Id |
||
26 | * @ORM\Column(type="integer", name="id") |
||
27 | * @ORM\GeneratedValue |
||
28 | */ |
||
29 | protected $id; |
||
30 | |||
31 | /** |
||
32 | * @var \DateTime |
||
33 | * |
||
34 | * @Gedmo\Timestampable(on="create") |
||
35 | * @ORM\Column(type="datetime", name="createdAt") |
||
36 | */ |
||
37 | private $createdAt; |
||
38 | |||
39 | /** |
||
40 | * @var \DateTime |
||
41 | * |
||
42 | * @Gedmo\Timestampable(on="update") |
||
43 | * @ORM\Column(type="datetime", name="updatedAt") |
||
44 | */ |
||
45 | private $updatedAt; |
||
46 | |||
47 | /** |
||
48 | * @ORM\Column(type="datetime", nullable=true, name="deletedAt") |
||
49 | */ |
||
50 | private $deletedAt; |
||
51 | |||
52 | public function getCreatedAt() |
||
56 | |||
57 | public function getUpdatedAt() |
||
61 | |||
62 | public function getDeletedAt() |
||
66 | |||
67 | public function setDeletedAt($deletedAt) |
||
71 | |||
72 | public function getDiscr() |
||
76 | |||
77 | public function getDiscrName() |
||
81 | |||
82 | /** |
||
83 | * Altera o campo updatedAt para forçar o persist da entity. |
||
84 | */ |
||
85 | public function forcePersist() |
||
91 | |||
92 | /** |
||
93 | * @ORM\PrePersist |
||
94 | */ |
||
95 | public function prePersist() |
||
101 | |||
102 | /** |
||
103 | * @ORM\PostPersist |
||
104 | */ |
||
105 | public function postPersist() |
||
110 | |||
111 | /** |
||
112 | * @ORM\PreUpdate |
||
113 | */ |
||
114 | public function preUpdate() |
||
120 | |||
121 | /** |
||
122 | * @ORM\PostUpdate |
||
123 | */ |
||
124 | public function postUpdate() |
||
129 | |||
130 | /** |
||
131 | * @ORM\PreFlush |
||
132 | */ |
||
133 | public function preFlush() |
||
138 | |||
139 | public function getRepository() |
||
143 | |||
144 | public function remove() |
||
150 | |||
151 | public function save($flush = false) |
||
157 | |||
158 | public function flush($all = true) |
||
164 | |||
165 | public function getId() |
||
169 | |||
170 | protected function getFillable() |
||
174 | |||
175 | /** |
||
176 | * Retona um array com o nome das propriedade que o cliente pode setar para realizar o store |
||
177 | * É usado principalmente em $this->setPropertiesEntity e nos Controllers. |
||
178 | * Este método não evita que uma propriedade seja alterada caso tenha seu método set(). |
||
179 | * |
||
180 | * @return array |
||
181 | */ |
||
182 | abstract public function getOnlyStore(); |
||
183 | |||
184 | /** |
||
185 | * Retona um array com o nome das propriedade que o cliente pode setar para realizar o update. |
||
186 | * Por padrão retorna os mesmos valores de $this->getOnlyStore(). |
||
187 | * Este método pode ser sobrescrito nas classes filhas. |
||
188 | * É usado principalmente em $this->setPropertiesEntity e nos Controllers. |
||
189 | * Este método não evita que uma propriedade seja alterada caso tenha seu método set(). |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | public function getOnlyUpdate() |
||
197 | |||
198 | View Code Duplication | final protected function checkOnyExceptInArray($key, array $options = null) |
|
214 | |||
215 | public function toArray(array $options = null) |
||
266 | } |
||
267 |
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.