GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ManageUsers::handle()   C
last analyzed

Complexity

Conditions 12
Paths 21

Size

Total Lines 70
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 43
nc 21
nop 0
dl 0
loc 70
rs 5.6441
c 1
b 0
f 0

How to fix   Long Method    Complexity   

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 App\Console\Commands;
4
5
use App\Models\User;
6
use Illuminate\Console\Command;
7
8
class ManageUsers extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'mmex:user {email?} {password?} {--delete} {--admin} {--noadmin}';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Add, remove and change users from commandline';
23
24
    /**
25
     * Create a new command instance.
26
     */
27
    public function __construct()
28
    {
29
        parent::__construct();
30
    }
31
32
    /**
33
     * Execute the console command.
34
     *
35
     * @return mixed
36
     */
37
    public function handle()
38
    {
39
        $email = $this->argument('email');
40
        $password = $this->argument('password');
41
42
        $delete = $this->option('delete');
43
        $admin = $this->option('admin');
44
        $noadmin = $this->option('noadmin');
45
46
        $user = User::whereEmail($email)->first();
47
48
        if ($delete) {
49
            if (!$user) {
50
                $this->error(sprintf('User %s does not exist!', $email));
51
            } else {
52
                $user->delete();
53
54
                $this->warn(sprintf('%s was deleted', $user->username));
55
            }
56
57
            return;
58
        }
59
60
        if (empty($email) && empty($password)) {
61
            $headers = ['Name', 'Email', 'Admin', 'MMEX Guid', 'Locale', 'API Key', 'Created at'];
62
63
            $users = User::all(['name', 'email', 'is_admin', 'mmex_guid', 'locale', 'api_token', 'created_at'])->toArray();
64
65
            $this->info(sprintf('%d users found', count($users)));
66
67
            $this->table($headers, $users);
68
69
            return;
70
        }
71
72
        if (!$user) {
73
            $name = $this->ask('Name of user?');
74
            $user = User::create([
75
                'name'     => $name,
76
                'email'    => $email,
77
                'password' => bcrypt($password),
0 ignored issues
show
Bug introduced by
It seems like $password defined by $this->argument('password') on line 40 can also be of type array; however, bcrypt() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
78
            ]);
79
80
            $this->info(sprintf('User %s was created!', $user->username));
81
        } else {
82
            if ($password) {
83
                $user->password = bcrypt($password);
0 ignored issues
show
Bug introduced by
It seems like $password defined by $this->argument('password') on line 40 can also be of type array; however, bcrypt() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
84
                $user->save();
85
86
                $this->info(sprintf('%s\'s password was updated!', $user->name));
87
            }
88
        }
89
90
        if ($admin || $noadmin) {
91
            if (!$user) {
92
                $this->error(sprintf('User %s does not exist!', $email));
93
            } else {
94
                $user->is_admin = $admin || $noadmin;
95
                $user->save();
96
97
                if ($admin == true) {
98
                    $this->info('Admin privileges set');
99
                } else {
100
                    $this->info('Admin privileges removed');
101
                }
102
103
                return;
104
            }
105
        }
106
    }
107
}
108