Completed
Pull Request — master (#18)
by
unknown
04:15
created

CommentController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 124
Duplicated Lines 41.13 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 7
Bugs 2 Features 4
Metric Value
wmc 8
lcom 1
cbo 3
dl 51
loc 124
rs 10
c 7
b 2
f 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A viewAction() 0 10 1
B addAction() 25 25 2
A editAction() 0 19 2
B updateAction() 26 26 2
A deleteAction() 0 10 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
 * class for commenting
4
 */
5
namespace Loom\Comment;
6
7
class CommentController extends \Phpmvc\Comment\CommentController
0 ignored issues
show
Bug introduced by
There is one abstract method setDI in this class; you could implement it, or declare this class as abstract.
Loading history...
8
{
9
10
    /**
11
     * View all comments.
12
     *
13
     * @return void
14
     */
15
    public function viewAction()
16
    {
17
        $comments = new \Loom\Comment\CommentsInSession();
18
        $comments->setDI($this->di);
0 ignored issues
show
Bug introduced by
The property di does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
20
        $all = $comments->findAll();
21
        $this->views->add('comment/comments', [
0 ignored issues
show
Bug introduced by
The property views does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
            'comments' => $all,
23
        ]);
24
    }
25
26
27
    /**
28
     * Add a comment.
29
     *
30
     * @return void
31
     */
32 View Code Duplication
    public function addAction()
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...
33
    {
34
        $isPosted = $this->request->getPost('doCreate');
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
36
        if (!$isPosted) {
37
            $this->response->redirect($this->request->getPost('redirect'));
0 ignored issues
show
Bug introduced by
The property response does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
        }
39
40
        $comment = [
41
            'content'   => $this->request->getPost('content'),
42
            'name'      => $this->request->getPost('name'),
43
            'web'       => $this->request->getPost('web'),
44
            'mail'      => $this->request->getPost('mail'),
45
            'timestamp' => time(),
46
            'ip'        => $this->request->getServer('REMOTE_ADDR'),
47
            'comment-flow'        => $this->request->getPost('comment-flow'),
48
        ];
49
50
        $comments = new \Phpmvc\Comment\CommentsInSession();
51
        $comments->setDI($this->di);
52
53
        $comments->add($comment);
54
55
        $this->response->redirect($this->request->getPost('redirect'));
56
    }
57
58
    /**
59
     * Edit a comment.
60
     *
61
     * @return void
62
     */
63
    public function editAction()
64
    {
65
        $comments = new \Phpmvc\Comment\CommentsInSession();
66
        $comments->setDI($this->di);
67
        $id = $this->request->getGet('id');
68
        $theComments = $comments->findAll();
69
        $this->validate->check($id, ['int', 'range' => [0, count($theComments)]])
0 ignored issues
show
Bug introduced by
The property validate does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
70
            or die("Wrong index. Comment does not exist!");
71
        $comment = $theComments[$id];
72
        $this->views->add('comment/edit', [
73
            'mail'      => $comment['mail'],
74
            'web'       => $comment['web'],
75
            'name'      => $comment['name'],
76
            'content'   => $comment['content'],
77
            'output'    => null,
78
            'id'        => $id,
79
            'commentFlow' => $comment['comment-flow'],
80
        ]);
81
    }
82
83
    /**
84
     * Update a comment.
85
     *
86
     * @return void
87
     */
88 View Code Duplication
    public function updateAction()
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...
89
    {
90
        $isPosted = $this->request->getPost('doUpdate');
91
92
        if (!$isPosted) {
93
            $this->response->redirect($this->request->getPost('redirect'));
94
        }
95
96
        $comment = [
97
            'content'   => $this->request->getPost('content'),
98
            'name'      => $this->request->getPost('name'),
99
            'web'       => $this->request->getPost('web'),
100
            'mail'      => $this->request->getPost('mail'),
101
            'timestamp' => time(),
102
            'ip'        => $this->request->getServer('REMOTE_ADDR'),
103
            'comment-flow'        => $this->request->getPost('comment-flow'),
104
        ];
105
        $id = $this->request->getPost('id');
106
107
        $comments = new \Loom\Comment\CommentsInSession();
108
        $comments->setDI($this->di);
109
110
        $comments->update($comment, $id);
111
112
        $this->response->redirect($this->request->getPost('redirect'));
113
    }
114
115
    /**
116
     * Delete a comment.
117
     *
118
     * @return void
119
     */
120
    public function deleteAction()
121
    {
122
        $id = $this->request->getGet('id');
123
        $comments = new \Loom\Comment\CommentsInSession();
124
        $comments->setDI($this->di);
125
126
        $comments->delete($id);
127
        // TODO: Lägg till metod i CRequestBasic med referer url. Check if HTTP_REFERER exists also and escape also.
128
        $this->response->redirect($_SERVER['HTTP_REFERER']);
129
    }
130
}
131