CommentsController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 18.92 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 63.64%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 12
c 3
b 2
f 0
lcom 1
cbo 1
dl 14
loc 74
ccs 28
cts 44
cp 0.6364
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A inject() 0 5 1
A add() 0 8 2
A delete() 7 7 2
A edit() 7 7 2
A get() 0 4 1
A addCommentSection() 0 7 1
A renderMain() 0 7 1
A renderEdit() 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\Comments;
4
5
use \Anax\DI\InjectionAwareInterface;
6
use \Anax\DI\InjectionAwareTrait;
7
8
class CommentsController implements InjectionAwareInterface
9
{
10
    use InjectionAwareTrait;
11
12
    private $db;
13
    private $session;
14
    private $response;
15
16 1
    public function init($database)
17
    {
18 1
        $this->db = $database;
19 1
        $this->response = $this->di->get("response");
20 1
    }
21
22 1
    public function inject($session)
23
    {
24 1
        $this->session = $session;
25 1
        return $this;
26
    }
27
28 1
    public function add()
29
    {
30 1
        $user = $this->session->get("user");
31 1
        $this->di->get("comments")->addComment($_POST, $this->db, $user);
32 1
        if (!headers_sent()) {
33
            $this->response->redirect("comments");
34
        }
35 1
    }
36
37 1 View Code Duplication
    public function delete()
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...
38
    {
39 1
        $this->di->get("comments")->deleteComment($_GET['id'], $this->db);
40 1
        if (!headers_sent()) {
41
            $this->response->redirect("comments");
42
        }
43 1
    }
44
45 1 View Code Duplication
    public function edit()
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...
46
    {
47 1
        $this->di->get("comments")->editComment($_POST['id'], $_POST['comment'], $this->db);
48 1
        if (!headers_sent()) {
49
            $this->response->redirect("comments");
50
        }
51 1
    }
52
53 1
    public function get($id)
54
    {
55 1
        return $this->di->get("comments")->getComment($id, $this->db);
56
    }
57
58 1
    public function addCommentSection()
59
    {
60 1
        $url = $this->di->get("url")->create('post_comment');
61 1
        $del = $this->di->get("url")->create('delete_comment');
62 1
        $edit = $this->di->get("url")->create('preview');
63 1
        $this->di->get("comments")->commentSection($url, $del, $edit, $this->di->get("session"));
64 1
    }
65
66
    public function renderMain()
67
    {
68
        $this->di->get("view")->add("comments/comments");
69
        $this->di->get("pageRender")->renderPage([
70
            "title" => "Comments"
71
        ]);
72
    }
73
74
    public function renderEdit()
75
    {
76
        $this->di->get("view")->add("comments/edit_comment");
77
        $this->di->get("pageRender")->renderPage([
78
            "title" => "Edit Comment"
79
        ]);
80
    }
81
}
82