Complex classes like Article 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 Article, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Article |
||
| 24 | { |
||
| 25 | use BusinessEntityTrait; |
||
| 26 | use TimestampableEntity; |
||
| 27 | use Translatable; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @VIC\BusinessProperty("businessParameter") |
||
| 31 | * @ORM\Column(name="id", type="integer") |
||
| 32 | * @ORM\Id |
||
| 33 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 34 | */ |
||
| 35 | private $id; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @deprecated |
||
| 39 | * Title is inherited from Page, just add the BusinessProperty annotation. |
||
| 40 | * |
||
| 41 | * @ORM\Column(name="name", type="string", length=255, nullable=true) |
||
| 42 | */ |
||
| 43 | private $name; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @deprecated |
||
| 47 | * @ORM\Column(name="slug", type="string", length=255, nullable=true) |
||
| 48 | */ |
||
| 49 | private $slug; |
||
|
|
|||
| 50 | |||
| 51 | /** |
||
| 52 | * @deprecated |
||
| 53 | * Description is inherited from Page, just add the BusinessProperty annotation. |
||
| 54 | * |
||
| 55 | * @ORM\Column(name="description", type="text", nullable=true) |
||
| 56 | */ |
||
| 57 | private $description; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @ORM\Column(name="status", type="string", nullable=false) |
||
| 61 | */ |
||
| 62 | protected $status; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Categories of the article. |
||
| 66 | * |
||
| 67 | * @ORM\ManyToOne(targetEntity="Category", inversedBy="articles") |
||
| 68 | * @ORM\JoinColumn(onDelete="SET NULL") |
||
| 69 | * @VIC\BusinessProperty({"textable", "seoable"}) |
||
| 70 | */ |
||
| 71 | private $category; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var datetime |
||
| 75 | * |
||
| 76 | * @ORM\Column(name="publishedAt", type="datetime", nullable=true) |
||
| 77 | * @VIC\BusinessProperty({"dateable", "textable"}) |
||
| 78 | */ |
||
| 79 | private $publishedAt; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * This relation is dynamically added by ArticleSubscriber |
||
| 83 | * The property is needed here. |
||
| 84 | * |
||
| 85 | * @VIC\BusinessProperty({"textable", "seoable"}) |
||
| 86 | */ |
||
| 87 | private $author; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Tags of the article. |
||
| 91 | * |
||
| 92 | * @ORM\ManyToMany(targetEntity="Tag", inversedBy="articles") |
||
| 93 | * @ORM\JoinTable(name="vic_article_tags") |
||
| 94 | * @Assert\Valid() |
||
| 95 | */ |
||
| 96 | private $tags; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\BlogBundle\Entity\Blog", inversedBy="articles", cascade={"persist"}) |
||
| 100 | * @ORM\JoinColumn(name="blog_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 101 | */ |
||
| 102 | private $blog; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var BusinessTemplate |
||
| 106 | * @ORM\ManyToOne(targetEntity="ArticleTemplate") |
||
| 107 | * @ORM\JoinColumn(name="template_id", referencedColumnName="id", onDelete="SET NULL") |
||
| 108 | * @Assert\NotNull() |
||
| 109 | */ |
||
| 110 | private $template; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var string |
||
| 114 | * |
||
| 115 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\MediaBundle\Entity\Media") |
||
| 116 | * @ORM\JoinColumn(name="image_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 117 | * @VIC\BusinessProperty("imageable") |
||
| 118 | */ |
||
| 119 | private $image; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @VIC\BusinessProperty("textable") |
||
| 123 | */ |
||
| 124 | private $categoryTitle; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @VIC\BusinessProperty("textable") |
||
| 128 | */ |
||
| 129 | private $publishedAtString; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @VIC\BusinessProperty("textable") |
||
| 133 | */ |
||
| 134 | private $authorAvatar; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @VIC\BusinessProperty("textable") |
||
| 138 | */ |
||
| 139 | private $authorFullName; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @ORM\Column(name="deletedAt", type="datetime", nullable=true) |
||
| 143 | */ |
||
| 144 | private $deletedAt; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @Gedmo\Locale |
||
| 148 | */ |
||
| 149 | protected $locale; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * to string method. |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public function __toString() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Constructor. |
||
| 163 | */ |
||
| 164 | public function __construct() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get id. |
||
| 171 | * |
||
| 172 | * @return int |
||
| 173 | */ |
||
| 174 | public function getId() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Set id. |
||
| 181 | * |
||
| 182 | * @param int $id |
||
| 183 | */ |
||
| 184 | public function setId($id) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get category. |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | public function getCategory() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get the published at property. |
||
| 201 | * |
||
| 202 | * @return \DateTime |
||
| 203 | */ |
||
| 204 | public function getPublishedAt() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Set publishedAt. |
||
| 215 | * |
||
| 216 | * @param \DateTime $publishedAt |
||
| 217 | * |
||
| 218 | * @return $this |
||
| 219 | */ |
||
| 220 | public function setPublishedAt($publishedAt) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get deletedAt. |
||
| 229 | * |
||
| 230 | * @return \DateTime |
||
| 231 | */ |
||
| 232 | public function getDeletedAt() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Set deletedAt. |
||
| 239 | * |
||
| 240 | * @param \DateTime $deletedAt |
||
| 241 | * |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | public function setDeletedAt($deletedAt) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get the blog. |
||
| 253 | * |
||
| 254 | * @return Blog |
||
| 255 | */ |
||
| 256 | public function getBlog() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Set the blog. |
||
| 263 | * |
||
| 264 | * @param Blog $blog |
||
| 265 | */ |
||
| 266 | public function setBlog(Blog $blog) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Set tags. |
||
| 273 | * |
||
| 274 | * @param string $tags |
||
| 275 | * |
||
| 276 | * @return Article |
||
| 277 | */ |
||
| 278 | public function setTags($tags) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Add tag. |
||
| 287 | * |
||
| 288 | * @param string $tag |
||
| 289 | * |
||
| 290 | * @return Article |
||
| 291 | */ |
||
| 292 | public function addTag($tag) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Remove tag. |
||
| 301 | * |
||
| 302 | * @param string $tag |
||
| 303 | * |
||
| 304 | * @return Article |
||
| 305 | */ |
||
| 306 | public function removeTag($tag) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get tags. |
||
| 315 | * |
||
| 316 | * @return [Tag] |
||
| 317 | */ |
||
| 318 | public function getTags() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Set image. |
||
| 325 | * |
||
| 326 | * @param Media $image |
||
| 327 | * |
||
| 328 | * @return Article |
||
| 329 | */ |
||
| 330 | public function setImage(Media $image = null) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get image. |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function getImage() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get businessEntity. |
||
| 349 | * |
||
| 350 | * @return Article |
||
| 351 | */ |
||
| 352 | public function getBusinessEntity() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Set template. |
||
| 359 | * |
||
| 360 | * @param ArticleTemplate $template |
||
| 361 | * |
||
| 362 | * @return Article |
||
| 363 | */ |
||
| 364 | public function setTemplate(ArticleTemplate $template) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get template. |
||
| 373 | * |
||
| 374 | * @return ArticleTemplate |
||
| 375 | */ |
||
| 376 | public function getTemplate() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Set status. |
||
| 383 | * |
||
| 384 | * @param status $status |
||
| 385 | */ |
||
| 386 | public function setStatus($status) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Get status. |
||
| 396 | * |
||
| 397 | * @return status |
||
| 398 | */ |
||
| 399 | public function getStatus() |
||
| 403 | |||
| 404 | |||
| 405 | /** |
||
| 406 | * Get categoryTitle. |
||
| 407 | * |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | public function getCategoryTitle() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Get publishedAtString. |
||
| 419 | * |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | public function getPublishedAtString() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Get author. |
||
| 435 | * |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | public function getAuthor() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Set author. |
||
| 445 | * |
||
| 446 | * @param string $author |
||
| 447 | * |
||
| 448 | * @return $this |
||
| 449 | */ |
||
| 450 | public function setAuthor($author) |
||
| 456 | |||
| 457 | public function getAuthorAvatar() |
||
| 463 | |||
| 464 | public function getAuthorFullname() |
||
| 470 | |||
| 471 | public function setLocale($locale) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @return string |
||
| 478 | */ |
||
| 479 | public function getLocale() |
||
| 483 | |||
| 484 | public function getName() |
||
| 488 | |||
| 489 | public function setName($name, $locale = null) |
||
| 494 | |||
| 495 | public function getSlug() |
||
| 499 | |||
| 500 | public function setSlug($slug, $locale = null) |
||
| 505 | |||
| 506 | public function getDescription() |
||
| 515 | } |
||
| 516 |
This check marks private properties in classes that are never used. Those properties can be removed.