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 | public function getRepository() |
||
| 140 | |||
| 141 | public function save($flush = false) |
||
| 147 | |||
| 148 | public function remove() |
||
| 154 | |||
| 155 | public function flush($all = true) |
||
| 161 | |||
| 162 | public function getId() |
||
| 166 | |||
| 167 | protected function getFillable() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Retona um array com o nome das propriedade que o cliente pode setar para realizar o store |
||
| 174 | * É usado principalmente em $this->setPropertiesEntity e nos Controllers. |
||
| 175 | * Este método não evita que uma propriedade seja alterada caso tenha seu método set(). |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | abstract public function getOnlyStore(); |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Retona um array com o nome das propriedade que o cliente pode setar para realizar o update. |
||
| 183 | * Por padrão retorna os mesmos valores de $this->getOnlyStore(). |
||
| 184 | * Este método pode ser sobrescrito nas classes filhas. |
||
| 185 | * É usado principalmente em $this->setPropertiesEntity e nos Controllers. |
||
| 186 | * Este método não evita que uma propriedade seja alterada caso tenha seu método set(). |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | public function getOnlyUpdate() |
||
| 194 | |||
| 195 | public function setPropertiesEntity(array $data) |
||
| 217 | |||
| 218 | View Code Duplication | final protected function checkOnyExceptInArray($key, array $options = null) |
|
| 234 | |||
| 235 | public function toArray(array $options = null) |
||
| 272 | } |
||
| 273 |
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.