EditCommentForm   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 69
Duplicated Lines 53.62 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 3
dl 37
loc 69
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 37 37 1
A callbackSubmit() 0 18 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 CJ\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \CJ\Comment\Comment;
8
9
/**
10
 * Form to create an item.
11
 */
12
class EditCommentForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19 2 View Code Duplication
    public function __construct(DIInterface $di, $comment)
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...
20
    {
21 2
        parent::__construct($di);
22 2
        $this->form->create(
23
            [
24 2
                "id" => __CLASS__,
25 2
            ],
26
            [
27
                "title" => [
28 2
                    "type" => "text",
29 2
                    "validation" => ["not_empty"],
30 2
                    "class" => "form-control",
31 2
                    "value" => "$comment->heading"
32 2
                ],
33
34
                "message" => [
35 2
                    "type" => "textarea",
36 2
                    "validation" => ["not_empty"],
37 2
                    "class" => "form-control",
38 2
                    "value" => "$comment->msg"
39 2
                ],
40
41
                "id" => [
42 2
                    "type" => "hidden",
43 2
                    "class" => "form-control",
44 2
                    "value" => "$comment->id"
45 2
                ],
46
47
                "submit" => [
48 2
                    "type" => "submit",
49 2
                    "value" => "Comment",
50 2
                    "callback" => [$this, "callbackSubmit"],
51
                    "class" => "btn btn-default"
52 2
                ],
53
            ]
54 2
        );
55 2
    }
56
57
58
59
    /**
60
     * @return boolean true if okey, false if something went wrong.
61
     */
62 1
    public function callbackSubmit()
63
    {
64 1
        $title = $this->form->value("title");
65 1
        $message = $this->form->value("message");
66 1
        $id = $this->form->value("id");
67
68 1
        $comment = new Comment();
69 1
        $comment->setDb($this->di->get("db"));
70
71 1
        $comment->find("id", $id);
72 1
        $comment->msg = $message;
73 1
        $comment->heading = $title;
74
75 1
        $comment->save();
76
77 1
        $this->form->addOutput("Kommentar uppdaterad");
78 1
        return true;
79
    }
80
}
81