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 |
||
| 19 | abstract class BaseEntity implements BaseEntityInterface, EntityTimestampInterface |
||
|
|
|||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @ORM\Id |
||
| 23 | * @ORM\Column(type="integer", name="id") |
||
| 24 | * @ORM\GeneratedValue |
||
| 25 | */ |
||
| 26 | protected $id; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var \DateTime |
||
| 30 | * |
||
| 31 | * @Gedmo\Timestampable(on="create") |
||
| 32 | * @ORM\Column(type="datetime", name="createdAt") |
||
| 33 | */ |
||
| 34 | private $createdAt; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var \DateTime |
||
| 38 | * |
||
| 39 | * @Gedmo\Timestampable(on="update") |
||
| 40 | * @ORM\Column(type="datetime", name="updatedAt") |
||
| 41 | */ |
||
| 42 | private $updatedAt; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @ORM\Column(type="datetime", nullable=true, name="deletedAt") |
||
| 46 | */ |
||
| 47 | private $deletedAt; |
||
| 48 | |||
| 49 | public function getCreatedAt() |
||
| 53 | |||
| 54 | public function getUpdatedAt() |
||
| 58 | |||
| 59 | public function getDeletedAt() |
||
| 63 | |||
| 64 | public function setDeletedAt($deletedAt) |
||
| 68 | |||
| 69 | public function getDiscr() |
||
| 73 | |||
| 74 | public function getDiscrName() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Altera o campo updatedAt para forçar o persist da entity. |
||
| 81 | */ |
||
| 82 | public function forcePersist() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @ORM\PrePersist |
||
| 91 | */ |
||
| 92 | public function prePersist() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @ORM\PreUpdate |
||
| 101 | */ |
||
| 102 | public function preUpdate() |
||
| 108 | |||
| 109 | public function getRepository() |
||
| 113 | |||
| 114 | public function remove() |
||
| 120 | |||
| 121 | public function save($flush = false) |
||
| 127 | |||
| 128 | public function flush($all = true) |
||
| 134 | |||
| 135 | public function getId() |
||
| 139 | |||
| 140 | protected function getFillable() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Retona um array com o nome das propriedade que o cliente pode setar para realizar o store |
||
| 147 | * É usado principalmente em $this->setPropertiesEntity e nos Controllers. |
||
| 148 | * Este método não evita que uma propriedade seja alterada caso tenha seu método set(). |
||
| 149 | * |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | abstract public function getOnlyStore(); |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Retona um array com o nome das propriedade que o cliente pode setar para realizar o update. |
||
| 156 | * Por padrão retorna os mesmos valores de $this->getOnlyStore(). |
||
| 157 | * Este método pode ser sobrescrito nas classes filhas. |
||
| 158 | * É usado principalmente em $this->setPropertiesEntity e nos Controllers. |
||
| 159 | * Este método não evita que uma propriedade seja alterada caso tenha seu método set(). |
||
| 160 | * |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | public function getOnlyUpdate() |
||
| 167 | |||
| 168 | public function setPropertiesEntity(array $data) |
||
| 190 | |||
| 191 | View Code Duplication | final protected function checkOnyExceptInArray($key, array $options = null) |
|
| 207 | |||
| 208 | public function toArray(array $options = null) |
||
| 259 | } |
||
| 260 |
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.