Passed
Push — master ( 0f723a...b5e9a2 )
by Nicklas
03:09
created

QuestionController::getPostQuestionAnswer()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 60
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 0
cts 37
cp 0
rs 7.4661
c 0
b 0
f 0
cc 7
eloc 34
nc 17
nop 2
crap 56

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
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, $sort = null)
43
    {
44
        $question = new Question($this->di->get("db"));
45
        if ($question->find("id", $id) == null) {
46
            return false;
47
        }
48
        $question = $question->getQuestion($id);
49
50
51
        // This is a bad practice and should be in the module, (fix this after kmom10 future)
52
        // getting actual user Objects for posts for reputation show in view.
53
        $question->question->userObj = $user->getUser($question->user);
0 ignored issues
show
Bug introduced by
The variable $user does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
54
        $question->userObj           = $user->getUser($question->user);
55
56
        $question->answers = array_map(function ($answer) {
57
            $user                = new User($this->di->get("db"));
58
            $answer->userObj     = $user->getUser($answer->user);
59
            $answer->vote->score = $answer->vote->score == null ? 0 : $answer->vote->score;
60
            return $answer;
61
        }, $question->answers);
62
63
        // Get query for up or down
64
        $order = isset($_GET["order"]) ? $_GET["order"] : "up";
65
66
        // Highest points
67
        if ($sort == "points") {
68
            usort($question->answers, function ($current, $next) {
69
                return $current->vote->score > $next->vote->score;
70
            });
71
        }
72
73
        // Highest votes
74
        if ($sort == "vote") {
75
            usort($question->answers, function ($current, $next) {
76
                return count($current->vote->likes) > count($next->vote->likes);
77
            });
78
        }
79
80
        // If up array_reverse, "down" is default from module
81
        if ($order == "up") {
82
            $question->answers = array_reverse($question->answers);
83
        }
84
85
        $form       = new CreateAnswerForm($this->di, $id);
86
        $form->check();
87
88
        $views = [
89
            ["comment/question/view/view-question", ["question" => $question], "question"],
90
            ["comment/question/view/view-answers",
91
            ["answers" => $question->answers, "questionId" => $question->question->questionId], "question"],
92
            ["comment/question/view/post-answer", ["form" => $form->getHTML()], "form"],
93
            ["comment/question/view/wrappedField", ["question" => $question], "main"]
94
            ];
95
        $this->di->get("pageRenderComment")->renderPage([
96
            "views" => $views,
97
            "title" => "Fråga $question->id"
98
        ]);
99
100
        return false;
101
    }
102
103
    /**
104
     * Show all items.
105
     *
106
     * @return void
107
     */
108
    public function getTaggedQuestions($tag)
109
    {
110
        $question = new Question($this->di->get("db"));
111
112
        $questions = $question->getQuestions();
113
114
115
116
        $filteredQuestions = array_filter($questions, function ($value) use ($tag) {
117
            return in_array($tag, $value->tags);
118
        });
119
120
        $views = [
121
            ["comment/question/view-all", ["questions" => $filteredQuestions], "main"]
122
        ];
123
124
        $this->di->get("pageRenderComment")->renderPage([
125
            "views" => $views,
126
            "title" => "Questions | $tag"
127
        ]);
128
    }
129
130
    /**
131
     * View all comments and create question form
132
     *
133
     * @return void
134
     */
135
    public function getPostCreateQuestion()
136
    {
137
        $question = new Question($this->di->get("db"));
138
139
        $form       = new CreateQuestionForm($this->di);
140
        $form->check();
141
142
        $views = [
143
            ["comment/question/create-question", ["form" => $form->getHTML()], "main"],
144
            ["comment/question/view-all", ["questions" => $question->getQuestions()], "main"]
145
        ];
146
147
        // If not logged in, render other views
148
        if (!$this->di->get("session")->has("user")) {
149
            $views = [
150
                ["comment/loginComment", [], "main"]
151
            ];
152
        }
153
154
        $this->di->get("pageRenderComment")->renderPage([
155
            "views" => $views,
156
            "title" => "Create your question"
157
        ]);
158
    }
159
}
160