Completed
Push — master ( ec1a89...79f0f5 )
by Nicklas
02:22
created

QuestionController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 107
Duplicated Lines 12.15 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 16%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 13
loc 107
ccs 8
cts 50
cp 0.16
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndex() 13 13 1
A getPostQuestionAnswer() 0 22 1
A getTaggedQuestions() 0 21 1
B getPostCreateQuestion() 0 24 2

How to fix   Duplicated Code   

Duplicated Code

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:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
     * Show all items.
67
     *
68
     * @return void
69
     */
70
    public function getTaggedQuestions($tag)
71
    {
72
        $question = new Question($this->di->get("db"));
73
74
        $questions = $question->getQuestions();
75
76
77
78
        $filteredQuestions = array_filter($questions, function ($value) use ($tag) {
79
            return in_array($tag, $value->tags);
80
        });
81
82
        $views = [
83
            ["comment/question/view-all", ["questions" => $filteredQuestions], "main"]
84
        ];
85
86
        $this->di->get("pageRenderComment")->renderPage([
87
            "views" => $views,
88
            "title" => "Questions | $tag"
89
        ]);
90
    }
91
92
    /**
93
     * View all comments and create question form
94
     *
95
     * @return void
96
     */
97
    public function getPostCreateQuestion()
98
    {
99
        $question = new Question($this->di->get("db"));
100
101
        $form       = new CreateQuestionForm($this->di);
102
        $form->check();
103
104
        $views = [
105
            ["comment/question/create-question", ["form" => $form->getHTML()], "main"],
106
            ["comment/question/view-all", ["questions" => $question->getQuestions()], "main"]
107
        ];
108
109
        // If not logged in, render other views
110
        if (!$this->di->get("session")->has("user")) {
111
            $views = [
112
                ["comment/loginComment", [], "main"]
113
            ];
114
        }
115
116
        $this->di->get("pageRenderComment")->renderPage([
117
            "views" => $views,
118
            "title" => "Create your question"
119
        ]);
120
    }
121
}
122