QuestionController::getPostQuestionAnswer()   C
last analyzed

Complexity

Conditions 9
Paths 33

Size

Total Lines 70
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 70
ccs 0
cts 42
cp 0
rs 6.1585
c 0
b 0
f 0
cc 9
eloc 38
nc 33
nop 2
crap 90

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use \Nicklas\Comment\Modules\User;
12
13
/**
14
 * Extends the UserController, for comments
15
 */
16
class QuestionController extends AdminController
17
{
18
19
    /**
20
     * Show all items.
21
     *
22
     * @return void
23
     */
24 1
    public function getIndex()
25
    {
26 1
        $question = new Question($this->di->get("db"));
27
28
        $views = [
29 1
            ["comment/question/view-all", ["questions" => $question->getQuestions(),
30 1
             "headerTitle" => "Senaste frågor"], "main"]
31 1
        ];
32
33 1
        $this->di->get("pageRenderComment")->renderPage([
34 1
            "views" => $views,
35
            "title" => "All questions"
36 1
        ]);
37 1
    }
38
39
    /**
40
     * View specific question and create answer form
41
     *
42
     * @return void
43
     */
44
    public function getPostQuestionAnswer($id, $sort = null)
45
    {
46
        $question = new Question($this->di->get("db"));
47
        $user     = new User($this->di->get("db"));
48
49
        if ($question->find("id", $id) == null) {
50
            return false;
51
        }
52
        $question = $question->getQuestion($id);
53
54
55
        // This is a bad practice and should be in the module, (fix this after kmom10 future)
56
        // getting actual user Objects for posts for reputation show in view.
57
        $question->question->userObj = $user->getUser($question->user);
58
        $question->userObj           = $user->getUser($question->user);
59
60
        $question->answers = array_map(function ($answer) {
61
            $user                = new User($this->di->get("db"));
62
            $answer->userObj     = $user->getUser($answer->user);
63
            $answer->vote->score = $answer->vote->score == null ? 0 : $answer->vote->score;
64
            return $answer;
65
        }, $question->answers);
66
67
        // Get query for up or down
68
        $order = isset($_GET["order"]) ? $_GET["order"] : "up";
69
70
        // Highest points
71
        if ($sort == "points") {
72
            usort($question->answers, function ($current, $next) {
73
                return $current->vote->score > $next->vote->score;
74
            });
75
        }
76
77
        // Accepted top
78
        if ($sort == "accepted") {
79
            usort($question->answers, function ($current, $next) {
0 ignored issues
show
Unused Code introduced by
The parameter $next is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
                return $current->accepted == "yes" ? 1 : -1;
81
            });
82
        }
83
84
85
        // Highest votes
86
        if ($sort == "vote") {
87
            usort($question->answers, function ($current, $next) {
88
                return count($current->vote->likes) > count($next->vote->likes);
89
            });
90
        }
91
92
        // If up array_reverse, "down" is default from module
93
        if ($order == "up") {
94
            $question->answers = array_reverse($question->answers);
95
        }
96
97
        $form       = new CreateAnswerForm($this->di, $id);
98
        $form->check();
99
100
        $views = [
101
            ["comment/question/view/view-question", ["question" => $question], "question"],
102
            ["comment/question/view/view-answers",
103
            ["answers" => $question->answers, "questionId" => $question->question->questionId], "answer"],
104
            ["comment/question/view/post-answer", ["form" => $form->getHTML()], "form"],
105
            ["comment/question/view/wrappedField", ["question" => $question], "main"]
106
            ];
107
        $this->di->get("pageRenderComment")->renderPage([
108
            "views" => $views,
109
            "title" => "Fråga $question->id"
110
        ]);
111
112
        return false;
113
    }
114
115
    /**
116
     * Show all items.
117
     *
118
     * @return void
119
     */
120
    public function getTaggedQuestions($tag)
121
    {
122
        $question = new Question($this->di->get("db"));
123
124
        $questions = $question->getQuestions();
125
126
127
128
        $filteredQuestions = array_filter($questions, function ($value) use ($tag) {
129
            return in_array($tag, $value->tags);
130
        });
131
132
        $views = [
133
            ["comment/question/view-all", ["questions" => $filteredQuestions,
134
             "headerTitle" => "Frågor med taggen $tag"], "main"]
135
        ];
136
137
        $this->di->get("pageRenderComment")->renderPage([
138
            "views" => $views,
139
            "title" => "Questions | $tag"
140
        ]);
141
    }
142
143
    /**
144
     * View all comments and create question form
145
     *
146
     * @return void
147
     */
148
    public function getPostCreateQuestion()
149
    {
150
        $question = new Question($this->di->get("db"));
151
152
        $form       = new CreateQuestionForm($this->di);
153
        $form->check();
154
155
        $views = [
156
            ["comment/question/create-question", ["form" => $form->getHTML()], "main"],
157
            ["comment/question/view-all", ["questions" => $question->getQuestions(),
158
             "headerTitle" => "Senaste frågor"], "main"]
159
        ];
160
161
        $this->di->get("pageRenderComment")->renderPage([
162
            "views" => $views,
163
            "title" => "Create your question"
164
        ]);
165
    }
166
}
167