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 implements BusinessEntityInterface |
||
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 | * @deprecated |
||
61 | * |
||
62 | * @var string |
||
63 | * |
||
64 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\MediaBundle\Entity\Media") |
||
65 | * @ORM\JoinColumn(name="image_id", referencedColumnName="id", onDelete="CASCADE") |
||
66 | * @VIC\BusinessProperty("imageable") |
||
67 | */ |
||
68 | private $image; |
||
69 | |||
70 | /** |
||
71 | * @ORM\Column(name="status", type="string", nullable=false) |
||
72 | */ |
||
73 | protected $status; |
||
74 | |||
75 | /** |
||
76 | * Categories of the article. |
||
77 | * |
||
78 | * @ORM\ManyToOne(targetEntity="Category", inversedBy="articles") |
||
79 | * @ORM\JoinColumn(onDelete="SET NULL") |
||
80 | * @VIC\BusinessProperty({"textable", "seoable"}) |
||
81 | */ |
||
82 | private $category; |
||
83 | |||
84 | /** |
||
85 | * @var datetime |
||
86 | * |
||
87 | * @ORM\Column(name="publishedAt", type="datetime", nullable=true) |
||
88 | * @VIC\BusinessProperty({"dateable", "textable"}) |
||
89 | */ |
||
90 | private $publishedAt; |
||
91 | |||
92 | /** |
||
93 | * This relation is dynamically added by ArticleSubscriber |
||
94 | * The property is needed here. |
||
95 | * |
||
96 | * @VIC\BusinessProperty({"textable", "seoable"}) |
||
97 | */ |
||
98 | private $author; |
||
99 | |||
100 | /** |
||
101 | * Tags of the article. |
||
102 | * |
||
103 | * @ORM\ManyToMany(targetEntity="Tag", inversedBy="articles") |
||
104 | * @ORM\JoinTable(name="vic_article_tags") |
||
105 | * @Assert\Valid() |
||
106 | */ |
||
107 | private $tags; |
||
108 | |||
109 | /** |
||
110 | * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\BlogBundle\Entity\Blog", inversedBy="articles", cascade={"persist"}) |
||
111 | * @ORM\JoinColumn(name="blog_id", referencedColumnName="id", onDelete="CASCADE") |
||
112 | */ |
||
113 | private $blog; |
||
114 | |||
115 | /** |
||
116 | * @var BusinessTemplate |
||
117 | * @ORM\ManyToOne(targetEntity="ArticleTemplate") |
||
118 | * @ORM\JoinColumn(name="template_id", referencedColumnName="id", onDelete="SET NULL") |
||
119 | * @Assert\NotNull() |
||
120 | */ |
||
121 | private $template; |
||
122 | |||
123 | /** |
||
124 | * @VIC\BusinessProperty("textable") |
||
125 | */ |
||
126 | private $categoryTitle; |
||
127 | |||
128 | /** |
||
129 | * @VIC\BusinessProperty("textable") |
||
130 | */ |
||
131 | private $publishedAtString; |
||
132 | |||
133 | /** |
||
134 | * @VIC\BusinessProperty({"textable", "imageable"}) |
||
135 | */ |
||
136 | private $authorAvatar; |
||
137 | |||
138 | /** |
||
139 | * @VIC\BusinessProperty("textable") |
||
140 | */ |
||
141 | private $authorFullName; |
||
142 | |||
143 | /** |
||
144 | * @ORM\Column(name="deletedAt", type="datetime", nullable=true) |
||
145 | */ |
||
146 | private $deletedAt; |
||
147 | |||
148 | /** |
||
149 | * @Gedmo\Locale |
||
150 | */ |
||
151 | protected $locale; |
||
152 | |||
153 | /** |
||
154 | * to string method. |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function __toString() |
||
162 | |||
163 | /** |
||
164 | * Constructor. |
||
165 | */ |
||
166 | public function __construct() |
||
170 | |||
171 | /** |
||
172 | * Get id. |
||
173 | * |
||
174 | * @return int |
||
175 | */ |
||
176 | public function getId() |
||
180 | |||
181 | /** |
||
182 | * Set id. |
||
183 | * |
||
184 | * @param int $id |
||
185 | */ |
||
186 | public function setId($id) |
||
190 | |||
191 | /** |
||
192 | * Get category. |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | public function getCategory() |
||
200 | |||
201 | /** |
||
202 | * Set category. |
||
203 | * |
||
204 | * @param Category $category |
||
205 | * |
||
206 | * @return Article |
||
207 | */ |
||
208 | public function setCategory(Category $category) |
||
212 | |||
213 | /** |
||
214 | * Get the published at property. |
||
215 | * |
||
216 | * @return \DateTime |
||
217 | */ |
||
218 | public function getPublishedAt() |
||
226 | |||
227 | /** |
||
228 | * Set publishedAt. |
||
229 | * |
||
230 | * @param \DateTime $publishedAt |
||
231 | * |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function setPublishedAt($publishedAt) |
||
240 | |||
241 | /** |
||
242 | * Get deletedAt. |
||
243 | * |
||
244 | * @return \DateTime |
||
245 | */ |
||
246 | public function getDeletedAt() |
||
250 | |||
251 | /** |
||
252 | * Set deletedAt. |
||
253 | * |
||
254 | * @param \DateTime $deletedAt |
||
255 | * |
||
256 | * @return $this |
||
257 | */ |
||
258 | public function setDeletedAt($deletedAt) |
||
264 | |||
265 | /** |
||
266 | * Get the blog. |
||
267 | * |
||
268 | * @return Blog |
||
269 | */ |
||
270 | public function getBlog() |
||
274 | |||
275 | /** |
||
276 | * Set the blog. |
||
277 | * |
||
278 | * @param Blog $blog |
||
279 | */ |
||
280 | public function setBlog(Blog $blog) |
||
284 | |||
285 | /** |
||
286 | * Set tags. |
||
287 | * |
||
288 | * @param string $tags |
||
289 | * |
||
290 | * @return Article |
||
291 | */ |
||
292 | public function setTags($tags) |
||
298 | |||
299 | /** |
||
300 | * Add tag. |
||
301 | * |
||
302 | * @param string $tag |
||
303 | * |
||
304 | * @return Article |
||
305 | */ |
||
306 | public function addTag($tag) |
||
312 | |||
313 | /** |
||
314 | * Remove tag. |
||
315 | * |
||
316 | * @param string $tag |
||
317 | * |
||
318 | * @return Article |
||
319 | */ |
||
320 | public function removeTag($tag) |
||
326 | |||
327 | /** |
||
328 | * Get tags. |
||
329 | * |
||
330 | * @return [Tag] |
||
331 | */ |
||
332 | public function getTags() |
||
336 | |||
337 | /** |
||
338 | * Get businessEntity. |
||
339 | * |
||
340 | * @return Article |
||
341 | */ |
||
342 | public function getBusinessEntity() |
||
346 | |||
347 | /** |
||
348 | * Set template. |
||
349 | * |
||
350 | * @param ArticleTemplate $template |
||
351 | * |
||
352 | * @return Article |
||
353 | */ |
||
354 | public function setTemplate(ArticleTemplate $template) |
||
360 | |||
361 | /** |
||
362 | * Get template. |
||
363 | * |
||
364 | * @return ArticleTemplate |
||
365 | */ |
||
366 | public function getTemplate() |
||
370 | |||
371 | /** |
||
372 | * Set status. |
||
373 | * |
||
374 | * @param string $status |
||
375 | */ |
||
376 | public function setStatus($status) |
||
383 | |||
384 | /** |
||
385 | * Get status. |
||
386 | * |
||
387 | * @return string |
||
388 | */ |
||
389 | public function getStatus() |
||
393 | |||
394 | /** |
||
395 | * Get categoryTitle. |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | public function getCategoryTitle() |
||
405 | |||
406 | /** |
||
407 | * Get publishedAtString. |
||
408 | * |
||
409 | * @return string |
||
410 | */ |
||
411 | public function getPublishedAtString() |
||
421 | |||
422 | /** |
||
423 | * Get author. |
||
424 | * |
||
425 | * @return string |
||
426 | */ |
||
427 | public function getAuthor() |
||
431 | |||
432 | /** |
||
433 | * Set author. |
||
434 | * |
||
435 | * @param string $author |
||
436 | * |
||
437 | * @return $this |
||
438 | */ |
||
439 | public function setAuthor($author) |
||
445 | |||
446 | public function getAuthorAvatar() |
||
452 | |||
453 | public function getAuthorFullname() |
||
459 | |||
460 | public function setLocale($locale) |
||
464 | |||
465 | /** |
||
466 | * @return string |
||
467 | */ |
||
468 | public function getLocale() |
||
472 | |||
473 | /** |
||
474 | * @return string |
||
475 | */ |
||
476 | public function getName() |
||
480 | |||
481 | public function setName($name, $locale = null) |
||
486 | |||
487 | public function getSlug() |
||
491 | |||
492 | public function setSlug($slug, $locale = null) |
||
497 | |||
498 | public function getDescription() |
||
502 | |||
503 | public function setDescription($description, $locale = null) |
||
508 | |||
509 | public function getImage() |
||
513 | |||
514 | public function setImage($image, $locale = null) |
||
519 | } |
||
520 |
This check marks private properties in classes that are never used. Those properties can be removed.