Passed
Push — master ( 76fde7...d6d75c )
by Magnus
01:55
created

CommentController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 217
Duplicated Lines 41.94 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 15.33%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 10
dl 91
loc 217
ccs 21
cts 137
cp 0.1533
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
B deleteComment() 0 27 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() 8 8 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
                var_dump($t->idcommentc);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($t->idcommentc); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
40
                $this->deleteCommentComment($t->idcommentc, true);
41
                $commentComments->getNext();
42
                echo "deleted";
43
            }
44
        }
45
46
        $comment->delete("idcomment", $commentId);
47
        $createUrl = $this->di->get("url")->create("comment/viewAllPosts");
48
        $url = isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : $createUrl;
49
        $this->di->get("response")->redirect($url);
50
    }
51
52 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...
53
    {
54
        $title = "Update comment";
55
        $view = $this->di->get("view");
56
        $pageRender = $this->di->get("pageRender");
57
        $form = new UpdateCommentForm($this->di, $commentid);
58
59
        $form->check();
60
61
        $data = [
62
            "form" => $form->getHTML(),
63
        ];
64
65
        $view->add("comment/editComment", $data);
66
67
        return $pageRender->renderPage(["title" => $title]);
68
    }
69
70
    public function viewAllPosts()
71
    {
72
        $title = "Retrieve all posts";
73
        $view = $this->di->get("view");
74
        $pageRender = $this->di->get("pageRender");
75
        $post = new Post();
76
        $post->setDb($this->di->get("db"));
77
78
        $posts = $post->getAllPosts();
79
80
        $allPosts = [];
81
82 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...
83
            array_push($allPosts, [$p, $post->getTags([$p->postid])]);
84
        }
85
86
        $data = [
87
            "items" => $allPosts,
88
        ];
89
90
        $view->add("comment/viewAllPosts", $data);
91
92
        return $pageRender->renderPage(["title" => $title]);
93
    }
94
95 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...
96
    {
97 1
        $title = "Create new post";
98 1
        $view = $this->di->get("view");
99 1
        $pageRender = $this->di->get("pageRender");
100 1
        $form = new CreatePostForm($this->di);
101
102 1
        $form->check();
103
104
        $data = [
105 1
            "form" => $form->getHTML(),
106 2
        ];
107
108 1
        $view->add("comment/addNewPost", $data);
109
110 1
        return $pageRender->renderPage(["title" => $title]);
111
    }
112
113 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...
114
    {
115 1
        if ($this->di->get("session")->has("email")) {
116 1
            $title = "Create new comment";
117 1
            $view = $this->di->get("view");
118 1
            $pageRender = $this->di->get("pageRender");
119 1
            $form = new CreateCommentForm($this->di, $id);
120
121 1
            $form->check();
122
123
            $data = [
124 1
                "form" => $form->getHTML(),
125 1
            ];
126
127 1
            $view->add("comment/addNewComment", $data);
128
129 1
            return $pageRender->renderPage(["title" => $title]);
130
        } else {
131
            $login = $this->di->get("url")->create("user/login");
132
            $this->di->get("response")->redirect($login);
133
            return false;
134
        }
135
    }
136
137
    public function postAndComments($id)
138
    {
139
        $title = "Retrieve one post with comments";
140
        $view = $this->di->get("view");
141
        $pageRender = $this->di->get("pageRender");
142
        $post = new Post();
143
        $post->setDb($this->di->get("db"));
144
        if ($post->checkId($id)) {
145
            if ($this->di->get("session")->has("email")) {
146
                $email = $this->di->get("session")->get("email");
147
                $user = new User();
148
                $user->setDb($this->di->get("db"));
149
                $permissions = $user->returnPermissions($email);
150
            } else {
151
                $permissions = "user";
152
            }
153
154
            $comment = new Comment();
155
            $comment->setDb($this->di->get("db"));
156
            $comments = $comment->getAllCommentsFromSpecificPost([$id]);
157
158
            $comment->getNext();
159
160
            $commentComments = new CommentComments();
161
            $commentComments->setDb($this->di->get("db"));
162
163
            $allComments = [];
164
165
            foreach ($comments as $comment) {
166
                array_push($allComments, [$comment,
167
                $commentComments->getAllCommentFromComments([$comment->idcomment])]);
168
                $commentComments->getNext();
169
            }
170
171
            $data = [
172
                "post" => $post->getPostInfo([$id]),
173
                "comments" => $allComments,
174
                "permissions" => $permissions,
175
                "tags" => $post->getTags([$id])
176
            ];
177
178
            $view->add("comment/onePostWithComment", $data);
179
180
            return $pageRender->renderPage(["title" => $title]);
181
        } else {
182
            $url = $this->di->get("url")->create("comment/viewAllPosts");
183
            $this->di->get("response")->redirect($url);
184
        }
185
    }
186
187 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...
188
    {
189
        if ($this->di->get("session")->has("email")) {
190
            $title = "Create new comment";
191
            $view = $this->di->get("view");
192
            $pageRender = $this->di->get("pageRender");
193
            $form = new CreateCommentCommentForm($this->di, $idcomment, $idpost);
194
195
            $form->check();
196
197
            $data = [
198
                "form" => $form->getHTML(),
199
            ];
200
201
            $view->add("comment/addNewComment", $data);
202
203
            return $pageRender->renderPage(["title" => $title]);
204
        } else {
205
            $login = $this->di->get("url")->create("user/login");
206
            $this->di->get("response")->redirect($login);
207
            return false;
208
        }
209
    }
210
211
    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...
212
    {
213
        if (!$nested) {
214
            $commentcomments = new CommentComments();
215
            $commentcomments->setDb($this->di->get("db"));
216
            $commentcomments->delete("idcommentc", $id);
217
            $createUrl = $this->di->get("url")->create("comment/viewAllPosts");
218
            $url = isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : $createUrl;
219
            $this->di->get("response")->redirect($url);
220
        } else {
221
            $commentcomments = new CommentComments();
222
            $commentcomments->setDb($this->di->get("db"));
223
            $commentcomments->delete("idcommentc", $id);
224
        }
225
    }
226
227
228 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...
229
    {
230
        $cat = new PostCategory();
231
        $cat->setDb($this->di->get("db"));
232
        // var_dump($cat->getId($category));
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
233
        $id = $cat->getId($category);
234
        return $id;
235
    }
236
}
237