Passed
Push — master ( b7618a...8c3cc9 )
by Mihail
15:19
created

User   B

Complexity

Total Complexity 19

Size/Duplication

Total Lines 173
Duplicated Lines 64.16 %

Coupling/Cohesion

Components 1
Dependencies 18

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 19
c 4
b 1
f 0
lcom 1
cbo 18
dl 111
loc 173
rs 7.3333

7 Methods

Rating   Name   Duplication   Size   Complexity  
B actionIndex() 26 26 1
B actionUpdate() 24 24 3
B actionDelete() 0 22 4
A actionGrouplist() 0 9 1
A actionGroupUpdate() 20 20 3
A actionSettings() 19 19 3
B actionInvite() 22 22 4

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 Apps\Controller\Admin;
4
5
use Apps\ActiveRecord\Role;
6
use Apps\Model\Admin\User\FormInviteSend;
7
use Apps\Model\Admin\User\FormUserDelete;
8
use Apps\Model\Admin\User\FormUserGroupUpdate;
9
use Apps\Model\Admin\User\FormUserSettings;
10
use Apps\Model\Admin\User\FormUserUpdate;
11
use Extend\Core\Arch\AdminAppController;
12
use Apps\ActiveRecord\User as UserRecords;
13
use Ffcms\Core\App;
14
use Ffcms\Core\Exception\NotFoundException;
15
use Ffcms\Core\Helper\HTML\SimplePagination;
16
17
18
class User extends AdminAppController
19
{
20
    const ITEM_PER_PAGE = 10;
21
22
    // list users
23 View Code Duplication
    public function actionIndex()
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...
24
    {
25
        // init Active Record
26
        $query = new UserRecords();
27
28
        // set current page and offset
29
        $page = (int)App::$Request->query->get('page');
30
        $offset = $page * self::ITEM_PER_PAGE;
31
32
        // build pagination
33
        $pagination = new SimplePagination([
34
            'url' => ['user/index'],
35
            'page' => $page,
36
            'step' => self::ITEM_PER_PAGE,
37
            'total' => $query->count()
0 ignored issues
show
Documentation Bug introduced by
The method count does not exist on object<Apps\ActiveRecord\User>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
38
        ]);
39
40
        // build listing objects
41
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
0 ignored issues
show
Documentation Bug introduced by
The method orderBy does not exist on object<Apps\ActiveRecord\User>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
42
43
        // display viewer
44
        $this->response = App::$View->render('index', [
45
            'records' => $records,
46
            'pagination' => $pagination
47
        ]);
48
    }
49
50
    // edit user profiles
51 View Code Duplication
    public function actionUpdate($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...
52
    {
53
        $user = UserRecords::findOrNew($id);
54
55
        // find user identify object
56
        //$user = App::$User->identity($id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
57
        // generate model data based on user object
58
        $model = new FormUserUpdate($user);
0 ignored issues
show
Documentation introduced by
$user is of type object<Illuminate\Suppor...atabase\Eloquent\Model>, but the function expects a object<Ffcms\Core\Interfaces\iUser>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
60
        // check is form is sended
61
        if ($model->send()) {
62
            if ($model->validate()) { // check validation
63
                $model->save();
64
                App::$Session->getFlashBag()->add('success', __('Data was successful updated'));
65
            } else {
66
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
67
            }
68
        }
69
70
        // render viewer
71
        $this->response = App::$View->render('user_update', [
72
            'model' => $model->export()
73
        ]);
74
    }
75
76
    /**
77
     * Delete user data
78
     * @param $id
79
     * @throws NotFoundException
80
     */
81
    public function actionDelete($id)
82
    {
83
        if ($id < !1 || !App::$User->isExist($id)) {
84
            throw new NotFoundException('User is not founded');
85
        }
86
87
        // get user object and load model
88
        $user = App::$User->identity($id);
89
        $model = new FormUserDelete($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by \Ffcms\Core\App::$User->identity($id) on line 88 can be null; however, Apps\Model\Admin\User\Fo...erDelete::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
90
91
        if ($model->send()) {
92
            $model->delete();
93
            App::$Response->redirect('user/index');
94
        } else {
95
            App::$Session->getFlashBag()->add('error', __('There is no way to revert this action! Be careful!'));
96
        }
97
98
        // set view response
99
        $this->response = App::$View->render('user_delete', [
100
            'model' => $model
101
        ]);
102
    }
103
104
    /**
105
     * Show all role groups
106
     */
107
    public function actionGrouplist()
108
    {
109
        // get all roles
110
        $roles = Role::getAll();
111
112
        $this->response = App::$View->render('group_list', [
113
            'records' => $roles
114
        ]);
115
    }
116
117
    /**
118
     * Edit and add groups
119
     * @param $id
120
     */
121 View Code Duplication
    public function actionGroupUpdate($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...
122
    {
123
        // find role or create new object
124
        $role = Role::findOrNew($id);
125
126
        $model = new FormUserGroupUpdate($role);
0 ignored issues
show
Documentation introduced by
$role is of type object<Illuminate\Suppor...atabase\Eloquent\Model>, but the function expects a object<Apps\ActiveRecord\Role>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127
        if ($model->send()) { // work with post request
128
            if ($model->validate()) {
129
                $model->save();
130
                App::$Session->getFlashBag()->add('success', __('Data was successful updated'));
131
            } else {
132
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
133
            }
134
        }
135
136
        // render view
137
        $this->response = App::$View->render('group_update', [
138
            'model' => $model
139
        ]);
140
    }
141
142
    /**
143
     * User identity settings
144
     */
145 View Code Duplication
    public function actionSettings()
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...
146
    {
147
        // load model and pass property's as argument
148
        $model = new FormUserSettings($this->getConfigs());
149
150
        if ($model->send()) {
151
            if ($model->validate()) {
152
                $this->setConfigs($model->getAllProperties());
153
                App::$Response->redirect('user/index');
154
            } else {
155
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
156
            }
157
        }
158
159
        // render view
160
        $this->response = App::$View->render('settings', [
161
            'model' => $model->export()
162
        ]);
163
    }
164
165
    /**
166
     * Send invite to users
167
     */
168 View Code Duplication
    public function actionInvite()
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...
169
    {
170
        // init model
171
        $model = new FormInviteSend();
172
173
        if ($model->send()) {
174
            if ($model->validate()) {
175
                if ($model->make()) {
176
                    App::$Session->getFlashBag()->add('success', __('Invite was successful send!'));
177
                } else {
178
                    App::$Session->getFlashBag()->add('error', __('Mail server connection is failed!'));
179
                }
180
            } else {
181
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
182
            }
183
        }
184
185
        // render view
186
        $this->response = App::$View->render('invite', [
187
            'model' => $model
188
        ]);
189
    }
190
}