Passed
Push — master ( 6494fd...7e1229 )
by Nicklas
02:09
created

CommentController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 80
Duplicated Lines 11.25 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 24.32%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 9
loc 80
ccs 9
cts 37
cp 0.2432
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndex() 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
    public function getIndex()
23
    {
24 1
        $comment = new Comment($this->di);
25 1
        $comment->setDb($this->di->get("db"));
26
27
        $views = [
28 1
            ["comment/crud/view-all", ["questions" => $comment->getPosts("type = ?", ["question"])], "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
     * Show all items.
39
     *
40
     * @return void
41
     */
42
    public function getPostCreateQuestion()
43
    {
44
        $comment = new Comment($this->di);
45
        $comment->setDb($this->di->get("db"));
46
47
        $form       = new CreateQuestionForm($this->di);
48
        $form->check();
49
50
        $views = [
51
            ["comment/crud/createQuestion", ["form" => $form->getHTML()], "main"],
52
            ["comment/crud/view-all", ["questions" => $comment->getPosts("type = ?", ["question"])], "main"]
53
        ];
54
55
        // If not logged in, render other views
56
        if (!$this->di->get("session")->has("user")) {
57
            $views = [
58
                ["comment/loginComment", [], "main"]
59
            ];
60
        }
61
62
        $this->di->get("pageRenderComment")->renderPage([
63
            "views" => $views,
64
            "title" => "Create your question"
65
        ]);
66
    }
67
68
    /**
69
     * Show all items.
70
     *
71
     * @return void
72
     */
73
    public function getPostQuestionAnswer($id)
74
    {
75
        $question = new Comment($this->di);
76
        $question->setDb($this->di->get("db"));
77
78
        $question = $question->getPost($id);
79
80
        // If not logged in, render other views
81 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...
82
            $views = [
83
                ["comment/crud/question", ["question" => $question], "main"]
84
             ];
85
            $this->di->get("pageRenderComment")->renderPage([
86
                "views" => $views,
87
                "title" => "Create your question"
88
            ]);
89
        }
90
        return false;
91
    }
92
}
93