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