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 | * @ORM\Column(name="status", type="string", nullable=false) |
||
| 60 | */ |
||
| 61 | protected $status; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Categories of the article. |
||
| 65 | * |
||
| 66 | * @ORM\ManyToOne(targetEntity="Category", inversedBy="articles") |
||
| 67 | * @ORM\JoinColumn(onDelete="SET NULL") |
||
| 68 | * @VIC\BusinessProperty({"textable", "seoable"}) |
||
| 69 | */ |
||
| 70 | private $category; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var datetime |
||
| 74 | * |
||
| 75 | * @ORM\Column(name="publishedAt", type="datetime", nullable=true) |
||
| 76 | * @VIC\BusinessProperty({"dateable", "textable"}) |
||
| 77 | */ |
||
| 78 | private $publishedAt; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * This relation is dynamically added by ArticleSubscriber |
||
| 82 | * The property is needed here. |
||
| 83 | * |
||
| 84 | * @VIC\BusinessProperty({"textable", "seoable"}) |
||
| 85 | */ |
||
| 86 | private $author; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Tags of the article. |
||
| 90 | * |
||
| 91 | * @ORM\ManyToMany(targetEntity="Tag", inversedBy="articles") |
||
| 92 | * @ORM\JoinTable(name="vic_article_tags") |
||
| 93 | * @Assert\Valid() |
||
| 94 | */ |
||
| 95 | private $tags; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\BlogBundle\Entity\Blog", inversedBy="articles", cascade={"persist"}) |
||
| 99 | * @ORM\JoinColumn(name="blog_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 100 | */ |
||
| 101 | private $blog; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var BusinessTemplate |
||
| 105 | * @ORM\ManyToOne(targetEntity="ArticleTemplate") |
||
| 106 | * @ORM\JoinColumn(name="template_id", referencedColumnName="id", onDelete="SET NULL") |
||
| 107 | * @Assert\NotNull() |
||
| 108 | */ |
||
| 109 | private $template; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @VIC\BusinessProperty("textable") |
||
| 113 | */ |
||
| 114 | private $categoryTitle; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @VIC\BusinessProperty("textable") |
||
| 118 | */ |
||
| 119 | private $publishedAtString; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @VIC\BusinessProperty("textable") |
||
| 123 | */ |
||
| 124 | private $authorAvatar; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @VIC\BusinessProperty("textable") |
||
| 128 | */ |
||
| 129 | private $authorFullName; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @ORM\Column(name="deletedAt", type="datetime", nullable=true) |
||
| 133 | */ |
||
| 134 | private $deletedAt; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @Gedmo\Locale |
||
| 138 | */ |
||
| 139 | protected $locale; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * to string method. |
||
| 143 | * |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | public function __toString() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Constructor. |
||
| 153 | */ |
||
| 154 | public function __construct() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get id. |
||
| 161 | * |
||
| 162 | * @return int |
||
| 163 | */ |
||
| 164 | public function getId() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Set id. |
||
| 171 | * |
||
| 172 | * @param int $id |
||
| 173 | */ |
||
| 174 | public function setId($id) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get category. |
||
| 181 | * |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | public function getCategory() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get the published at property. |
||
| 191 | * |
||
| 192 | * @return \DateTime |
||
| 193 | */ |
||
| 194 | public function getPublishedAt() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Set publishedAt. |
||
| 205 | * |
||
| 206 | * @param \DateTime $publishedAt |
||
| 207 | * |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | public function setPublishedAt($publishedAt) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Get deletedAt. |
||
| 219 | * |
||
| 220 | * @return \DateTime |
||
| 221 | */ |
||
| 222 | public function getDeletedAt() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Set deletedAt. |
||
| 229 | * |
||
| 230 | * @param \DateTime $deletedAt |
||
| 231 | * |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | public function setDeletedAt($deletedAt) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get the blog. |
||
| 243 | * |
||
| 244 | * @return Blog |
||
| 245 | */ |
||
| 246 | public function getBlog() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Set the blog. |
||
| 253 | * |
||
| 254 | * @param Blog $blog |
||
| 255 | */ |
||
| 256 | public function setBlog(Blog $blog) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Set tags. |
||
| 263 | * |
||
| 264 | * @param string $tags |
||
| 265 | * |
||
| 266 | * @return Article |
||
| 267 | */ |
||
| 268 | public function setTags($tags) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Add tag. |
||
| 277 | * |
||
| 278 | * @param string $tag |
||
| 279 | * |
||
| 280 | * @return Article |
||
| 281 | */ |
||
| 282 | public function addTag($tag) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Remove tag. |
||
| 291 | * |
||
| 292 | * @param string $tag |
||
| 293 | * |
||
| 294 | * @return Article |
||
| 295 | */ |
||
| 296 | public function removeTag($tag) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Get tags. |
||
| 305 | * |
||
| 306 | * @return [Tag] |
||
| 307 | */ |
||
| 308 | public function getTags() |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * Get businessEntity. |
||
| 316 | * |
||
| 317 | * @return Article |
||
| 318 | */ |
||
| 319 | public function getBusinessEntity() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Set template. |
||
| 326 | * |
||
| 327 | * @param ArticleTemplate $template |
||
| 328 | * |
||
| 329 | * @return Article |
||
| 330 | */ |
||
| 331 | public function setTemplate(ArticleTemplate $template) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get template. |
||
| 340 | * |
||
| 341 | * @return ArticleTemplate |
||
| 342 | */ |
||
| 343 | public function getTemplate() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Set status. |
||
| 350 | * |
||
| 351 | * @param status $status |
||
| 352 | */ |
||
| 353 | public function setStatus($status) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Get status. |
||
| 363 | * |
||
| 364 | * @return status |
||
| 365 | */ |
||
| 366 | public function getStatus() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get categoryTitle. |
||
| 373 | * |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | public function getCategoryTitle() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get publishedAtString. |
||
| 385 | * |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | public function getPublishedAtString() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Get author. |
||
| 401 | * |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | public function getAuthor() |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Set author. |
||
| 411 | * |
||
| 412 | * @param string $author |
||
| 413 | * |
||
| 414 | * @return $this |
||
| 415 | */ |
||
| 416 | public function setAuthor($author) |
||
| 422 | |||
| 423 | public function getAuthorAvatar() |
||
| 429 | |||
| 430 | public function getAuthorFullname() |
||
| 436 | |||
| 437 | public function setLocale($locale) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | public function getLocale() |
||
| 449 | |||
| 450 | public function getName() |
||
| 454 | |||
| 455 | public function setName($name, $locale = null) |
||
| 460 | |||
| 461 | public function getSlug() |
||
| 465 | |||
| 466 | public function setSlug($slug, $locale = null) |
||
| 471 | |||
| 472 | public function getDescription() |
||
| 476 | |||
| 477 | public function setDescription($description, $locale = null) |
||
| 486 | |||
| 487 | public function setImage($image, $locale = null) |
||
| 492 | } |
||
| 493 |
This check marks private properties in classes that are never used. Those properties can be removed.