CreateUserForm   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 104
Duplicated Lines 35.58 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 50.94%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 37
loc 104
ccs 27
cts 53
cp 0.5094
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B callbackSubmit() 37 38 2
A __construct() 0 50 3

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 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 CreateUserForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19 1
    public function __construct(DIInterface $di)
20
    {
21 1
        parent::__construct($di);
22
23 1
        $editable = "hidden";
24 1
        $acronym = $this->di->session->get("user");
25
26 1
        if (isset($acronym)) {
27
            $role = $this->di->get("commentController")->getRole($acronym);
28
            if ($role == 10) {
29
                $editable = "text";
30
            }
31
        }
32
33 1
        $this->form->create(
34
            [
35 1
                "id" => __CLASS__,
36 1
                "legend" => "Create user",
37 1
            ],
38
            [
39
                "acronym" => [
40 1
                    "type"        => "text",
41 1
                ],
42
43
                "password" => [
44 1
                    "type"        => "password",
45 1
                ],
46
47
                "password-again" => [
48 1
                    "type"        => "password",
49
                    "validation" => [
50
                        "match" => "password"
51 1
                    ],
52 1
                ],
53
                "email" => [
54 1
                    "type"        => "text",
55 1
                ],
56
                "role" => [
57 1
                    "type"        => $editable,
58 1
                    "value"       => 1,
59 1
                ],
60
61
                "submit" => [
62 1
                    "type" => "submit",
63 1
                    "value" => "Create user",
64 1
                    "callback" => [$this, "callbackSubmit"]
65 1
                ],
66
            ]
67 1
        );
68 1
    }
69
70
71
    /**
72
    * Callback for submit-button which should return true if it could
73
    * carry out its work and false if something failed.
74
    *
75
    * @return boolean true if okey, false if something went wrong.
76
    */
77 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...
78
    {
79
    // Get values from the submitted form
80
81
        $acronym       = $this->form->value("acronym");
82
        $password      = $this->form->value("password");
83
        $passwordAgain = $this->form->value("password-again");
84
        $email         = $this->form->value("email");
85
        $role          = $this->form->value("role");
86
87
        // Check password matches
88
        if ($password !== $passwordAgain) {
89
            $this->form->rememberValues();
90
            $this->form->addOutput("Password did not match.");
91
            return false;
92
        }
93
94
        // Save to database
95
        // $db = $this->di->get("db");
96
        // $password = password_hash($password, PASSWORD_DEFAULT);
97
        // $db->connect()
98
        //    ->insert("User", ["acronym", "password"])
99
        //    ->execute([$acronym, $password]);
100
        $user = new User();
101
        $user->setDb($this->di->get("db"));
102
        $user->acronym = $acronym;
103
        $user->email = $email;
104
        $user->role = $role;
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...
105
        // create gravatar from email
106
        $gravatar = $user->gravatar($email);
107
        $user->gravatar = $gravatar;
108
109
        $user->setPassword($password);
110
        $user->save();
111
112
        $this->form->addOutput("User was created.");
113
        return true;
114
    }
115
}
116