UpdateUserForm   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 113
Duplicated Lines 6.19 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 7
loc 113
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
B callbackSubmit() 0 29 2
A loadUserData() 6 6 1
A __construct() 0 54 2

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 Alvo\User\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Alvo\User\User;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class UpdateUserForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Alvo\User\HTMLForm\Anax\DI\DIInterface was not found. Did you mean Anax\DI\DIInterface? If so, make sure to prefix the type with \.
Loading history...
18
     */
19
    public function __construct(DIInterface $di, $user = null)
20
    {
21
        parent::__construct($di);
22
23
        $this->user = $user;
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
24
        if (!$this->user) {
25
            $this->user = $this->loadUserData($di->get('session')->get('userId'));
26
        }
27
        $this->di = $di;
0 ignored issues
show
Documentation Bug introduced by
It seems like $di of type Anax\DI\DIInterface is incompatible with the declared type Anax\HTMLForm\Anax\DI\DIInterface of property $di.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
29
        $this->form->create(
30
            [
31
                "id" => __CLASS__,
32
                "class" => "center form",
33
                "wrapper-element" => "div",
34
                "use_fieldset" => false,
35
                // "br-after-label" => false,
36
            ],
37
            [
38
                "email" => [
39
                    "type" => "email",
40
                    "value" => esc($this->user->email),
41
                    "validation" => [
42
                    //     "custom_test" => [
43
                    //         "message" => "User with this email is already registered",
44
                    //         "test" => function ($email)
45
                    //         {
46
                    //             $check = $this->user->getUser('email', $email);
47
                    //             return !$check;
48
                    //         }
49
                    //     ],
50
                        "not_empty"
51
                    ]
52
                ],
53
54
                "password" => [
55
                    "type"        => "password",
56
                    "validation" => [
57
                        "not_empty"
58
                    ],
59
                ],
60
61
                "password-again" => [
62
                    "type"        => "password",
63
                    "validation" => [
64
                        "match" => "password"
65
                    ],
66
                ],
67
68
                "submit" => [
69
                    "type" => "submit",
70
                    "value" => "Update User",
71
                    "callback" => [$this, "callbackSubmit"],
72
                    "class" => "btn btn-warning"
73
                ],
74
            ]
75
        );
76
    }
77
78
79
80 View Code Duplication
    public function loadUserData($id)
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...
81
    {
82
        $user = new User();
83
        $user->setDb($this->di->get("db"));
84
        $user->find("id", $id);
85
        return $user;
86
    }
87
88
89
90
    /**
91
     * Callback for submit-button which should return true if it could
92
     * carry out its work and false if something failed.
93
     *
94
     * @return boolean true if okey, false if something went wrong.
95
     */
96
    public function callbackSubmit()
97
    {
98
        // Get values from the submitted form
99
        $email         = $this->form->value("email");
100
        $password      = $this->form->value("password");
101
        $passwordAgain = $this->form->value("password-again");
102
103
        // Check password matches
104
        if ($password !== $passwordAgain) {
105
            $this->form->rememberValues();
106
            $this->form->addOutput("Password did not match.");
107
            return false;
108
        }
109
110
        $user = new User();
111
        $user->setDb($this->di->get("db"));
112
        $user->find("id", $this->user->id);
113
        $user->email = $email;
114
        $user->setPassword($password);
115
        $user->updated = date("Y-m-d H:i:s");
116
        $user->save();
117
118
        // $this->di->get("user")->logout();
119
        // $t = $this->di->get("user")->login($email, $password);
120
        // debug($t);
121
122
        $this->form->addOutput("Profile has been updated.");
123
124
        return true;
125
    }
126
}
127