Complex classes like PostVotePost 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 PostVotePost, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class PostVotePost implements InputFilterAwareInterface, Translatable, \JsonSerializable |
||
23 | { |
||
24 | /** |
||
25 | * @Gedmo\Locale |
||
26 | * Used locale to override Translation listener`s locale |
||
27 | * this is not a mapped field of entity metadata, just a simple property |
||
28 | */ |
||
29 | protected $locale; |
||
30 | |||
31 | protected $inputFilter; |
||
32 | |||
33 | /** |
||
34 | * @ORM\Id |
||
35 | * @ORM\Column(type="integer"); |
||
36 | * @ORM\GeneratedValue(strategy="AUTO") |
||
37 | */ |
||
38 | protected $id; |
||
39 | |||
40 | /** |
||
41 | * @ORM\ManyToOne(targetEntity="PostVote", inversedBy="posts") |
||
42 | * @ORM\JoinColumn(name="postvote_id", referencedColumnName="id", onDelete="CASCADE") |
||
43 | */ |
||
44 | protected $postvote; |
||
45 | |||
46 | /** |
||
47 | * @ORM\ManyToOne(targetEntity="PlaygroundUser\Entity\User") |
||
48 | * @ORM\JoinColumn(name="user_id", referencedColumnName="user_id", onDelete="CASCADE") |
||
49 | **/ |
||
50 | protected $user; |
||
51 | |||
52 | /** |
||
53 | * @ORM\ManyToOne(targetEntity="Entry") |
||
54 | * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="CASCADE") |
||
55 | **/ |
||
56 | protected $entry; |
||
57 | |||
58 | /** |
||
59 | * @ORM\OneToMany(targetEntity="PostVoteVote", mappedBy="post") |
||
60 | * @ORM\OrderBy({"createdAt" = "DESC"}) |
||
61 | */ |
||
62 | private $votes; |
||
63 | |||
64 | /** |
||
65 | * @ORM\OneToMany(targetEntity="PostVoteComment", mappedBy="post") |
||
66 | * @ORM\OrderBy({"createdAt" = "DESC"}) |
||
67 | */ |
||
68 | private $comments; |
||
69 | |||
70 | /** |
||
71 | * @ORM\OneToMany(targetEntity="PostVotePostElement", mappedBy="post", cascade={"persist","remove"}) |
||
72 | */ |
||
73 | private $postElements; |
||
74 | |||
75 | /** |
||
76 | * # of shares (FB, Twitter, mails...) |
||
77 | * @ORM\Column(type="integer", nullable=false) |
||
78 | */ |
||
79 | protected $shares = 0; |
||
80 | |||
81 | /** |
||
82 | * values : |
||
83 | * 0 : draft |
||
84 | * 1 : user confirmed |
||
85 | * 2 : admin accepted |
||
86 | * 9 : admin rejected |
||
87 | * |
||
88 | * @ORM\Column(type="integer", nullable=false) |
||
89 | */ |
||
90 | protected $status = 0; |
||
91 | |||
92 | /** |
||
93 | * @ORM\Column(type="boolean", nullable=true) |
||
94 | */ |
||
95 | protected $pushed = 0; |
||
96 | |||
97 | /** |
||
98 | * @ORM\Column(name="created_at", type="datetime") |
||
99 | */ |
||
100 | protected $createdAt; |
||
101 | |||
102 | /** |
||
103 | * @ORM\Column(name="updated_at", type="datetime") |
||
104 | */ |
||
105 | protected $updatedAt; |
||
106 | |||
107 | public function __construct() |
||
113 | |||
114 | /** |
||
115 | * @PrePersist |
||
116 | */ |
||
117 | public function createChrono() |
||
122 | |||
123 | /** |
||
124 | * @PreUpdate |
||
125 | */ |
||
126 | public function updateChrono() |
||
130 | |||
131 | /** |
||
132 | * @return the unknown_type |
||
133 | */ |
||
134 | public function getId() |
||
138 | |||
139 | /** |
||
140 | * @param unknown_type $id |
||
141 | */ |
||
142 | public function setId($id) |
||
148 | |||
149 | /** |
||
150 | * @return the unknown_type |
||
151 | */ |
||
152 | public function getPostvote() |
||
156 | |||
157 | /** |
||
158 | * @param unknown_type $postvote |
||
159 | */ |
||
160 | public function setPostvote($postvote) |
||
167 | |||
168 | /** |
||
169 | * @return the unknown_type |
||
170 | */ |
||
171 | public function getUser() |
||
175 | |||
176 | /** |
||
177 | * @param unknown_type $user |
||
178 | */ |
||
179 | public function setUser($user) |
||
185 | |||
186 | /** |
||
187 | * @return boolean |
||
188 | */ |
||
189 | public function getEntry() |
||
193 | |||
194 | /** |
||
195 | * @param boolean $entry |
||
196 | */ |
||
197 | public function setEntry($entry) |
||
203 | |||
204 | /** |
||
205 | * Add an entry to the post. |
||
206 | * |
||
207 | * @param PostVotePostEntry $postElement |
||
208 | * |
||
209 | * @return void |
||
210 | */ |
||
211 | public function addPostElement($postElement) |
||
216 | |||
217 | /** |
||
218 | * @return ArrayCollection unknown_type |
||
219 | */ |
||
220 | public function getPostElements() |
||
224 | |||
225 | /** |
||
226 | */ |
||
227 | public function setPostElements($postElements) |
||
233 | |||
234 | /** |
||
235 | * Add an entry to the vote. |
||
236 | * |
||
237 | * @param PostVoteVote $vote |
||
238 | * |
||
239 | * @return void |
||
240 | */ |
||
241 | public function addVote($vote) |
||
245 | |||
246 | /** |
||
247 | * @return the unknown_type |
||
248 | */ |
||
249 | public function getVotes() |
||
253 | |||
254 | /** |
||
255 | * @param unknown_type $votes |
||
256 | */ |
||
257 | public function setVotes($votes) |
||
263 | |||
264 | /** |
||
265 | * Add an entry to the comment. |
||
266 | * |
||
267 | * @param PostVoteComment $comment |
||
268 | * |
||
269 | * @return void |
||
270 | */ |
||
271 | public function addComment(PostVoteComment $comment) |
||
276 | |||
277 | /** |
||
278 | * @return the unknown_type |
||
279 | */ |
||
280 | public function getComments() |
||
284 | |||
285 | /** |
||
286 | * @param unknown_type $comments |
||
287 | */ |
||
288 | public function setComments($comments) |
||
294 | |||
295 | public function addComments(ArrayCollection $comments) |
||
302 | |||
303 | public function removeComments(\Doctrine\Common\Collections\Collection $comments) |
||
310 | |||
311 | /** |
||
312 | * @return integer unknown_type |
||
313 | */ |
||
314 | public function getShares() |
||
318 | |||
319 | /** |
||
320 | * @param unknown_shares $shares |
||
321 | */ |
||
322 | public function setShares($shares) |
||
328 | |||
329 | /** |
||
330 | * @return integer unknown_type |
||
331 | */ |
||
332 | public function getStatus() |
||
336 | |||
337 | /** |
||
338 | * @param unknown_status $status |
||
339 | */ |
||
340 | public function setStatus($status) |
||
346 | |||
347 | /** |
||
348 | * @return integer |
||
349 | */ |
||
350 | public function isPushed() |
||
354 | |||
355 | /** |
||
356 | * @param bool $pushed |
||
357 | * @return PostVotePost |
||
358 | */ |
||
359 | public function setPushed($pushed) |
||
364 | |||
365 | /** |
||
366 | * @return the unknown_type |
||
367 | */ |
||
368 | public function getCreatedAt() |
||
372 | |||
373 | /** |
||
374 | * @param unknown_type $createdAt |
||
375 | */ |
||
376 | public function setCreatedAt($createdAt) |
||
382 | |||
383 | /** |
||
384 | * @return the unknown_type |
||
385 | */ |
||
386 | public function getUpdatedAt() |
||
390 | |||
391 | /** |
||
392 | * @param unknown_type $updatedAt |
||
393 | */ |
||
394 | public function setUpdatedAt($updatedAt) |
||
400 | |||
401 | /** |
||
402 | * Convert the object to an array. |
||
403 | * |
||
404 | * @return array |
||
405 | */ |
||
406 | public function getArrayCopy() |
||
407 | { |
||
408 | $obj_vars = get_object_vars($this); |
||
409 | |||
410 | if (isset($obj_vars['postElements'])) { |
||
411 | $obj_vars['postElements'] = $this->getPostElements()->toArray(); |
||
412 | } |
||
413 | |||
414 | return $obj_vars; |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * Convert the object to json. |
||
419 | * |
||
420 | * @return array |
||
421 | */ |
||
422 | public function jsonSerialize() |
||
423 | { |
||
424 | return $this->getArrayCopy(); |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * Populate from an array. |
||
429 | * |
||
430 | * @param array $data |
||
431 | */ |
||
432 | public function populate($data = array()) |
||
435 | |||
436 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
440 | |||
441 | public function getInputFilter() |
||
462 | |||
463 | public function setTranslatableLocale($locale) |
||
467 | } |
||
468 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..