EditUserForm::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 32
cts 32
cp 1
rs 9.4929
c 0
b 0
f 0
cc 1
eloc 30
nc 1
nop 2
crap 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Anax\User\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Anax\User\User;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class EditUserForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19 2
    public function __construct(DIInterface $di, $acronym)
20
    {
21 2
        parent::__construct($di);
22
23 2
        $user = new User();
24 2
        $user->setDb($this->di->get("db"));
25
26
        // $acronym = $this->di->session->get("user");
27 2
        $user = $user->find("acronym", $acronym);
28
29 2
        $this->form->create(
30
            [
31 2
                "id" => __CLASS__,
32 2
                "legend" => "Redigera användare",
33 2
            ],
34
            [
35
                "id" => [
36 2
                    "value"       => $user->id,
37 2
                    "type"        => "hidden",
38 2
                ],
39
                "acronym" => [
40 2
                    "type"        => "text",
41 2
                    "value"       => $user->acronym,
42 2
                ],
43
44
                "password" => [
45 2
                    "type"        => "password",
46 2
                ],
47
                "role" => [
48 2
                    "type"        => "hidden",
49 2
                    "value"       => 1,
50 2
                ],
51
52
                "password-again" => [
53 2
                    "type"        => "password",
54
                    "validation" => [
55
                        "match" => "password"
56 2
                    ],
57 2
                ],
58
                "email" => [
59 2
                    "type"        => "text",
60 2
                    "value"       => $user->email,
61 2
                ],
62
63
                "submit" => [
64 2
                    "type" => "submit",
65 2
                    "value" => "Skicka",
66 2
                    "callback" => [$this, "callbackSubmit"]
67 2
                ],
68
            ]
69 2
        );
70 2
    }
71
72
73
    /**
74
    * Callback for submit-button which should return true if it could
75
    * carry out its work and false if something failed.
76
    *
77
    * @return boolean true if okey, false if something went wrong.
78
    */
79 1 View Code Duplication
    public function callbackSubmit()
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...
80
    {
81
    // Get values from the submitted form
82 1
        $id            = $this->form->value("id");
83 1
        $acronym       = $this->form->value("acronym");
84 1
        $password      = $this->form->value("password");
85 1
        $passwordAgain = $this->form->value("password-again");
86 1
        $email         = $this->form->value("email");
87
88
        // Check password matches
89 1
        if ($password !== $passwordAgain) {
90
            $this->form->rememberValues();
91
            $this->form->addOutput("Password did not match.");
92
            return false;
93
        }
94
95
        // Save to database
96
        // $db = $this->di->get("db");
97
        // $password = password_hash($password, PASSWORD_DEFAULT);
98
        // $db->connect()
99
        //    ->insert("User", ["acronym", "password"])
100
        //    ->execute([$acronym, $password]);
101 1
        $user = new User();
102 1
        $user->setDb($this->di->get("db"));
103
104 1
        $user->acronym = $acronym;
105 1
        $user->email = $email;
106 1
        $user->id = $id;
107 1
        $user->role = 1;
0 ignored issues
show
Bug introduced by
The property role does not seem to exist in Anax\User\User.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
108
109
        // create/update gravatar from email
110 1
        $gravatar = $user->gravatar($email);
111 1
        $user->gravatar = $gravatar;
112
113 1
        $user->setPassword($password);
114 1
        $user->save();
115
116 1
        $this->form->addOutput("Användarprofil uppdaterades.");
117 1
        return true;
118
    }
119
}
120