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 Page 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 Page, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | abstract class Page |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @return int |
||
| 29 | */ |
||
| 30 | abstract public function getId(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | * @ORM\Column(name="title", type="string", length=255) |
||
| 35 | */ |
||
| 36 | protected $title; |
||
| 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="page_content", type="text", nullable=true) |
||
| 47 | */ |
||
| 48 | protected $content; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | * @ORM\Column(name="meta_description", type="string", length=255, nullable=true) |
||
| 53 | */ |
||
| 54 | protected $metaDescription; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | * @ORM\Column(name="meta_title", type="string", length=255, nullable=true) |
||
| 59 | */ |
||
| 60 | protected $metaTitle; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | * @ORM\Column(name="meta_keywords", type="string", length=255, nullable=true) |
||
| 65 | */ |
||
| 66 | protected $metaKeywords; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var Category |
||
| 70 | */ |
||
| 71 | protected $category; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | * @ORM\Column(name="css", type="text", nullable=true) |
||
| 76 | */ |
||
| 77 | protected $css; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | * @ORM\Column(name="js", type="text", nullable=true) |
||
| 82 | */ |
||
| 83 | protected $js; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var \DateTime |
||
| 87 | * |
||
| 88 | * @ORM\Column(name="created_at", type="datetime") |
||
| 89 | */ |
||
| 90 | protected $createdAt; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var bool |
||
| 94 | * @ORM\Column(name="enabled", type="boolean") |
||
| 95 | */ |
||
| 96 | protected $enabled = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var bool |
||
| 100 | * @ORM\Column(name="homepage", type="boolean") |
||
| 101 | */ |
||
| 102 | protected $homepage = false; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | * @ORM\Column(name="host", type="string", length=255, nullable=true) |
||
| 107 | */ |
||
| 108 | protected $host; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var string |
||
| 112 | * @ORM\Column(name="locale", type="string", length=6, nullable=true) |
||
| 113 | */ |
||
| 114 | protected $locale; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var Page |
||
| 118 | */ |
||
| 119 | protected $parent; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var Page[]|ArrayCollection |
||
| 123 | */ |
||
| 124 | protected $children; |
||
| 125 | |||
| 126 | public function __toString() |
||
| 130 | |||
| 131 | public function __construct() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function getTitle() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $title |
||
| 147 | * |
||
| 148 | * @return Page |
||
| 149 | */ |
||
| 150 | public function setTitle($title) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public function getSlug() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param string $slug |
||
| 167 | * |
||
| 168 | * @return Page |
||
| 169 | */ |
||
| 170 | public function setSlug($slug) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getContent() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $content |
||
| 187 | * |
||
| 188 | * @return Page |
||
| 189 | */ |
||
| 190 | public function setContent($content) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function getMetaDescription() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param string $metaDescription |
||
| 207 | * |
||
| 208 | * @return Page |
||
| 209 | */ |
||
| 210 | public function setMetaDescription($metaDescription) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | public function getMetaTitle() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $metaTitle |
||
| 227 | * |
||
| 228 | * @return Page |
||
| 229 | */ |
||
| 230 | public function setMetaTitle($metaTitle) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public function getMetaKeywords() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param string $metaKeywords |
||
| 247 | * |
||
| 248 | * @return Page |
||
| 249 | */ |
||
| 250 | public function setMetaKeywords($metaKeywords) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return Category |
||
| 259 | */ |
||
| 260 | public function getCategory() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param Category $category |
||
| 267 | * |
||
| 268 | * @return Page |
||
| 269 | */ |
||
| 270 | public function setCategory($category) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public function getCss() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param string $css |
||
| 287 | * |
||
| 288 | * @return Page |
||
| 289 | */ |
||
| 290 | public function setCss($css) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public function getJs() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $js |
||
| 307 | * |
||
| 308 | * @return Page |
||
| 309 | */ |
||
| 310 | public function setJs($js) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return \DateTime |
||
| 319 | */ |
||
| 320 | public function getCreatedAt() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | public function isEnabled() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param bool $enabled |
||
| 335 | * |
||
| 336 | * @return Page |
||
| 337 | */ |
||
| 338 | public function setEnabled($enabled) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @return Page |
||
| 347 | */ |
||
| 348 | public function getParent() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param Page|null $parent |
||
| 355 | * |
||
| 356 | * @return Page |
||
| 357 | */ |
||
| 358 | public function setParent(Page $parent = null) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @return Page[]|ArrayCollection |
||
| 373 | */ |
||
| 374 | public function getChildren() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param Page $page |
||
| 381 | * |
||
| 382 | * @return Page |
||
| 383 | */ |
||
| 384 | public function addChild(Page $page) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param Page $page |
||
| 393 | * |
||
| 394 | * @return Page |
||
| 395 | */ |
||
| 396 | public function removeChild(Page $page) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @return bool |
||
| 405 | */ |
||
| 406 | public function isHomepage() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param bool $homepage |
||
| 413 | * |
||
| 414 | * @return Page |
||
| 415 | */ |
||
| 416 | public function setHomepage($homepage) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public function getHost() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param string $host |
||
| 433 | * |
||
| 434 | * @return Page |
||
| 435 | */ |
||
| 436 | public function setHost($host) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | public function getLocale() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param string $locale |
||
| 453 | * |
||
| 454 | * @return Page |
||
| 455 | */ |
||
| 456 | public function setLocale($locale) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param string $separator |
||
| 465 | * |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | View Code Duplication | public function getTree($separator = '/') |
|
| 480 | |||
| 481 | /** |
||
| 482 | * @ORM\PrePersist() |
||
| 483 | * @ORM\PreUpdate() |
||
| 484 | */ |
||
| 485 | public function updateSlug() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @ORM\PreRemove() |
||
| 494 | * |
||
| 495 | * @param LifecycleEventArgs $event |
||
| 496 | */ |
||
| 497 | View Code Duplication | public function onRemove(LifecycleEventArgs $event) |
|
| 511 | } |
||
| 512 |