Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PostVote 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 PostVote, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class PostVote extends Game implements ServiceManagerAwareInterface |
||
9 | { |
||
10 | protected $postvoteMapper; |
||
11 | protected $postvoteformMapper; |
||
12 | protected $postVotePostMapper; |
||
13 | protected $postVoteVoteMapper; |
||
14 | protected $postVoteCommentMapper; |
||
15 | protected $postVotePostElementMapper; |
||
16 | |||
17 | public function getGameEntity() |
||
21 | |||
22 | public function getPath($post) |
||
36 | |||
37 | public function getMediaUrl($post) |
||
44 | |||
45 | /** |
||
46 | * @param boolean $entry |
||
47 | */ |
||
48 | public function checkPost($entry) |
||
62 | |||
63 | public function uploadFileToPost($data, $game, $user) |
||
64 | { |
||
65 | $result = false; |
||
66 | $entry = $this->findLastActiveEntry($game, $user); |
||
67 | |||
68 | if (!$entry) { |
||
69 | return '0'; |
||
70 | } |
||
71 | |||
72 | $post = $this->checkPost($entry); |
||
73 | $path = $this->getPath($post); |
||
74 | $media_url = $this->getMediaUrl($post); |
||
75 | |||
76 | $key = key($data); |
||
77 | $uploadFile = $this->uploadFile($path, $data[$key]); |
||
78 | |||
79 | if ($uploadFile) { |
||
80 | $postElement = $this->getPostVotePostElementMapper()->findOneBy(array('post' => $post, 'name' => $key)); |
||
81 | if (! $postElement) { |
||
82 | $postElement = new \PlaygroundGame\Entity\PostVotePostElement(); |
||
83 | } |
||
84 | $postElement->setName($key); |
||
85 | $postElement->setPosition(0); |
||
86 | $postElement->setValue($media_url.$uploadFile); |
||
87 | $postElement->setPost($post); |
||
88 | $postElement = $this->getPostVotePostElementMapper()->insert($postElement); |
||
89 | |||
90 | $result = $media_url.$uploadFile; |
||
91 | } |
||
92 | |||
93 | return $result; |
||
94 | } |
||
95 | |||
96 | public function deleteFilePosted($data, $game, $user) |
||
121 | |||
122 | /** |
||
123 | * |
||
124 | * @param array $data |
||
125 | * @return \PlaygroundGame\Entity\Game |
||
126 | */ |
||
127 | public function createPost(array $data, $game, $user, $form) |
||
207 | |||
208 | /** |
||
209 | * |
||
210 | * @return \PlaygroundGame\Entity\Game |
||
211 | */ |
||
212 | public function confirmPost($game, $user) |
||
213 | { |
||
214 | $postvotePostMapper = $this->getPostVotePostMapper(); |
||
215 | |||
216 | $entryMapper = $this->getEntryMapper(); |
||
217 | $entry = $this->findLastActiveEntry($game, $user); |
||
218 | |||
219 | if (!$entry) { |
||
220 | return false; |
||
221 | } |
||
222 | |||
223 | $post = $postvotePostMapper->findOneBy(array('entry' => $entry)); |
||
224 | |||
225 | if (! $post) { |
||
226 | return false; |
||
227 | } |
||
228 | |||
229 | // The post is confirmed by user. I update the status and close the associated entry |
||
230 | // Post are validated by default, unless pre-moderation is enable for the game |
||
231 | if ($game->getModerationType()) { |
||
232 | $post->setStatus(1); |
||
233 | } else { |
||
234 | $post->setStatus(2); |
||
235 | } |
||
236 | |||
237 | $postvotePostMapper->update($post); |
||
238 | |||
239 | $entry->setActive(0); |
||
240 | $entryMapper->update($entry); |
||
241 | |||
242 | $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
||
243 | 'user' => $user, |
||
244 | 'game' => $game, |
||
245 | 'entry' => $entry, |
||
246 | 'post' => $post |
||
247 | )); |
||
248 | |||
249 | // sending a mail after Post creation should be optional |
||
250 | // if ($user) { |
||
251 | // // send mail for participation |
||
252 | // $this->sendGameMail($game, $user, $post, 'postvote'); |
||
253 | // } |
||
254 | |||
255 | return $post; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * |
||
260 | * This service is ready for all types of games |
||
261 | * |
||
262 | * @param array $data |
||
263 | * @return \PlaygroundGame\Entity\Game |
||
264 | */ |
||
265 | View Code Duplication | public function createForm(array $data, $game, $form = null) |
|
266 | { |
||
267 | $title =''; |
||
268 | $description = ''; |
||
269 | |||
270 | if ($data['form_jsonified']) { |
||
271 | $jsonPV = json_decode($data['form_jsonified']); |
||
272 | foreach ($jsonPV as $element) { |
||
273 | if ($element->form_properties) { |
||
274 | $attributes = $element->form_properties[0]; |
||
275 | $title = $attributes->title; |
||
276 | $description = $attributes->description; |
||
277 | |||
278 | break; |
||
279 | } |
||
280 | } |
||
281 | } |
||
282 | if (!$form) { |
||
283 | $form = new \PlaygroundGame\Entity\PostVoteForm(); |
||
284 | } |
||
285 | $form->setPostvote($game); |
||
286 | $form->setTitle($title); |
||
287 | $form->setDescription($description); |
||
288 | $form->setForm($data['form_jsonified']); |
||
289 | $form->setFormTemplate($data['form_template']); |
||
290 | |||
291 | $form = $this->getPostVoteFormMapper()->insert($form); |
||
292 | |||
293 | return $form; |
||
294 | } |
||
295 | |||
296 | public function findArrayOfValidatedPosts($game, $user, $filter, $search = '') |
||
297 | { |
||
298 | $em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default'); |
||
299 | $qb = $em->createQueryBuilder(); |
||
300 | $and = $qb->expr()->andx(); |
||
301 | |||
302 | $and->add($qb->expr()->eq('p.status', 2)); |
||
303 | |||
304 | $and->add($qb->expr()->eq('g.id', ':game')); |
||
305 | $qb->setParameter('game', $game); |
||
306 | |||
307 | if ($search != '') { |
||
308 | $and->add( |
||
309 | $qb->expr()->orX( |
||
310 | $qb->expr()->like('u.username', $qb->expr()->literal('%:search%')), |
||
311 | $qb->expr()->like('u.firstname', $qb->expr()->literal('%:search%')), |
||
312 | $qb->expr()->like('u.lastname', $qb->expr()->literal('%:search%')), |
||
313 | $qb->expr()->like('e.value', $qb->expr()->literal('%:search%')), |
||
314 | $qb->expr()->isNull('g.publicationDate') |
||
315 | ) |
||
316 | ); |
||
317 | $qb->setParameter('search', $search); |
||
318 | } |
||
319 | |||
320 | if ('push' == $filter) { |
||
321 | $and->add( |
||
322 | $qb->expr()->andX( |
||
323 | $qb->expr()->eq('p.pushed', 1) |
||
324 | ) |
||
325 | ); |
||
326 | } |
||
327 | |||
328 | $qb->select('p, COUNT(DISTINCT v) AS votesCount, COUNT(distinct av) AS voted') |
||
329 | ->from('PlaygroundGame\Entity\PostVotePost', 'p') |
||
330 | ->innerJoin('p.postvote', 'g') |
||
331 | ->leftJoin('p.user', 'u') |
||
332 | ->innerJoin('p.postElements', 'e') |
||
333 | ->leftJoin('p.votes', 'v') |
||
334 | ->leftJoin('p.votes', 'av', 'WITH', 'av.user = :user') |
||
335 | ->where($and) |
||
336 | ->groupBy('p.id'); |
||
337 | |||
338 | if($user){ |
||
339 | $qb->setParameter('user', $user); |
||
340 | } else { |
||
341 | $qb->setParameter('user', null); |
||
342 | } |
||
343 | |||
344 | switch ($filter) { |
||
345 | case 'random': |
||
346 | $qb->orderBy('e.value', 'ASC'); |
||
347 | break; |
||
348 | case 'vote': |
||
349 | $qb->orderBy('votesCount', 'DESC'); |
||
350 | break; |
||
351 | case 'date': |
||
352 | $qb->orderBy('p.createdAt', 'DESC'); |
||
353 | break; |
||
354 | case 'push': |
||
355 | $qb->orderBy('p.createdAt', 'DESC'); |
||
356 | break; |
||
357 | case 'id': |
||
358 | $qb->orderBy('p.createdAt', 'ASC'); |
||
359 | break; |
||
360 | default: |
||
361 | $qb->orderBy('p.createdAt', 'ASC'); |
||
362 | break; |
||
363 | } |
||
364 | |||
365 | $query = $qb->getQuery(); |
||
366 | |||
367 | $posts = $query->getResult(); |
||
368 | $arrayPosts = array(); |
||
369 | $i=0; |
||
370 | foreach ($posts as $postRaw) { |
||
371 | $data = array(); |
||
372 | $post = $postRaw[0]; |
||
373 | if ($post) { |
||
374 | foreach ($post->getPostElements() as $element) { |
||
375 | $data[$element->getPosition()] = $element->getValue(); |
||
376 | } |
||
377 | $arrayPosts[$i]['post'] = $post; |
||
378 | $arrayPosts[$i]['data'] = $data; |
||
379 | $arrayPosts[$i]['votes'] = count($post->getVotes()); |
||
380 | $arrayPosts[$i]['voted'] = $postRaw['voted']; |
||
381 | $arrayPosts[$i]['id'] = $post->getId(); |
||
382 | $arrayPosts[$i]['user'] = $post->getUser(); |
||
383 | $arrayPosts[$i]['createdAt'] = $post->getCreatedAt(); |
||
384 | $i++; |
||
385 | } |
||
386 | } |
||
387 | |||
388 | return $arrayPosts; |
||
389 | } |
||
390 | |||
391 | public function addVote($user, $ipAddress, $post) |
||
392 | { |
||
393 | $postvoteVoteMapper = $this->getPostVoteVoteMapper(); |
||
394 | $postId = $post->getId(); |
||
395 | |||
396 | if ($user) { |
||
397 | $entryUser = count($postvoteVoteMapper->findBy(array('user' => $user, 'post' =>$postId))); |
||
398 | } else { |
||
399 | $entryUser =count($postvoteVoteMapper->findBy(array('ip' => $ipAddress, 'post' =>$postId))); |
||
400 | } |
||
401 | if ($entryUser && $entryUser > 0) { |
||
402 | |||
403 | return false; |
||
404 | } else { |
||
405 | $vote = new \PlaygroundGame\Entity\PostVoteVote(); |
||
406 | $vote->setPost($post); |
||
407 | $vote->setIp($ipAddress); |
||
408 | $vote->setNote(1); |
||
409 | $vote->setPostvote($post->getPostvote()); |
||
410 | if ($user) { |
||
411 | $vote->setUser($user); |
||
412 | } |
||
413 | |||
414 | $postvoteVoteMapper->insert($vote); |
||
415 | $game = $post->getPostvote(); |
||
416 | $this->getEventManager()->trigger( |
||
417 | __FUNCTION__ .'.post', |
||
418 | $this, |
||
419 | array('user' => $user, 'game' => $game, 'post' => $post, 'vote' => $vote) |
||
420 | ); |
||
421 | |||
422 | return true; |
||
423 | } |
||
424 | } |
||
425 | |||
426 | public function addComment($user, $ipAddress, $post, $message = '') |
||
451 | |||
452 | |||
453 | public function addShare($post) |
||
465 | /** |
||
466 | * Get all comments for this game |
||
467 | */ |
||
468 | public function getCommentsForPostvote($postvote) |
||
475 | |||
476 | public function removeComment($user, $ipAddress, $messageId) |
||
493 | |||
494 | public function getEntriesHeader($game) |
||
510 | |||
511 | View Code Duplication | public function getEntriesQuery($game) |
|
549 | |||
550 | /** |
||
551 | * getGameEntries : All entries of a game |
||
552 | * |
||
553 | * @return Array of PlaygroundGame\Entity\Game |
||
554 | */ |
||
555 | public function getGameEntries($header, $entries, $game) |
||
583 | |||
584 | public function getPostVoteFormMapper() |
||
585 | { |
||
586 | if (null === $this->postvoteformMapper) { |
||
587 | $this->postvoteformMapper = $this->getServiceManager()->get('playgroundgame_postvoteform_mapper'); |
||
588 | } |
||
589 | |||
590 | return $this->postvoteformMapper; |
||
591 | } |
||
592 | |||
593 | public function setPostVoteFormMapper($postvoteformMapper) |
||
599 | |||
600 | public function getPostVotePostElementMapper() |
||
610 | |||
611 | public function setPostVotePostElementMapper($postVotePostElementMapper) |
||
617 | |||
618 | public function getPostVoteVoteMapper() |
||
626 | |||
627 | public function setPostVoteVoteMapper($postVoteVoteMapper) |
||
633 | |||
634 | public function getPostVoteCommentMapper() |
||
642 | |||
643 | public function setPostVoteCommentMapper($postVoteCommentMapper) |
||
649 | |||
650 | public function getPostVotePostMapper() |
||
658 | |||
659 | public function setPostVotePostMapper($postVotePostMapper) |
||
665 | |||
666 | public function getPostVoteMapper() |
||
674 | |||
675 | /** |
||
676 | * setQuizQuestionMapper |
||
677 | * |
||
678 | * @return PostVote |
||
679 | */ |
||
680 | public function setPostVoteMapper($postvoteMapper) |
||
686 | } |
||
687 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.