UpdateForm   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 112
Duplicated Lines 8.93 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 41 1
A getItemDetails() 0 7 1
A callbackSubmit() 10 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
namespace Forum\Forum\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Forum\Forum\Question;
8
9
/**
10
 * Form to update an item.
11
 */
12
class UpdateForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container and the id to update.
16
     *
17
     * @param Psr\Container\ContainerInterface $di a service container
18
     * @param integer             $id to update
19
     */
20
    public function __construct(ContainerInterface $di, $id)
21
    {
22
        parent::__construct($di);
23
        $book = $this->getItemDetails($id);
24
        $this->form->create(
25
            [
26
                "id" => __CLASS__,
27
                "legend" => "Update details of the item",
28
            ],
29
            [
30
                "id" => [
31
                    "type" => "text",
32
                    "validation" => ["not_empty"],
33
                    "readonly" => true,
34
                    "value" => $book->id,
35
                ],
36
37
                "column1" => [
38
                    "type" => "text",
39
                    "validation" => ["not_empty"],
40
                    "value" => $book->column1,
41
                ],
42
43
                "column2" => [
44
                    "type" => "text",
45
                    "validation" => ["not_empty"],
46
                    "value" => $book->column2,
47
                ],
48
49
                "submit" => [
50
                    "type" => "submit",
51
                    "value" => "Save",
52
                    "callback" => [$this, "callbackSubmit"]
53
                ],
54
55
                "reset" => [
56
                    "type"      => "reset",
57
                ],
58
            ]
59
        );
60
    }
61
62
63
64
    /**
65
     * Get details on item to load form with.
66
     *
67
     * @param integer $id get details on item with id.
68
     *
69
     * @return Book
70
     */
71
    public function getItemDetails($id) : object
72
    {
73
        $book = new Book();
74
        $book->setDb($this->di->get("dbqb"));
75
        $book->find("id", $id);
76
        return $book;
77
    }
78
79
80
81
    /**
82
     * Callback for submit-button which should return true if it could
83
     * carry out its work and false if something failed.
84
     *
85
     * @return bool true if okey, false if something went wrong.
86
     */
87 View Code Duplication
    public function callbackSubmit() : bool
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...
88
    {
89
        $book = new Book();
90
        $book->setDb($this->di->get("dbqb"));
91
        $book->find("id", $this->form->value("id"));
92
        $book->column1 = $this->form->value("column1");
93
        $book->column2 = $this->form->value("column2");
94
        $book->save();
95
        return true;
96
    }
97
98
99
100
    // /**
101
    //  * Callback what to do if the form was successfully submitted, this
102
    //  * happen when the submit callback method returns true. This method
103
    //  * can/should be implemented by the subclass for a different behaviour.
104
    //  */
105
    // public function callbackSuccess()
106
    // {
107
    //     $this->di->get("response")->redirect("book")->send();
108
    //     //$this->di->get("response")->redirect("book/update/{$book->id}");
109
    // }
110
111
112
113
    // /**
114
    //  * Callback what to do if the form was unsuccessfully submitted, this
115
    //  * happen when the submit callback method returns false or if validation
116
    //  * fails. This method can/should be implemented by the subclass for a
117
    //  * different behaviour.
118
    //  */
119
    // public function callbackFail()
120
    // {
121
    //     $this->di->get("response")->redirectSelf()->send();
122
    // }
123
}
124