Completed
Pull Request — develop (#69)
by
unknown
08:00
created

DeleteUser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * Copyright (C) 2016 Paul Heinrichs <[email protected]>
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
/**
19
 * DeleteUser.php
20
 *
21
 * @package    LibreNMS
22
 * @author     Paul Heinrichs <[email protected]>
23
 * @copyright  2016 Paul Heinrichs
24
 * @license    @license http://opensource.org/licenses/GPL-3.0 GNU Public License v3 or later
25
 */
26
27
namespace App\Console\Commands;
28
29
use App\Models\User;
30
use Illuminate\Console\Command;
31
32
class DeleteUser extends Command
33
{
34
    /**
35
     * The name and signature of the console command.
36
     *
37
     * @var string
38
     */
39
    protected $signature = 'user:delete
40
                            {user : The user to search}';
41
42
    /**
43
     * The console command description.
44
     *
45
     * @var string
46
     */
47
    protected $description = 'Remove a user';
48
49
    /**
50
     * Create a new command instance.
51
     *
52
     * @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...
53
     */
54
    public function __construct()
55
    {
56
        parent::__construct();
57
    }
58
59
    /**
60
     * Execute the console command.
61
     *
62
     * @return mixed
63
     */
64
    public function handle()
65
    {
66
        $user = $this->argument('user');
67
        $user_list = User::select('username')->where('username', 'like', '%'.$user.'%')->orWhere('realname','like', '%'.$user.'%')->get();
68
        $names = [];
69
70
        if (count($user_list) < 1 ) {
71
            $this->info('No user found.');
72
            return;
73
        }
74
        foreach ($user_list as $i) {
75
            array_push($names,$i->username);
76
        }
77
        if (count($names) > 1) {
78
            $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...
79
        }
80
        else {
81
            $name = $names[0];
82
        }
83
        if ($this->confirm('Do you wish to remove '.$name.'?')) {
84
            $remove_user = User::select('user_id')->where('username', $name)->delete();
0 ignored issues
show
Unused Code introduced by
$remove_user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
85
            $this->info('User deleted.');
86
        }
87
    }
88
}
89