| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Nicklas\Comment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use \Nicklas\Comment\HTMLForm\Comment\CreateQuestionForm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use \Nicklas\Comment\HTMLForm\Comment\CreateAnswerForm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use \Nicklas\Comment\HTMLForm\Comment\EditCommentForm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use \Nicklas\Comment\Modules\Question; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use \Nicklas\Comment\Modules\Comment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Extends the UserController, for comments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class QuestionController extends AdminController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * Show all items. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 23 | 1 | View Code Duplication |     public function getIndex() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 1 |  |         $question = new Question($this->di->get("db")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $views = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 1 |  |             ["comment/question/view-all", ["questions" => $question->getQuestions()], "main"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 1 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 1 |  |         $this->di->get("pageRenderComment")->renderPage([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |             "views" => $views, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             "title" => "All questions" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 1 |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * View specific question and create answer form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function getPostQuestionAnswer($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $question = new Question($this->di->get("db")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $form       = new CreateAnswerForm($this->di, $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $form->check(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $question = $question->getQuestion($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $views = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             ["comment/question/view/view-question", ["question" => $question], "question"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ["comment/question/view/view-answers", ["answers" => $question->answers], "question"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             ["comment/question/view/post-answer", ["form" => $form->getHTML()], "form"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             ["comment/question/view/wrappedField", ["question" => $question], "main"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->di->get("pageRenderComment")->renderPage([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             "views" => $views, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             "title" => "Create your question" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * View specific question and create answer form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public function postComment($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $text = isset($_POST["text"]) ? $_POST["text"] : "nothingfound"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if ($text == "nothingfound") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $user = $this->di->get("session")->get("user"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $comment = new Comment($this->di->get("db")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $comment->text = $text; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $comment->parentId = $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $comment->user = $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $comment->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * Show all items. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 91 |  | View Code Duplication |     public function getTaggedQuestions($tag) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         $comment = new Comment($this->di->get("db")); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         $views = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |             ["comment/crud/view-all", ["questions" => $comment->getPosts("tags LIKE ?", [$tag])], "main"] | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         $this->di->get("pageRenderComment")->renderPage([ | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             "views" => $views, | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             "title" => "Questions | $tag" | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * View all comments and create question form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function getPostCreateQuestion() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $question = new Question($this->di->get("db")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $form       = new CreateQuestionForm($this->di); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $form->check(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $views = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             ["comment/question/create-question", ["form" => $form->getHTML()], "main"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             ["comment/question/view-all", ["questions" => $question->getQuestions()], "main"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         // If not logged in, render other views | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         if (!$this->di->get("session")->has("user")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $views = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 ["comment/loginComment", [], "main"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $this->di->get("pageRenderComment")->renderPage([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             "views" => $views, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             "title" => "Create your question" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 134 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 135 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.