CreateForm   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 50
Duplicated Lines 44 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 3
dl 22
loc 50
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 22 22 1
A callbackSubmit() 0 11 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 Vibe\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Vibe\Comment\Comment;
8
9
/**
10
 * Form to create an item.
11
 */
12
class CreateForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19 View Code Duplication
    public function __construct(DIInterface $di)
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
        parent::__construct($di);
22
        $this->form->create(
23
            [
24
                "id" => __CLASS__,
25
                "legend" => "Details of the item",
26
            ],
27
            [
28
                "text" => [
29
                    "type" => "textarea",
30
                    "validation" => ["not_empty"],
31
                ],
32
33
                "submit" => [
34
                    "type" => "submit",
35
                    "value" => "Add comment",
36
                    "callback" => [$this, "callbackSubmit"]
37
                ],
38
            ]
39
        );
40
    }
41
42
43
44
    /**
45
     * Callback for submit-button which should return true if it could
46
     * carry out its work and false if something failed.
47
     *
48
     * @return boolean true if okey, false if something went wrong.
49
     */
50
    public function callbackSubmit()
51
    {
52
        $comment = new Comment();
53
        $comment->setDb($this->di->get("database"));
54
        $comment->userId = $this->di->get("session")->get("userId");
55
        $comment->email = $this->di->get("session")->get("userEmail");
56
        $comment->postedBy = $this->di->get("session")->get("username");
57
        $comment->text = $this->form->value("text");
58
        $comment->save();
59
        $this->di->get("response")->redirect("comment");
60
    }
61
}
62