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") |
||
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 | * values : |
||
77 | * 0 : draft |
||
78 | * 1 : user confirmed |
||
79 | * 2 : admin accepted |
||
80 | * 9 : admin rejected |
||
81 | * |
||
82 | * @ORM\Column(type="integer", nullable=false) |
||
83 | */ |
||
84 | protected $status = 0; |
||
85 | |||
86 | /** |
||
87 | * @ORM\Column(type="boolean", nullable=true) |
||
88 | */ |
||
89 | protected $pushed = 0; |
||
90 | |||
91 | /** |
||
92 | * @ORM\Column(name="created_at", type="datetime") |
||
93 | */ |
||
94 | protected $createdAt; |
||
95 | |||
96 | /** |
||
97 | * @ORM\Column(name="updated_at", type="datetime") |
||
98 | */ |
||
99 | protected $updatedAt; |
||
100 | |||
101 | public function __construct() |
||
107 | |||
108 | /** |
||
109 | * @PrePersist |
||
110 | */ |
||
111 | public function createChrono() |
||
116 | |||
117 | /** |
||
118 | * @PreUpdate |
||
119 | */ |
||
120 | public function updateChrono() |
||
124 | |||
125 | /** |
||
126 | * @return the unknown_type |
||
127 | */ |
||
128 | public function getId() |
||
132 | |||
133 | /** |
||
134 | * @param unknown_type $id |
||
135 | */ |
||
136 | public function setId($id) |
||
142 | |||
143 | /** |
||
144 | * @return the unknown_type |
||
145 | */ |
||
146 | public function getPostvote() |
||
150 | |||
151 | /** |
||
152 | * @param unknown_type $postvote |
||
153 | */ |
||
154 | public function setPostvote($postvote) |
||
161 | |||
162 | /** |
||
163 | * @return the unknown_type |
||
164 | */ |
||
165 | public function getUser() |
||
169 | |||
170 | /** |
||
171 | * @param unknown_type $user |
||
172 | */ |
||
173 | public function setUser($user) |
||
179 | |||
180 | /** |
||
181 | * @return boolean |
||
182 | */ |
||
183 | public function getEntry() |
||
187 | |||
188 | /** |
||
189 | * @param boolean $entry |
||
190 | */ |
||
191 | public function setEntry($entry) |
||
197 | |||
198 | /** |
||
199 | * Add an entry to the post. |
||
200 | * |
||
201 | * @param PostVotePostEntry $postElement |
||
202 | * |
||
203 | * @return void |
||
204 | */ |
||
205 | public function addPostElement($postElement) |
||
210 | |||
211 | /** |
||
212 | * @return ArrayCollection unknown_type |
||
213 | */ |
||
214 | public function getPostElements() |
||
218 | |||
219 | /** |
||
220 | */ |
||
221 | public function setPostElements($postElements) |
||
227 | |||
228 | /** |
||
229 | * Add an entry to the vote. |
||
230 | * |
||
231 | * @param PostVoteVote $vote |
||
232 | * |
||
233 | * @return void |
||
234 | */ |
||
235 | public function addVote($vote) |
||
239 | |||
240 | /** |
||
241 | * @return the unknown_type |
||
242 | */ |
||
243 | public function getVotes() |
||
247 | |||
248 | /** |
||
249 | * @param unknown_type $votes |
||
250 | */ |
||
251 | public function setVotes($votes) |
||
257 | |||
258 | /** |
||
259 | * Add an entry to the comment. |
||
260 | * |
||
261 | * @param PostVoteComment $comment |
||
262 | * |
||
263 | * @return void |
||
264 | */ |
||
265 | public function addComment(PostVoteComment $comment) |
||
270 | |||
271 | /** |
||
272 | * @return the unknown_type |
||
273 | */ |
||
274 | public function getComments() |
||
278 | |||
279 | /** |
||
280 | * @param unknown_type $comments |
||
281 | */ |
||
282 | public function setComments($comments) |
||
288 | |||
289 | public function addComments(ArrayCollection $comments) |
||
296 | |||
297 | public function removeComments(\Doctrine\Common\Collections\Collection $comments) |
||
304 | |||
305 | /** |
||
306 | * @return integer unknown_type |
||
307 | */ |
||
308 | public function getStatus() |
||
312 | |||
313 | /** |
||
314 | * @param unknown_status $status |
||
315 | */ |
||
316 | public function setStatus($status) |
||
322 | |||
323 | /** |
||
324 | * @return integer |
||
325 | */ |
||
326 | public function isPushed() |
||
330 | |||
331 | /** |
||
332 | * @param bool $pushed |
||
333 | * @return PostVotePost |
||
334 | */ |
||
335 | public function setPushed($pushed) |
||
340 | |||
341 | /** |
||
342 | * @return the unknown_type |
||
343 | */ |
||
344 | public function getCreatedAt() |
||
348 | |||
349 | /** |
||
350 | * @param unknown_type $createdAt |
||
351 | */ |
||
352 | public function setCreatedAt($createdAt) |
||
358 | |||
359 | /** |
||
360 | * @return the unknown_type |
||
361 | */ |
||
362 | public function getUpdatedAt() |
||
366 | |||
367 | /** |
||
368 | * @param unknown_type $updatedAt |
||
369 | */ |
||
370 | public function setUpdatedAt($updatedAt) |
||
376 | |||
377 | /** |
||
378 | * Convert the object to an array. |
||
379 | * |
||
380 | * @return array |
||
381 | */ |
||
382 | public function getArrayCopy() |
||
383 | { |
||
384 | $obj_vars = get_object_vars($this); |
||
385 | |||
386 | if (isset($obj_vars['postElements'])) { |
||
387 | $obj_vars['postElements'] = $this->getPostElements()->toArray(); |
||
388 | } |
||
389 | |||
390 | return $obj_vars; |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Convert the object to json. |
||
395 | * |
||
396 | * @return array |
||
397 | */ |
||
398 | public function jsonSerialize() |
||
399 | { |
||
400 | return $this->getArrayCopy(); |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Populate from an array. |
||
405 | * |
||
406 | * @param array $data |
||
407 | */ |
||
408 | public function populate($data = array()) |
||
411 | |||
412 | public function setInputFilter(InputFilterInterface $inputFilter) |
||
416 | |||
417 | public function getInputFilter() |
||
438 | |||
439 | public function setTranslatableLocale($locale) |
||
443 | } |
||
444 |
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..