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() |
||
158 | { |
||
159 | return $this->name; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Constructor. |
||
164 | */ |
||
165 | public function __construct() |
||
166 | { |
||
167 | $this->status = PageStatus::DRAFT; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Get id. |
||
172 | * |
||
173 | * @return int |
||
174 | */ |
||
175 | public function getId() |
||
176 | { |
||
177 | return $this->id; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Set id. |
||
182 | * |
||
183 | * @param int $id |
||
184 | */ |
||
185 | public function setId($id) |
||
186 | { |
||
187 | $this->id = $id; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Get category. |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | public function getCategory() |
||
196 | { |
||
197 | return $this->category; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Get the published at property. |
||
202 | * |
||
203 | * @return \DateTime |
||
204 | */ |
||
205 | public function getPublishedAt() |
||
206 | { |
||
207 | if ($this->status == PageStatus::PUBLISHED && $this->publishedAt === null) { |
||
208 | $this->setPublishedAt($this->getCreatedAt()); |
||
209 | } |
||
210 | |||
211 | return $this->publishedAt; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Set publishedAt. |
||
216 | * |
||
217 | * @param \DateTime $publishedAt |
||
218 | * |
||
219 | * @return $this |
||
220 | */ |
||
221 | public function setPublishedAt($publishedAt) |
||
222 | { |
||
223 | $this->publishedAt = $publishedAt; |
||
224 | |||
225 | return $this; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Get deletedAt. |
||
230 | * |
||
231 | * @return \DateTime |
||
232 | */ |
||
233 | public function getDeletedAt() |
||
234 | { |
||
235 | return $this->deletedAt; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Set deletedAt. |
||
240 | * |
||
241 | * @param \DateTime $deletedAt |
||
242 | * |
||
243 | * @return $this |
||
244 | */ |
||
245 | public function setDeletedAt($deletedAt) |
||
246 | { |
||
247 | $this->deletedAt = $deletedAt; |
||
248 | |||
249 | return $this; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Get the blog. |
||
254 | * |
||
255 | * @return Blog |
||
256 | */ |
||
257 | public function getBlog() |
||
258 | { |
||
259 | return $this->blog; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Set the blog. |
||
264 | * |
||
265 | * @param Blog $blog |
||
266 | */ |
||
267 | public function setBlog(Blog $blog) |
||
268 | { |
||
269 | $this->blog = $blog; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Set tags. |
||
274 | * |
||
275 | * @param string $tags |
||
276 | * |
||
277 | * @return Article |
||
278 | */ |
||
279 | public function setTags($tags) |
||
280 | { |
||
281 | $this->tags = $tags; |
||
282 | |||
283 | return $this; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Add tag. |
||
288 | * |
||
289 | * @param string $tag |
||
290 | * |
||
291 | * @return Article |
||
292 | */ |
||
293 | public function addTag($tag) |
||
294 | { |
||
295 | $this->tags[] = $tag; |
||
296 | |||
297 | return $this; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Remove tag. |
||
302 | * |
||
303 | * @param string $tag |
||
304 | * |
||
305 | * @return Article |
||
306 | */ |
||
307 | public function removeTag($tag) |
||
308 | { |
||
309 | $this->tags->removeElement($tag); |
||
310 | |||
311 | return $this; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Get tags. |
||
316 | * |
||
317 | * @return [Tag] |
||
318 | */ |
||
319 | public function getTags() |
||
320 | { |
||
321 | return $this->tags; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Get businessEntity. |
||
326 | * |
||
327 | * @return Article |
||
328 | */ |
||
329 | public function getBusinessEntity() |
||
333 | |||
334 | /** |
||
335 | * Set template. |
||
336 | * |
||
337 | * @param ArticleTemplate $template |
||
338 | * |
||
339 | * @return Article |
||
340 | */ |
||
341 | public function setTemplate(ArticleTemplate $template) |
||
347 | |||
348 | /** |
||
349 | * Get template. |
||
350 | * |
||
351 | * @return ArticleTemplate |
||
352 | */ |
||
353 | public function getTemplate() |
||
357 | |||
358 | /** |
||
359 | * Set status. |
||
360 | * |
||
361 | * @param status $status |
||
362 | */ |
||
363 | public function setStatus($status) |
||
370 | |||
371 | /** |
||
372 | * Get status. |
||
373 | * |
||
374 | * @return status |
||
375 | */ |
||
376 | public function getStatus() |
||
380 | |||
381 | /** |
||
382 | * Get categoryTitle. |
||
383 | * |
||
384 | * @return string |
||
385 | */ |
||
386 | public function getCategoryTitle() |
||
392 | |||
393 | /** |
||
394 | * Get publishedAtString. |
||
395 | * |
||
396 | * @return string |
||
397 | */ |
||
398 | public function getPublishedAtString() |
||
408 | |||
409 | /** |
||
410 | * Get author. |
||
411 | * |
||
412 | * @return string |
||
413 | */ |
||
414 | public function getAuthor() |
||
418 | |||
419 | /** |
||
420 | * Set author. |
||
421 | * |
||
422 | * @param string $author |
||
423 | * |
||
424 | * @return $this |
||
425 | */ |
||
426 | public function setAuthor($author) |
||
432 | |||
433 | public function getAuthorAvatar() |
||
439 | |||
440 | public function getAuthorFullname() |
||
446 | |||
447 | public function setLocale($locale) |
||
451 | |||
452 | /** |
||
453 | * @return string |
||
454 | */ |
||
455 | public function getLocale() |
||
459 | |||
460 | public function getName() |
||
464 | |||
465 | public function setName($name, $locale = null) |
||
470 | |||
471 | public function getSlug() |
||
475 | |||
476 | public function setSlug($slug, $locale = null) |
||
481 | |||
482 | public function getDescription() |
||
486 | |||
487 | public function setDescription($description, $locale = null) |
||
492 | |||
493 | public function getImage() |
||
494 | { |
||
495 | return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), 'getImage'); |
||
496 | } |
||
497 | |||
498 | public function setImage($image, $locale = null) |
||
499 | { |
||
500 | $this->translate($locale, false)->setImage($image); |
||
501 | $this->mergeNewTranslations(); |
||
502 | } |
||
503 | } |
||
504 |
This check marks private properties in classes that are never used. Those properties can be removed.