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

CreateCommentCommentForm   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 74
Duplicated Lines 35.14 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 4
dl 26
loc 74
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 32 1
B callbackSubmit() 26 26 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\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Radchasay\Comment\CommentComments;
8
use \Radchasay\User\User;
9
10
/**
11
 * Example of FormModel implementation.
12
 */
13
class CreateCommentCommentForm extends FormModel
14
{
15
    /**
16
     * Constructor injects with DI container.
17
     *
18
     * @param Anax\DI\DIInterface $di a service container
19
     */
20
    public function __construct(DIInterface $di, $idcomment, $idpost)
21
    {
22
        parent::__construct($di);
23
24
        $this->form->create(
25
            [
26
                "id"     => __CLASS__,
27
                "legend" => "Create new comment",
28
            ],
29
            [
30
                    "text"    => [
31
                        "type"     => "textarea"
32
                    ],
33
34
                    "hiddenid" => [
35
                        "type"       => "hidden",
36
                        "value"      => $idcomment,
37
                    ],
38
39
                    "hiddenpost" => [
40
                        "type" => "hidden",
41
                        "value" => $idpost,
42
                    ],
43
44
                    "submit" => [
45
                        "type"     => "submit",
46
                        "value"    => "Create comment",
47
                        "callback" => [$this, "callbackSubmit"],
48
                    ],
49
            ]
50
        );
51
    }
52
53
54
    /**
55
     * Callback for submit-button which should return true if it could
56
     * carry out its work and false if something failed.
57
     *
58
     * @return boolean true if okey, false if something went wrong.
59
     */
60 View Code Duplication
    public function callbackSubmit()
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...
61
    {
62
        // Get values from the submitted form
63
        //
64
        $commentComment = new CommentComments();
65
        $commentComment->setDB($this->di->get("db"));
66
        $data = $this->form->value("text");
67
        $text = $this->di->get("textfilter")->doFilter($data, ["bbcode", "clickable",
68
        "shortcode", "markdown", "purify"]);
69
        $commentComment->textcomment = $text;
70
        $commentComment->idcommentcomment = htmlentities($this->form->value("hiddenid"));
71
        $commentComment->postuser = $this->di->get("session")->get("email");
72
73
        $idpost = htmlentities($this->form->value("hiddenpost"));
74
        $user = new User();
75
        $user->setDb($this->di->get("db"));
76
        $user->getInformation($commentComment->postuser);
77
        $user->points += 1;
0 ignored issues
show
Bug introduced by
The property points does not seem to exist in Radchasay\User\User.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
78
79
        $user->save();
80
        $commentComment->save();
81
82
        $url = $this->di->get("url")->create("comment/retrieve/$idpost");
83
        $this->di->get("response")->redirect($url);
84
        return true;
85
    }
86
}
87