CommentController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 15.22 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 3
dl 14
loc 92
ccs 0
cts 47
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A newComment() 0 17 2
A removeComment() 7 17 3
A editComment() 7 22 3
A renderComments() 0 15 1

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 CJ\Comment;
4
5
use \Anax\DI\InjectionAwareInterface;
6
use \Anax\DI\InjectionAwareTrait;
7
use \CJ\Comment\HTMLForm\CreateCommentForm;
8
use \CJ\Comment\HTMLForm\EditCommentForm;
9
10
/**
11
 * A controller for the comment section
12
 */
13
class CommentController implements InjectionAwareInterface
14
{
15
    use InjectionAwareTrait;
16
17
    /**
18
     * process incomming POST
19
     */
20
    public function newComment()
21
    {
22
        $umodel = $this->di->get("umodel");
23
24
        if (!$umodel->isLoggedIn()) {
25
            $this->di->get("response")->redirect($this->di->get("url")->create("user/login"));
26
        }
27
28
        $form = new CreateCommentForm($this->di, $umodel->getLoggedInUser());
29
        $form->check();
30
31
        $data = ["form" => $form->getHTML()];
32
33
        $this->di->get("view")->add("user/createUser", $data);
34
35
        $this->di->get("pageRender")->renderPage(["title" => "CommentForm"]);
36
    }
37
38
    /**
39
     * remove one comment
40
     */
41
    public function removeComment($index)
42
    {
43
        $user = $this->di->get("umodel");
44
        $comment = $this->di->get("comment");
45
        $comment->getComment($index);
46
47 View Code Duplication
        if ($comment->userId !== $this->di->get("session")->get("user")) {
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...
48
            if ($user->isUserAdmin()) {
49
                // Do nothing
50
            } else {
51
                $this->di->get("response")->redirect($this->di->get("url")->create("comment"));
52
            }
53
        }
54
55
        $comment->deleteComment($index);
56
        $this->di->get("response")->redirect($this->di->get("url")->create("comment"));
57
    }
58
59
    /**
60
     * load edit page
61
     */
62
    public function editComment($index)
63
    {
64
        $comment = $this->di->get("comment")->getComment($index);
65
        $user = $this->di->get("umodel");
66
67
68 View Code Duplication
        if ($comment->userId !== $this->di->get("session")->get("user")) {
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...
69
            if ($user->isUserAdmin()) {
70
                // Do nothing
71
            } else {
72
                $this->di->get("response")->redirect($this->di->get("url")->create("comment"));
73
            }
74
        }
75
76
        $form = new EditCommentForm($this->di, $comment);
77
        $form->check();
78
79
        $data = ["form" => $form->getHTML()];
80
81
        $this->di->get("view")->add("user/update", $data, "main");
82
        $this->di->get("pageRender")->renderPage(["title" => "guestbook - edit"]);
83
    }
84
85
86
    /**
87
     * render comment view
88
     */
89
    public function renderComments()
90
    {
91
        $data = ["title" => "guestbook"];
92
        $comments = $this->di->get("comment")->getComments();
93
        $user = $this->di->get("umodel")->getLoggedInUser();
94
95
        $comments = array_reverse($comments);
96
97
        $this->di->get("view")->add("components/commentholder", [
98
            "comments" => $comments,
99
            "user" => $user
100
        ], "main");
101
102
        $this->di->get("pageRender")->renderPage($data);
103
    }
104
}
105