Passed
Push — master ( ea5d4f...e4e351 )
by Magnus
02:01
created

CommentController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 214
Duplicated Lines 42.52 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 15.55%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 1
cbo 10
dl 91
loc 214
ccs 21
cts 135
cp 0.1555
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
B deleteComment() 0 25 4
A editComment() 17 17 1
B viewAllPosts() 3 24 2
A newPost() 17 17 1
A newComment() 23 23 2
B postAndComments() 0 49 4
A newCommentComment() 23 23 2
A deleteCommentComment() 0 15 3
A returnCatId() 7 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 Radchasay\Comment;
4
5
use \Anax\DI\InjectionAwareInterface;
6
use \Anax\DI\InjectionAwareTrait;
7
use \Radchasay\Comment\Post;
8
use \Radchasay\Comment\Comment;
9
use \Radchasay\Comment\CommentComments;
10
use \Radchasay\Comment\PostCategory;
11
use \Radchasay\User\User;
12
use \Radchasay\Comment\HTMLForm\CreatePostForm;
13
use \Radchasay\Comment\HTMLForm\CreateCommentForm;
14
use \Radchasay\Comment\HTMLForm\UpdateCommentForm;
15
use \Radchasay\Comment\HTMLForm\CreateCommentCommentForm;
16
17
/**
18
 * CommentModel
19
 */
