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:
| 1 | <?php |
||
| 25 | abstract class Category |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @return int |
||
| 29 | */ |
||
| 30 | abstract public function getId(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | * @ORM\Column(name="name", type="string", length=255) |
||
| 35 | */ |
||
| 36 | protected $name; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | * @ORM\Column(name="slug", type="string", length=255, unique=true) |
||
| 41 | */ |
||
| 42 | protected $slug; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | * @ORM\Column(name="description", type="text", nullable=true) |
||
| 47 | */ |
||
| 48 | protected $description; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var bool |
||
| 52 | * @ORM\Column(name="enabled", type="boolean") |
||
| 53 | */ |
||
| 54 | protected $enabled = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var \DateTime |
||
| 58 | * |
||
| 59 | * @ORM\Column(name="created_at", type="datetime") |
||
| 60 | */ |
||
| 61 | protected $createdAt; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var Category |
||
| 65 | */ |
||
| 66 | protected $parent; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var Category[]|ArrayCollection |
||
| 70 | */ |
||
| 71 | protected $children; |
||
| 72 | |||
| 73 | public function __toString() |
||
| 77 | |||
| 78 | public function __construct() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function getName() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param string $name |
||
| 94 | * |
||
| 95 | * @return Category |
||
| 96 | */ |
||
| 97 | public function setName($name) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function getDescription() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $description |
||
| 114 | * |
||
| 115 | * @return Category |
||
| 116 | */ |
||
| 117 | public function setDescription($description) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return mixed |
||
| 126 | */ |
||
| 127 | public function getSlug() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param mixed $slug |
||
| 134 | * |
||
| 135 | * @return Category |
||
| 136 | */ |
||
| 137 | public function setSlug($slug) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public function isEnabled() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param bool $enabled |
||
| 154 | * |
||
| 155 | * @return Category |
||
| 156 | */ |
||
| 157 | public function setEnabled($enabled) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return Category |
||
| 166 | */ |
||
| 167 | public function getParent() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param mixed $parent |
||
| 174 | * |
||
| 175 | * @return Category |
||
| 176 | */ |
||
| 177 | public function setParent(Category $parent = null) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return \DateTime |
||
| 192 | */ |
||
| 193 | public function getCreatedAt() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return Category[]|ArrayCollection |
||
| 200 | */ |
||
| 201 | public function getChildren() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param Category $child |
||
| 208 | * |
||
| 209 | * @return Category[] |
||
| 210 | */ |
||
| 211 | public function addChild(Category $child) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param Category $child |
||
| 220 | * |
||
| 221 | * @return Category[] |
||
| 222 | */ |
||
| 223 | public function removeChild(Category $child) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param string $separator |
||
| 232 | * |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | View Code Duplication | public function getTree($separator = '/') |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @ORM\PrePersist() |
||
| 250 | * @ORM\PreUpdate() |
||
| 251 | */ |
||
| 252 | public function updateSlug() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @ORM\PreRemove() |
||
| 261 | * |
||
| 262 | * @param LifecycleEventArgs $event |
||
| 263 | */ |
||
| 264 | View Code Duplication | public function onRemove(LifecycleEventArgs $event) |
|
| 278 | } |
||
| 279 |