Completed
Pull Request — develop (#69)
by
unknown
07:13
created

DeleteUser::handle()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 20
rs 9.2
cc 4
eloc 14
nc 8
nop 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\User;
6
use Illuminate\Console\Command;
7
8
class DeleteUser extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'user:delete
16
                            {user : The user to search}';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Remove a user';
24
25
    /**
26
     * Create a new command instance.
27
     *
28
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
29
     */
30
    public function __construct()
31
    {
32
        parent::__construct();
33
    }
34
35
    /**
36
     * Execute the console command.
37
     *
38
     * @return mixed
39
     */
40
    public function handle()
41
    {
42
        $user = $this->argument('user');
43
        $user_list = User::select('username')->where('username', 'like', '%'.$user.'%')->orWhere('realname','like', '%'.$user.'%')->get();
44
        $names = [];
45
        foreach ($user_list as $i) {
46
            array_push($names,$i->username);
47
        }
48
        if (count($names) > 1) {
49
            $name = $this->choice('Who would you like to remove?',$names, false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|null.

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...
50
        }
51
        else {
52
            $name = $names[0];
53
        }
54
        if ($this->confirm('Do you wish to remove '.$name.'?')) {
55
            $remove_user = User::select('user_id')->where('username', $name)->first();
56
            User::find($remove_user->user_id)->delete();
57
            $this->info('User deleted.');
58
        }
59
    }
60
}
61