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 | public function getRepository() |
||
| 134 | |||
| 135 | public function remove() |
||
| 141 | |||
| 142 | public function save($flush = false) |
||
| 148 | |||
| 149 | public function flush($all = true) |
||
| 155 | |||
| 156 | public function getId() |
||
| 160 | |||
| 161 | protected function getFillable() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Retona um array com o nome das propriedade que o cliente pode setar para realizar o store |
||
| 168 | * É usado principalmente em $this->setPropertiesEntity e nos Controllers. |
||
| 169 | * Este método não evita que uma propriedade seja alterada caso tenha seu método set(). |
||
| 170 | * |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | abstract public function getOnlyStore(); |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Retona um array com o nome das propriedade que o cliente pode setar para realizar o update. |
||
| 177 | * Por padrão retorna os mesmos valores de $this->getOnlyStore(). |
||
| 178 | * Este método pode ser sobrescrito nas classes filhas. |
||
| 179 | * É usado principalmente em $this->setPropertiesEntity e nos Controllers. |
||
| 180 | * Este método não evita que uma propriedade seja alterada caso tenha seu método set(). |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public function getOnlyUpdate() |
||
| 188 | |||
| 189 | View Code Duplication | final protected function checkOnyExceptInArray($key, array $options = null) |
|
| 205 | |||
| 206 | public function toArray(array $options = null) |
||
| 257 | } |
||
| 258 |
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.