Completed
Push — master ( 8260e2...8ffaa6 )
by Nicklas
02:17
created

CommentController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 99
Duplicated Lines 23.23 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 19.15%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 23
loc 99
ccs 9
cts 47
cp 0.1915
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndex() 14 14 1
A getTaggedQuestions() 0 14 1
B getPostCreateQuestion() 0 25 2
A getPostQuestionAnswer() 9 19 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\EditCommentForm;
7
8
use \Nicklas\Comment\Modules\Question;
9
10
/**
11
 * Extends the UserController, for comments
12
 */
13
class CommentController extends AdminController
14
{
15
16
17
    /**
18
     * Show all items.
19
     *
20
     * @return void
21
     */
22 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...
23
    {
24 1
        $question = new Question($this->di);
25 1
        $question->setDb($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
     * Show all items.
38
     *
39
     * @return void
40
     */
41
    public function getTaggedQuestions($tag)
42
    {
43
        $comment = new Comment($this->di);
44
        $comment->setDb($this->di->get("db"));
45
46
        $views = [
47
            ["comment/crud/view-all", ["questions" => $comment->getPosts("tags LIKE ?", [$tag])], "main"]
48
        ];
49
50
        $this->di->get("pageRenderComment")->renderPage([
51
            "views" => $views,
52
            "title" => "Questions | $tag"
53
        ]);
54
    }
55
56
    /**
57
     * View all comments and create question form
58
     *
59
     * @return void
60
     */
61
    public function getPostCreateQuestion()
62
    {
63
        $comment = new Comment($this->di);
64
        $comment->setDb($this->di->get("db"));
65
66
        $form       = new CreateQuestionForm($this->di);
67
        $form->check();
68
69
        $views = [
70
            ["comment/crud/createQuestion", ["form" => $form->getHTML()], "main"],
71
            ["comment/crud/view-all", ["questions" => $comment->getPosts("type = ?", ["question"])], "main"]
72
        ];
73
74
        // If not logged in, render other views
75
        if (!$this->di->get("session")->has("user")) {
76
            $views = [
77
                ["comment/loginComment", [], "main"]
78
            ];
79
        }
80
81
        $this->di->get("pageRenderComment")->renderPage([
82
            "views" => $views,
83
            "title" => "Create your question"
84
        ]);
85
    }
86
87
    /**
88
     * View specific question and create answer form
89
     *
90
     * @return void
91
     */
92
    public function getPostQuestionAnswer($id)
93
    {
94
        $question = new Question($this->di);
95
        $question->setDb($this->di->get("db"));
96
97
        $question = $question->getQuestion($id);
98
99
        // If not logged in, render other views
100 View Code Duplication
        if ($question->type == "question") {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
101
            $views = [
102
                ["comment/question/view-question", ["question" => $question], "main"]
103
             ];
104
            $this->di->get("pageRenderComment")->renderPage([
105
                "views" => $views,
106
                "title" => "Create your question"
107
            ]);
108
        }
109
        return false;
110
    }
111
}
112