CommentController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 137
Duplicated Lines 19.71 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 27
loc 137
ccs 0
cts 47
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A postComment() 0 11 1
A showComments() 14 14 1
A showEdit() 13 13 1
A saveEdit() 0 12 1
A deletePost() 0 7 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 Anax\Comment;
4
5
use \Anax\DI\InjectionAwareInterface;
6
use \Anax\DI\InjectionAwareTrait;
7
use \Anax\Comment\Comment;
8
9
/**
10
 * A controller for the Comment module
11
 *
12
 * @SuppressWarnings(PHPMD.ExitExpression)
13
 */
14
class CommentController implements InjectionAwareInterface
15
{
16
    use InjectionAwareTrait;
17
18
19
    //private $started = false;
20
21
    /**
22
     * Start the session.
23
     *
24
     * @return void
25
     */
26
     /*
27
    public function start()
28
    {
29
30
        if (session_status() == PHP_SESSION_NONE) {
31
            $this->di->get("session")->start();
32
        }
33
        if (!$this->di->get("comment")->hasComments()) {
34
            $this->di->get("comment")->init();
35
        }
36
        $this->started = true;
37
    }
38
    */
39
40
41
    /**
42
     * Destroy the session.
43
     *
44
     * @return void
45
     */
46
     /*
47
    public function destroy()
48
    {
49
        $this->di->get("session")->destroy();
50
        exit;
51
    }*/
52
53
54
55
    /**
56
     * Create a new item by getting the entry from the request body and add
57
     * to the dataset.
58
     *
59
     * @return void
60
     */
61
    public function postComment()
62
    {
63
        $session = $this->di->get("session");
64
        $comment = new Comment();
65
        $comment->setDb($this->di->get("db"));
66
        $username = $session->get("username");
67
        $content = getPost("comment");
68
        $gravatar = "https://www.gravatar.com/avatar/" . $comment->convertEmail($session->get("email"));
69
        $comment->addComment($username, $content, $gravatar);
70
        $this->di->get("response")->redirect("comment");
71
    }
72
73
    /**
74
     * Get comments
75
     *
76
     * @return array
77
     */
78
79 View Code Duplication
    public function showComments()
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...
80
    {
81
        //$this->start();
82
        $comment = new Comment();
83
        $comment->setDb($this->di->get("db"));
84
        $this->di->get("view")->add("comment/comment", [
85
         "comments" => $comment->findAll()
86
        ]);
87
88
       // Render a standard page using layout
89
        $this->di->get("pageRender")->renderPage([
90
            "title" => "Comment",
91
        ]);
92
    }
93
94
    /**
95
     * Edit a post
96
     *
97
     * @param string $postId for the post to edit
98
     *
99
     * @return void
100
     */
101
102 View Code Duplication
    public function showEdit($postId)
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...
103
    {
104
        $comment = new Comment();
105
        $comment->setDb($this->di->get("db"));
106
        $this->di->get("view")->add("comment/edit", [
107
         "values" => $comment->find("id", $postId)
108
        ]);
109
110
       // Render a standard page using layout
111
        $this->di->get("pageRender")->renderPage([
112
            "title" => "Edit",
113
        ]);
114
    }
115
116
    /**
117
     * Save the edited post
118
     *
119
     * @return void
120
     */
121
122
    public function saveEdit()
123
    {
124
        $session = $this->di->get("session");
125
        $comment = new Comment();
126
        $comment->setDb($this->di->get("db"));
127
        $id = getPost("id");
128
        $user = getPost("user");
129
        $content = getPost("comment");
130
        $gravatar = "https://www.gravatar.com/avatar/" . $comment->convertEmail($session->get("email"));
131
        $comment->updateComment($id, $user, $content, $gravatar);
132
        $this->di->get("response")->redirect("comment");
133
    }
134
135
    /**
136
     * Delete a post
137
     *
138
     * @param string $postId for the post to delete
139
     *
140
     * @return void
141
     */
142
143
    public function deletePost($postId)
144
    {
145
        $comment = new Comment();
146
        $comment->setDb($this->di->get("db"));
147
        $comment->deletePost($postId);
148
        $this->di->get("response")->redirect("comment");
149
    }
150
}
151