UpdateForm   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 118
Duplicated Lines 9.32 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 11
loc 118
rs 10
c 0
b 0
f 0

4 Methods

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