Test Failed
Push — master ( 7e1229...8260e2 )
by Nicklas
02:49
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 24.32%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 23
loc 99
ccs 9
cts 37
cp 0.2432
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\Comment;
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/crud/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"]
0 ignored issues
show
Bug introduced by
The method getPosts() does not seem to exist on object<Nicklas\Comment\Modules\Comment>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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"]
0 ignored issues
show
Bug introduced by
The method getPosts() does not seem to exist on object<Nicklas\Comment\Modules\Comment>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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 Comment($this->di);
95
        $question->setDb($this->di->get("db"));
96
97
        $question = $question->getPost($id);
0 ignored issues
show
Bug introduced by
The method getPost() does not seem to exist on object<Nicklas\Comment\Modules\Comment>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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/crud/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