UsersController::search()   C
last analyzed

Complexity

Conditions 9
Paths 45

Size

Total Lines 88
Code Lines 58

Duplication

Lines 63
Ratio 71.59 %

Importance

Changes 0
Metric Value
dl 63
loc 88
rs 5.2636
c 0
b 0
f 0
cc 9
eloc 58
nc 45
nop 0

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
namespace App\Controller\Admin;
3
4
use App\Controller\AppController;
5
6
class UsersController extends AppController
7
{
8
9
    /**
10
     * Search users form.
11
     *
12
     * @return void
13
     */
14
    public function index()
15
    {
16
    }
17
18
    /**
19
     * Search users.
20
     *
21
     * @return void
22
     */
23
    public function search()
24
    {
25
        //Keyword to search. (For pagination)
26 View Code Duplication
        if (!empty($this->request->getData('search'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
27
            $keyword = $this->request->getData('search');
28
            $this->request->session()->write('Search.Admin.Users.Keyword', $keyword);
29
        } else {
30
            if ($this->request->session()->read('Search.Admin.Users.Keyword')) {
31
                $keyword = $this->request->session()->read('Search.Admin.Users.Keyword');
32
            } else {
33
                $keyword = '';
34
            }
35
        }
36
37
        //Search type. (For pagination)
38 View Code Duplication
        if (!empty($this->request->getData('type'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
39
            $type = $this->request->getData('type');
40
            $this->request->session()->write('Search.Admin.Users.Type', $type);
41
        } else {
42
            if ($this->request->session()->read('Search.Admin.Users.Type')) {
43
                $type = $this->request->session()->read('Search.Admin.Users.Type');
44
            } else {
45
                $type = '';
46
            }
47
        }
48
49
        switch ($type) {
50 View Code Duplication
            case "username":
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
51
                $this->paginate = [
52
                    'limit' => 15,
53
                    'conditions' => [
54
                        'Users.username LIKE' => "%$keyword%"
55
                    ],
56
                    'order' => [
57
                        'Users.username' => 'asc'
58
                    ]
59
                ];
60
                break;
61
62 View Code Duplication
            case "ip":
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
63
                $this->paginate = [
64
                    'limit' => 15,
65
                    'conditions' => [
66
                        'Users.last_login_ip LIKE' => "%$keyword%"
67
                    ],
68
                    'order' => [
69
                        'Users.last_login_ip' => 'asc'
70
                    ]
71
                ];
72
                break;
73
74 View Code Duplication
            case "mail":
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
75
                $this->paginate = [
76
                    'limit' => 15,
77
                    'conditions' => [
78
                        'Users.email LIKE' => "%$keyword%"
79
                    ],
80
                    'order' => [
81
                        'Users.email' => 'asc'
82
                    ]
83
                ];
84
                break;
85
86
            case "group":
87
                $this->paginate = [
88
                    'limit' => 15,
89
                    'conditions' => [
90
                        'Groups.name LIKE' => "%$keyword%"
91
                    ],
92
                    'contain' => ['Groups']
93
                ];
94
                break;
95
96 View Code Duplication
            default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
97
                $this->paginate = [
98
                    'limit' => 15,
99
                    'conditions' => [
100
                        'Users.username LIKE' => "%$keyword%"
101
                    ],
102
                    'order' => [
103
                        'Users.username' => 'asc'
104
                    ]
105
                ];
106
        }
107
108
        $users = $this->paginate($this->Users->find());
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\Admin\UsersController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
109
        $this->set(compact('users', 'keyword', 'type'));
110
    }
111
112
    /**
113
     * Edit an user.
114
     *
115
     * @return \Cake\Network\Response|void
116
     */
117
    public function edit()
118
    {
119
        $user = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\Admin\UsersController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
120
            ->find()
121
            ->where([
122
                'Users.id' => $this->request->id
123
            ])
124
            ->first();
125
126
        //Check if the user is found.
127
        if (empty($user)) {
128
            $this->Flash->error(__d('admin', 'This user doesn\'t exist or has been deleted.'));
129
130
            return $this->redirect(['action' => 'index']);
131
        }
132
133
        if ($this->request->is('put')) {
134
            $this->Users->patchEntity(
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\Admin\UsersController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
135
                $user,
136
                $this->request->getParsedBody(),
137
                [
138
                    'validate' => 'update',
139
                    'accessibleFields' => ['group_id' => true]
140
                ]
141
            );
142
143
            if ($this->Users->save($user)) {
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\Admin\UsersController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
144
                $this->Flash->success(__d('admin', 'This user has been updated successfully !'));
145
146
                return $this->redirect(['action' => 'index']);
147
            }
148
        }
149
150
        $this->loadModel('Groups');
151
152
        $groups = $this->Groups->find('list');
0 ignored issues
show
Documentation introduced by
The property Groups does not exist on object<App\Controller\Admin\UsersController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
153
154
        $this->set(compact('user', 'groups'));
155
    }
156
157
    /**
158
     * Delete an user and all his articles, comments and likes.
159
     *
160
     * @return \Cake\Network\Response
161
     */
162
    public function delete()
163
    {
164
        $user = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\Admin\UsersController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
165
            ->find()
166
            ->where([
167
                'Users.id' => $this->request->id
168
            ])
169
            ->first();
170
171
        //Check if the user is found.
172
        if (empty($user) || $user->is_deleted == true) {
173
            $this->Flash->error(__d('admin', 'This user doesn\'t exist or has been deleted.'));
174
175
            return $this->redirect(['action' => 'index']);
176
        }
177
178
        $user->is_deleted = true;
179
180
        if ($this->Users->save($user)) {
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\Admin\UsersController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
181
            $this->Flash->success(__d('admin', 'This user has been deleted successfully !'));
182
183
            return $this->redirect(['action' => 'index']);
184
        }
185
186
        $this->Flash->error(__d('admin', 'Unable to delete this user.'));
187
188
        return $this->redirect(['action' => 'index']);
189
    }
190
191
    /**
192
     * Delete an avatar.
193
     *
194
     * @return \Cake\Network\Response
195
     */
196
    public function deleteAvatar()
197
    {
198
        $user = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\Admin\UsersController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
199
            ->find()
200
            ->where([
201
                'Users.id' => $this->request->id
202
            ])
203
            ->first();
204
205
        //Check if the user is found.
206
        if (empty($user)) {
207
            $this->Flash->error(__d('admin', 'This user doesn\'t exist or has been deleted.'));
208
209
            return $this->redirect(['action' => 'index']);
210
        }
211
212
        $user->avatar = '../img/avatar.png';
213
214
        if ($this->Users->save($user)) {
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\Admin\UsersController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
215
            $this->Flash->success(__d('admin', 'His avatar has been deleted successfully !'));
216
217
            return $this->redirect($this->referer());
0 ignored issues
show
Bug introduced by
It seems like $this->referer() targeting Cake\Controller\Controller::referer() can also be of type object<Cake\Http\ServerRequest>; however, Cake\Controller\Controller::redirect() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
218
        }
219
220
        $this->Flash->error(__d('admin', 'Unable to delete his avatar.'));
221
222
        return $this->redirect($this->referer());
0 ignored issues
show
Bug introduced by
It seems like $this->referer() targeting Cake\Controller\Controller::referer() can also be of type object<Cake\Http\ServerRequest>; however, Cake\Controller\Controller::redirect() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
223
    }
224
}
225