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 Quiz 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 Quiz, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Quiz extends Game implements ServiceManagerAwareInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var QuizMapperInterface |
||
15 | */ |
||
16 | protected $quizMapper; |
||
17 | |||
18 | /** |
||
19 | * @var QuizAnswerMapperInterface |
||
20 | */ |
||
21 | protected $quizAnswerMapper; |
||
22 | |||
23 | /** |
||
24 | * @var QuizQuestionMapperInterface |
||
25 | */ |
||
26 | protected $quizQuestionMapper; |
||
27 | |||
28 | /** |
||
29 | * @var QuizReplyMapperInterface |
||
30 | */ |
||
31 | protected $quizReplyMapper; |
||
32 | |||
33 | /** |
||
34 | * @var quizReplyAnswerMapper |
||
35 | */ |
||
36 | protected $quizReplyAnswerMapper; |
||
37 | |||
38 | /** |
||
39 | * |
||
40 | * |
||
41 | * @param array $data |
||
42 | * @return \PlaygroundGame\Entity\Game |
||
43 | */ |
||
44 | public function createQuestion(array $data) |
||
45 | { |
||
46 | $path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR; |
||
47 | $media_url = $this->getOptions()->getMediaUrl() . '/'; |
||
48 | |||
49 | $question = new \PlaygroundGame\Entity\QuizQuestion(); |
||
50 | $form = $this->getServiceManager()->get('playgroundgame_quizquestion_form'); |
||
51 | $form->bind($question); |
||
52 | $form->setData($data); |
||
53 | |||
54 | $quiz = $this->getGameMapper()->findById($data['quiz_id']); |
||
55 | if (!$form->isValid()) { |
||
56 | return false; |
||
|
|||
57 | } |
||
58 | |||
59 | $question->setQuiz($quiz); |
||
60 | |||
61 | // If question is a prediction, no need to calculate max good answers |
||
62 | if (!$question->getPrediction()) { |
||
63 | // Max points and correct answers calculation for the question |
||
64 | if (!$question = $this->calculateMaxAnswersQuestion($question)) { |
||
65 | return false; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | // Max points and correct answers recalculation for the quiz |
||
70 | $quiz = $this->calculateMaxAnswersQuiz($question->getQuiz()); |
||
71 | |||
72 | $this->getEventManager()->trigger(__FUNCTION__, $this, array('game' => $question, 'data' => $data)); |
||
73 | $this->getQuizQuestionMapper()->insert($question); |
||
74 | $this->getEventManager()->trigger(__FUNCTION__.'.post', $this, array('game' => $question, 'data' => $data)); |
||
75 | |||
76 | View Code Duplication | if (!empty($data['upload_image']['tmp_name'])) { |
|
77 | ErrorHandler::start(); |
||
78 | $data['upload_image']['name'] = $this->fileNewname( |
||
79 | $path, |
||
80 | $question->getId() . "-" . $data['upload_image']['name'] |
||
81 | ); |
||
82 | move_uploaded_file($data['upload_image']['tmp_name'], $path . $data['upload_image']['name']); |
||
83 | $question->setImage($media_url . $data['upload_image']['name']); |
||
84 | ErrorHandler::stop(true); |
||
85 | } |
||
86 | |||
87 | $this->getQuizQuestionMapper()->update($question); |
||
88 | $this->getQuizMapper()->update($quiz); |
||
89 | |||
90 | return $question; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param array $data |
||
95 | * @return \PlaygroundGame\Entity\Game |
||
96 | */ |
||
97 | public function updateQuestion(array $data, $question) |
||
98 | { |
||
99 | $path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR; |
||
100 | $media_url = $this->getOptions()->getMediaUrl() . '/'; |
||
101 | |||
102 | $form = $this->getServiceManager()->get('playgroundgame_quizquestion_form'); |
||
103 | $form->bind($question); |
||
104 | $form->setData($data); |
||
105 | |||
106 | if (!$form->isValid()) { |
||
107 | return false; |
||
108 | } |
||
109 | |||
110 | // If question is a prediction, no need to calculate max good answers |
||
111 | if (!$question->getPrediction()) { |
||
112 | // Max points and correct answers calculation for the question |
||
113 | if (!$question = $this->calculateMaxAnswersQuestion($question)) { |
||
114 | return false; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | View Code Duplication | if (!empty($data['upload_image']['tmp_name'])) { |
|
119 | ErrorHandler::start(); |
||
120 | $data['upload_image']['name'] = $this->fileNewname( |
||
121 | $path, |
||
122 | $question->getId() . "-" . $data['upload_image']['name'] |
||
123 | ); |
||
124 | move_uploaded_file($data['upload_image']['tmp_name'], $path . $data['upload_image']['name']); |
||
125 | $question->setImage($media_url . $data['upload_image']['name']); |
||
126 | ErrorHandler::stop(true); |
||
127 | } |
||
128 | |||
129 | View Code Duplication | if (isset($data['delete_image']) && empty($data['upload_image']['tmp_name'])) { |
|
130 | ErrorHandler::start(); |
||
131 | $image = $question->getImage(); |
||
132 | $image = str_replace($media_url, '', $image); |
||
133 | if (file_exists($path .$image)) { |
||
134 | unlink($path .$image); |
||
135 | } |
||
136 | $question->setImage(null); |
||
137 | ErrorHandler::stop(true); |
||
138 | } |
||
139 | |||
140 | $i = 0; |
||
141 | foreach ($question->getAnswers() as $answer) { |
||
142 | if (!empty($data['answers'][$i]['upload_image']['tmp_name'])) { |
||
143 | ErrorHandler::start(); |
||
144 | $data['answers'][$i]['upload_image']['name'] = $this->fileNewname( |
||
145 | $path, |
||
146 | $question->getId() . "-" . $data['answers'][$i]['upload_image']['name'] |
||
147 | ); |
||
148 | move_uploaded_file( |
||
149 | $data['answers'][$i]['upload_image']['tmp_name'], |
||
150 | $path . $data['answers'][$i]['upload_image']['name'] |
||
151 | ); |
||
152 | $answer->setImage($media_url . $data['answers'][$i]['upload_image']['name']); |
||
153 | ErrorHandler::stop(true); |
||
154 | } |
||
155 | $i++; |
||
156 | } |
||
157 | |||
158 | // Max points and correct answers recalculation for the quiz |
||
159 | $quiz = $this->calculateMaxAnswersQuiz($question->getQuiz()); |
||
160 | |||
161 | // If the question was a pronostic, I update entries with the results ! |
||
162 | if ($question->getPrediction()) { |
||
163 | $this->updatePrediction($question); |
||
164 | } |
||
165 | |||
166 | $this->getEventManager()->trigger( |
||
167 | __FUNCTION__, |
||
168 | $this, |
||
169 | array('question' => $question, 'data' => $data) |
||
170 | ); |
||
171 | $this->getQuizQuestionMapper()->update($question); |
||
172 | $this->getEventManager()->trigger( |
||
173 | __FUNCTION__.'.post', |
||
174 | $this, |
||
175 | array('question' => $question, 'data' => $data) |
||
176 | ); |
||
177 | |||
178 | $this->getQuizMapper()->update($quiz); |
||
179 | |||
180 | return $question; |
||
181 | } |
||
182 | |||
183 | public function findRepliesByGame($game) |
||
198 | |||
199 | public function updatePrediction($question) |
||
263 | /** |
||
264 | * This function update the sort order of the questions in a Quiz |
||
265 | * BEWARE : This function is time consuming (1s for 11 updates) |
||
266 | * If you have many replies, switch to a batch |
||
267 | * |
||
268 | * To improve performance, usage of DQL update |
||
269 | * http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html |
||
270 | * |
||
271 | * @param string $data |
||
272 | * @return boolean |
||
273 | */ |
||
274 | public function updatePredictionOld($question) |
||
354 | |||
355 | /** |
||
356 | * This function update the sort order of the questions in a Quiz |
||
357 | * |
||
358 | * @param string $data |
||
359 | * @return boolean |
||
360 | */ |
||
361 | public function sortQuestion($data) |
||
373 | |||
374 | /** |
||
375 | * @return string |
||
376 | */ |
||
377 | public function calculateMaxAnswersQuestion($question) |
||
378 | { |
||
379 | $question_max_points = 0; |
||
380 | $question_max_correct_answers = 0; |
||
381 | // Closed question : Only one answer allowed |
||
382 | if ($question->getType() == 0) { |
||
383 | foreach ($question->getAnswers() as $answer) { |
||
384 | if ($answer->getPoints() > $question_max_points) { |
||
385 | $question_max_points = $answer->getPoints(); |
||
386 | } |
||
387 | if ($answer->getCorrect() && $question_max_correct_answers==0) { |
||
388 | $question_max_correct_answers=1; |
||
389 | } |
||
390 | } |
||
391 | if ($question_max_correct_answers == 0) { |
||
392 | return false; |
||
393 | } |
||
394 | // Closed question : Many answers allowed |
||
395 | } elseif ($question->getType() == 1) { |
||
396 | foreach ($question->getAnswers() as $answer) { |
||
397 | $question_max_points += $answer->getPoints(); |
||
398 | |||
399 | if ($answer->getCorrect()) { |
||
400 | ++$question_max_correct_answers; |
||
401 | } |
||
402 | } |
||
403 | if ($question_max_correct_answers == 0) { |
||
404 | return false; |
||
405 | } |
||
406 | // Not a question : A textarea to fill in |
||
407 | } elseif ($question->getType() == 2) { |
||
408 | $question_max_correct_answers = 0; |
||
409 | } |
||
410 | |||
411 | $question->setMaxPoints($question_max_points); |
||
412 | $question->setMaxCorrectAnswers($question_max_correct_answers); |
||
413 | |||
414 | return $question; |
||
415 | } |
||
416 | |||
417 | public function calculateMaxAnswersQuiz($quiz) |
||
430 | |||
431 | View Code Duplication | public function getNumberCorrectAnswersQuiz($user, $count = 'count') |
|
446 | |||
447 | public function createQuizReply($data, $game, $user) |
||
448 | { |
||
449 | // Si mon nb de participation est < au nb autorisé, j'ajoute une entry + reponses au quiz et points |
||
450 | $quizReplyMapper = $this->getQuizReplyMapper(); |
||
451 | $entryMapper = $this->getEntryMapper(); |
||
452 | $entry = $this->findLastActiveEntry($game, $user); |
||
453 | |||
454 | if (!$entry) { |
||
455 | return false; |
||
456 | } |
||
457 | |||
458 | $quizPoints = 0; |
||
459 | $quizCorrectAnswers = 0; |
||
460 | $maxCorrectAnswers = $game->getMaxCorrectAnswers(); |
||
461 | $totalQuestions = 0; |
||
462 | |||
463 | $quizReply = $this->getQuizReplyMapper()->getLastGameReply($entry); |
||
464 | if (!$quizReply) { |
||
465 | $quizReply = new QuizReply(); |
||
466 | } else { |
||
467 | $quizReplyAnswered = []; |
||
468 | foreach ($quizReply->getAnswers() as $answer) { |
||
469 | $quizReplyAnswered[$answer->getQuestionId()] = $answer; |
||
470 | //$this->getQuizReplyAnswerMapper()->remove($answer); |
||
471 | } |
||
472 | } |
||
473 | |||
474 | foreach ($data as $group) { |
||
475 | foreach ($group as $q => $a) { |
||
476 | if (strlen($q) > 5 && strpos($q, '-data', strlen($q) - 5) !== false) { |
||
477 | continue; // answer data is processed below |
||
478 | } |
||
479 | $question = $this->getQuizQuestionMapper()->findById((int) str_replace('q', '', $q)); |
||
480 | ++$totalQuestions; |
||
481 | if (is_array($a)) { |
||
482 | foreach ($a as $k => $answer_id) { |
||
483 | $answer = $this->getQuizAnswerMapper()->findById($answer_id); |
||
484 | View Code Duplication | if ($answer) { |
|
485 | if(isset($quizReplyAnswered[$question->getId()])){ |
||
486 | $this->getQuizReplyAnswerMapper()->remove($quizReplyAnswered[$question->getId()]); |
||
487 | } |
||
488 | |||
489 | $quizReplyAnswer = new QuizReplyAnswer(); |
||
490 | $quizReplyAnswer->setAnswer($answer->getAnswer()); |
||
491 | $quizReplyAnswer->setAnswerId($answer_id); |
||
492 | $quizReplyAnswer->setQuestion($question->getQuestion()); |
||
493 | $quizReplyAnswer->setQuestionId($question->getId()); |
||
494 | $quizReplyAnswer->setPoints($answer->getPoints()); |
||
495 | $quizReplyAnswer->setCorrect($answer->getCorrect()); |
||
496 | |||
497 | $quizReply->addAnswer($quizReplyAnswer); |
||
498 | |||
499 | $quizPoints += $answer->getPoints(); |
||
500 | $quizCorrectAnswers += $answer->getCorrect(); |
||
501 | |||
502 | if (isset($group[$q.'-'.$answer_id.'-data'])) { |
||
503 | $quizReplyAnswer->setAnswerData($group[$q.'-'.$answer_id.'-data']); |
||
504 | } |
||
505 | } |
||
506 | } |
||
507 | } elseif ($question->getType() == 0 || $question->getType() == 1) { |
||
508 | ++$totalQuestions; |
||
509 | $answer = $this->getQuizAnswerMapper()->findById($a); |
||
510 | View Code Duplication | if ($answer) { |
|
511 | if(isset($quizReplyAnswered[$question->getId()])){ |
||
512 | $this->getQuizReplyAnswerMapper()->remove($quizReplyAnswered[$question->getId()]); |
||
513 | } |
||
514 | $quizReplyAnswer = new QuizReplyAnswer(); |
||
515 | $quizReplyAnswer->setAnswer($answer->getAnswer()); |
||
516 | $quizReplyAnswer->setAnswerId($a); |
||
517 | $quizReplyAnswer->setQuestion($question->getQuestion()); |
||
518 | $quizReplyAnswer->setQuestionId($question->getId()); |
||
519 | $quizReplyAnswer->setPoints($answer->getPoints()); |
||
520 | $quizReplyAnswer->setCorrect($answer->getCorrect()); |
||
521 | |||
522 | $quizReply->addAnswer($quizReplyAnswer); |
||
523 | |||
524 | $quizPoints += $answer->getPoints(); |
||
525 | $quizCorrectAnswers += $answer->getCorrect(); |
||
526 | if (isset($group[$q.'-'.$a.'-data'])) { |
||
527 | $quizReplyAnswer->setAnswerData($group[$q.'-'.$a.'-data']); |
||
528 | } |
||
529 | } |
||
530 | } elseif ($question->getType() == 2) { |
||
531 | ++$totalQuestions; |
||
532 | if(isset($quizReplyAnswered[$question->getId()])){ |
||
533 | $this->getQuizReplyAnswerMapper()->remove($quizReplyAnswered[$question->getId()]); |
||
534 | } |
||
535 | $quizReplyAnswer = new QuizReplyAnswer(); |
||
536 | $quizReplyAnswer->setAnswer($a); |
||
537 | $quizReplyAnswer->setAnswerId(0); |
||
538 | $quizReplyAnswer->setQuestion($question->getQuestion()); |
||
539 | $quizReplyAnswer->setQuestionId($question->getId()); |
||
540 | $quizReplyAnswer->setPoints(0); |
||
541 | $quizReplyAnswer->setCorrect(0); |
||
542 | |||
543 | $quizReply->addAnswer($quizReplyAnswer); |
||
544 | |||
545 | $quizPoints += 0; |
||
546 | $quizCorrectAnswers += 0; |
||
547 | $qAnswers = $question->getAnswers(); |
||
548 | foreach ($qAnswers as $qAnswer) { |
||
549 | if (trim(strip_tags($a)) == trim(strip_tags($qAnswer->getAnswer()))) { |
||
550 | $quizReplyAnswer->setPoints($qAnswer->getPoints()); |
||
551 | $quizPoints += $qAnswer->getPoints(); |
||
552 | $quizReplyAnswer->setCorrect($qAnswer->getCorrect()); |
||
553 | $quizCorrectAnswers += $qAnswer->getCorrect(); |
||
554 | break; |
||
555 | } |
||
556 | } |
||
557 | |||
558 | if (isset($group[$q.'-'.$a.'-data'])) { |
||
559 | $quizReplyAnswer->setAnswerData($group[$q.'-'.$a.'-data']); |
||
560 | } |
||
561 | } |
||
562 | } |
||
563 | } |
||
564 | |||
565 | $winner = $this->isWinner($game, $quizCorrectAnswers); |
||
566 | |||
567 | $entry->setWinner($winner); |
||
568 | // Every winning participation is eligible to draw |
||
569 | // Make this modifiable in the admin (choose who can participate to draw) |
||
570 | $entry->setDrawable($winner); |
||
571 | $entry->setPoints($quizPoints); |
||
572 | $entry->setActive(false); |
||
573 | $entry = $entryMapper->update($entry); |
||
574 | |||
575 | $quizReply->setEntry($entry); |
||
576 | $quizReply->setTotalCorrectAnswers($quizCorrectAnswers); |
||
577 | $quizReply->setMaxCorrectAnswers($maxCorrectAnswers); |
||
578 | $quizReply->setTotalQuestions($totalQuestions); |
||
579 | |||
580 | $quizReplyMapper->insert($quizReply); |
||
581 | |||
582 | $this->getEventManager()->trigger( |
||
583 | __FUNCTION__.'.post', |
||
584 | $this, |
||
585 | array('user' => $user, 'entry' => $entry, 'reply' => $quizReply, 'game' => $game) |
||
586 | ); |
||
587 | |||
588 | return $entry; |
||
589 | } |
||
590 | |||
591 | public function isWinner($game, $quizCorrectAnswers = 0) |
||
592 | { |
||
593 | // Pour déterminer le gagnant, je regarde le nombre max de reponses correctes possibles |
||
594 | // dans le jeu, puis je calcule le ratio de bonnes réponses et le compare aux conditions |
||
595 | // de victoire |
||
596 | $winner = false; |
||
597 | $maxCorrectAnswers = $game->getMaxCorrectAnswers(); |
||
598 | if ($maxCorrectAnswers > 0) { |
||
599 | $ratioCorrectAnswers = ($quizCorrectAnswers / $maxCorrectAnswers) * 100; |
||
600 | } elseif ($game->getVictoryConditions() > 0) { |
||
601 | // In the case I have a pronostic game for example |
||
602 | $ratioCorrectAnswers = 0; |
||
603 | } else { |
||
604 | // In the case I want everybody to win |
||
605 | $ratioCorrectAnswers = 100; |
||
606 | } |
||
607 | |||
608 | if ($game->getVictoryConditions() >= 0) { |
||
609 | if ($ratioCorrectAnswers >= $game->getVictoryConditions()) { |
||
610 | $winner = true; |
||
611 | } |
||
612 | } |
||
613 | return $winner; |
||
614 | } |
||
615 | |||
616 | public function getEntriesHeader($game) |
||
617 | { |
||
618 | $header = parent::getEntriesHeader($game); |
||
619 | $header['totalCorrectAnswers'] = 1; |
||
620 | |||
621 | return $header; |
||
622 | } |
||
623 | |||
624 | View Code Duplication | public function getEntriesQuery($game) |
|
625 | { |
||
626 | $em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default'); |
||
627 | |||
628 | $qb = $em->createQueryBuilder(); |
||
629 | $qb->select(' |
||
630 | r.id, |
||
631 | u.username, |
||
632 | u.title, |
||
633 | u.firstname, |
||
634 | u.lastname, |
||
635 | u.email, |
||
636 | u.optin, |
||
637 | u.optinPartner, |
||
638 | u.address, |
||
639 | u.address2, |
||
640 | u.postalCode, |
||
641 | u.city, |
||
642 | u.telephone, |
||
643 | u.mobile, |
||
644 | u.created_at, |
||
645 | u.dob, |
||
646 | e.winner, |
||
647 | e.socialShares, |
||
648 | e.playerData, |
||
649 | e.updated_at, |
||
650 | r.totalCorrectAnswers |
||
651 | ') |
||
652 | ->from('PlaygroundGame\Entity\QuizReply', 'r') |
||
653 | ->innerJoin('r.entry', 'e') |
||
654 | ->leftJoin('e.user', 'u') |
||
655 | ->where($qb->expr()->eq('e.game', ':game')); |
||
656 | |||
657 | $qb->setParameter('game', $game); |
||
658 | |||
659 | return $qb->getQuery(); |
||
660 | } |
||
661 | |||
662 | public function getGameEntity() |
||
666 | |||
667 | /** |
||
668 | * getQuizMapper |
||
669 | * |
||
670 | * @return QuizMapperInterface |
||
671 | */ |
||
672 | public function getQuizMapper() |
||
680 | |||
681 | /** |
||
682 | * setQuizMapper |
||
683 | * |
||
684 | * @param QuizMapperInterface $quizMapper |
||
685 | * @return Game |
||
686 | */ |
||
687 | public function setQuizMapper(GameMapperInterface $quizMapper) |
||
693 | |||
694 | /** |
||
695 | * getQuizQuestionMapper |
||
696 | * |
||
697 | * @return QuizQuestionMapperInterface |
||
698 | */ |
||
699 | public function getQuizQuestionMapper() |
||
707 | |||
708 | /** |
||
709 | * setQuizQuestionMapper |
||
710 | * |
||
711 | * @param QuizQuestionMapperInterface $quizquestionMapper |
||
712 | * @return Quiz |
||
713 | */ |
||
714 | public function setQuizQuestionMapper($quizquestionMapper) |
||
720 | |||
721 | /** |
||
722 | * setQuizAnswerMapper |
||
723 | * |
||
724 | * @param QuizAnswerMapperInterface $quizAnswerMapper |
||
725 | * @return Quiz |
||
726 | */ |
||
727 | public function setQuizAnswerMapper($quizAnswerMapper) |
||
733 | |||
734 | /** |
||
735 | * getQuizAnswerMapper |
||
736 | * |
||
737 | * @return QuizAnswerMapperInterface |
||
738 | */ |
||
739 | public function getQuizAnswerMapper() |
||
747 | |||
748 | /** |
||
749 | * getQuizReplyMapper |
||
750 | * |
||
751 | * @return QuizReplyMapperInterface |
||
752 | */ |
||
753 | public function getQuizReplyMapper() |
||
761 | |||
762 | /** |
||
763 | * setQuizReplyMapper |
||
764 | * |
||
765 | * @param QuizReplyMapperInterface $quizreplyMapper |
||
766 | * @return Quiz |
||
767 | */ |
||
768 | public function setQuizReplyMapper($quizreplyMapper) |
||
774 | |||
775 | /** |
||
776 | * getQuizReplyAnswerMapper |
||
777 | * |
||
778 | * @return QuizReplyAnswerMapper |
||
779 | */ |
||
780 | public function getQuizReplyAnswerMapper() |
||
788 | |||
789 | /** |
||
790 | * setQuizReplyAnswerMapper |
||
791 | * |
||
792 | * @param QuizReplyAnswerMapper $quizReplyAnswerMapper |
||
793 | * @return Quiz |
||
794 | */ |
||
795 | public function setQuizReplyAnswerMapper($quizReplyAnswerMapper) |
||
801 | } |
||
802 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.