AdminUpdateUser   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 90
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 45 45 1
A getItemDetails() 7 7 1
A callbackSubmit() 12 12 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\User\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Radchasay\User\User;
8
9
/**
10
 * Form to update an item.
11
 */
12 View Code Duplication
class AdminUpdateUser extends FormModel
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    /**
15
     * Constructor injects with DI container and the id to update.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     * @param integer             $id to update
19
     */
20
    public function __construct(DIInterface $di, $id)
21
    {
22
        parent::__construct($di);
23
        $user = $this->getItemDetails($id);
24
        $this->form->create(
25
            [
26
                "id"     => __CLASS__,
27
                "legend" => "Update details of the item",
28
            ],
29
            [
30
                "id" => [
31
                    "type"       => "hidden",
32
                    "value"      => $user->id,
33
                ],
34
35
                "name" => [
36
                    "type"       => "text",
37
                    "validation" => ["not_empty"],
38
                    "value"      => $user->name,
39
                ],
40
41
                "email" => [
42
                    "type"       => "text",
43
                    "validation" => ["not_empty"],
44
                    "value"      => $user->email,
45
                ],
46
47
                "age" => [
48
                    "type"       => "number",
49
                    "validation" => ["not_empty"],
50
                    "value"      => $user->age,
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
     * Get details on item to load form with.
69
     *
70
     * @param integer $id get details on item with id.
71
     *
72
     * @return $user
0 ignored issues
show
Documentation introduced by
The doc-type $user could not be parsed: Unknown type name "$user" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
73
     */
74
    public function getItemDetails($id)
75
    {
76
        $user = new User();
77
        $user->setDb($this->di->get("db"));
78
        $user->find("id", $id);
79
        return $user;
80
    }
81
82
83
    /**
84
     * Callback for submit-button which should return true if it could
85
     * carry out its work and false if something failed.
86
     *
87
     * @return boolean true if okey, false if something went wrong.
88
     */
89
    public function callbackSubmit()
90
    {
91
        $user = new User();
92
        $user->setDb($this->di->get("db"));
93
        $user->find("id", $this->form->value("id"));
94
        $user->name = htmlentities($this->form->value("name"));
95
        $user->email = htmlentities($this->form->value("email"));
96
        $user->age = htmlentities($this->form->value("age"));
97
        $user->save();
98
        $url = $this->di->get("url")->create("admin");
99
        $this->di->get("response")->redirect($url . "/update/{$user->id}");
100
    }
101
}
102