20
class CommentController implements InjectionAwareInterface
21
{
22
    use InjectionAwareTrait;
23
24
    public function deleteComment($commentId)
0 ignored issues
show
Coding Style introduced by
deleteComment uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
25
    {
26
        $comment = new Comment();
27
        $comment->setDb($this->di->get("db"));
28
29
30
        $commentComments = new CommentComments();
31
        $commentComments->setDb($this->di->get("db"));
32
33
        $test = $commentComments->getAllCommentFromComments([$commentId]);
34
35
        $commentComments->getNext();
36
37
        if (!empty($test)) {
38
            foreach ($test as $t) {
39
                $this->deleteCommentComment($t->idcommentc, true);
40
                $commentComments->getNext();
41
            }
42
        }
43
44
        $comment->delete("idcomment", $commentId);
45
        $createUrl = $this->di->get("url")->create("comment/viewAllPosts");
46
        $url = isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : $createUrl;
47
        $this->di->get("response")->redirect($url);
48
    }
49
50 View Code Duplication
    public function editComment($commentid)
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...
51
    {
52
        $title = "Update comment";
53
        $view = $this->di->get("view");
54
        $pageRender = $this->di->get("pageRender");
55
        $form = new UpdateCommentForm($this->di, $commentid);
56
57
        $form->check();
58
59
        $data = [
60
            "form" => $form->getHTML(),
61
        ];
62
63
        $view->add("comment/editComment", $data);
64
65
        return $pageRender->renderPage(["title" => $title]);
66
    }
67
68
    public function viewAllPosts()
69
    {
70
        $title = "Retrieve all posts";
71
        $view = $this->di->get("view");
72
        $pageRender = $this->di->get("pageRender");
73
        $post = new Post();
74
        $post->setDb($this->di->get("db"));
75
76
        $posts = $post->getAllPosts();
77
78
        $allPosts = [];
79
80 View Code Duplication
        foreach ($posts as $p) {
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...
81
            array_push($allPosts, [$p, $post->getTags([$p->postid])]);
82
        }
83
84
        $data = [
85
            "items" => $allPosts,
86
        ];
87
88
        $view->add("comment/viewAllPosts", $data);
89
90
        return $pageRender->renderPage(["title" => $title]);
91
    }
92
93 2 View Code Duplication
    public function newPost()
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...
94
    {
95 1
        $title = "Create new post";
96 1
        $view = $this->di->get("view");
97 1
        $pageRender = $this->di->get("pageRender");
98 1
        $form = new CreatePostForm($this->di);
99
100 1
        $form->check();
101
102
        $data = [
103 1
            "form" => $form->getHTML(),
104 1
        ];
105
106 2
        $view->add("comment/addNewPost", $data);
107
108 1
        return $pageRender->renderPage(["title" => $title]);
109
    }
110
111 1 View Code Duplication
    public function newComment($id)
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...
112
    {
113 1
        if ($this->di->get("session")->has("email")) {
114 1
            $title = "Create new comment";
115 1
            $view = $this->di->get("view");
116 1
            $pageRender = $this->di->get("pageRender");
117 1
            $form = new CreateCommentForm($this->di, $id);
118
119 1
            $form->check();
120
121
            $data = [
122 1
                "form" => $form->getHTML(),
123 1
            ];
124
125 1
            $view->add("comment/addNewComment", $data);
126
127 1
            return $pageRender->renderPage(["title" => $title]);
128
        } else {
129
            $login = $this->di->get("url")->create("user/login");
130
            $this->di->get("response")->redirect($login);
131
            return false;
132
        }
133
    }
134
135
    public function postAndComments($id)
136
    {
137
        $title = "Retrieve one post with comments";
138
        $view = $this->di->get("view");
139
        $pageRender = $this->di->get("pageRender");
140
        $post = new Post();
141
        $post->setDb($this->di->get("db"));
142
        if ($post->checkId($id)) {
143
            if ($this->di->get("session")->has("email")) {
144
                $email = $this->di->get("session")->get("email");
145
                $user = new User();
146
                $user->setDb($this->di->get("db"));
147
                $permissions = $user->returnPermissions($email);
148
            } else {
149
                $permissions = "user";
150
            }
151
152
            $comment = new Comment();
153
            $comment->setDb($this->di->get("db"));
154
            $comments = $comment->getAllCommentsFromSpecificPost([$id]);
155
156
            $comment->getNext();
157
158
            $commentComments = new CommentComments();
159
            $commentComments->setDb($this->di->get("db"));
160
161
            $allComments = [];
162
163
            foreach ($comments as $comment) {
164
                array_push($allComments, [$comment,
165
                $commentComments->getAllCommentFromComments([$comment->idcomment])]);
166
                $commentComments->getNext();
167
            }
168
169
            $data = [
170
                "post" => $post->getPostInfo([$id]),
171
                "comments" => $allComments,
172
                "permissions" => $permissions,
173
                "tags" => $post->getTags([$id])
174
            ];
175
176
            $view->add("comment/onePostWithComment", $data);
177
178
            return $pageRender->renderPage(["title" => $title]);
179
        } else {
180
            $url = $this->di->get("url")->create("comment/viewAllPosts");
181
            $this->di->get("response")->redirect($url);
182
        }
183
    }
184
185 View Code Duplication
    public function newCommentComment($idcomment, $idpost)
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...
186
    {
187
        if ($this->di->get("session")->has("email")) {
188
            $title = "Create new comment";
189
            $view = $this->di->get("view");
190
            $pageRender = $this->di->get("pageRender");
191
            $form = new CreateCommentCommentForm($this->di, $idcomment, $idpost);
192
193
            $form->check();
194
195
            $data = [
196
                "form" => $form->getHTML(),
197
            ];
198
199
            $view->add("comment/addNewComment", $data);
200
201
            return $pageRender->renderPage(["title" => $title]);
202
        } else {
203
            $login = $this->di->get("url")->create("user/login");
204
            $this->di->get("response")->redirect($login);
205
            return false;
206
        }
207
    }
208
209
    public function deleteCommentComment($id, $nested = false)
0 ignored issues
show
Coding Style introduced by
deleteCommentComment uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
210
    {
211
        if (!$nested) {
212
            $commentcomments = new CommentComments();
213
            $commentcomments->setDb($this->di->get("db"));
214
            $commentcomments->delete("idcommentc", $id);
215
            $createUrl = $this->di->get("url")->create("comment/viewAllPosts");
216
            $url = isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : $createUrl;
217
            $this->di->get("response")->redirect($url);
218
        } else {
219
            $commentcomments = new CommentComments();
220
            $commentcomments->setDb($this->di->get("db"));
221
            $commentcomments->delete("idcommentc", $id);
222
        }
223
    }
224
225
226 View Code Duplication
    public function returnCatId($category)
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...
227
    {
228
        $cat = new PostCategory();
229
        $cat->setDb($this->di->get("db"));
230
        $id = $cat->getId($category);
231
        return $id;
232
    }
233
}
234