Completed
Push — master ( ab3e16...edcf17 )
by Avtandil
03:22
created

UserAdd   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 24.64 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 2
dl 17
loc 69
ccs 0
cts 38
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 17 38 5
A getGuard() 0 6 1

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
 * This file is part of the Laravel Lodash package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11
12
namespace Longman\LaravelLodash\Commands;
13
14
use Illuminate\Console\Command;
15
16
class UserAdd extends Command
17
{
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = 'user:add {email} {password?}
24
                {--guard= : The guard to use.}';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Create a new user.';
32
33
    /**
34
     * Execute the console command.
35
     *
36
     * @return mixed
37
     */
38
    public function handle()
39
    {
40
        $guard = $this->getGuard();
41
        $config = config('auth');
42
43 View Code Duplication
        if (! isset($config['guards'][$guard]['provider'])) {
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...
44
            $this->error('Provider not found for guard "' . $guard . '"!');
45
46
            return;
47
        }
48
        $provider = $config['guards'][$guard]['provider'];
49 View Code Duplication
        if (! isset($config['providers'][$provider]['model'])) {
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...
50
            $this->error('Model not found for provider "' . $provider . '"!');
51
52
            return;
53
        }
54
        $model = $config['providers'][$provider]['model'];
55
        $user = new $model;
56
57
        $email = $this->argument('email');
58
        $password = $this->argument('password');
59 View Code Duplication
        if (! $password) {
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...
60
            if ($this->confirm('Let system generate password?', true)) {
61
                $password = str_random(16);
62
            } else {
63
                $password = $this->secret('Please enter new password');
64
            }
65
        }
66
        $crypted_password = bcrypt($password);
0 ignored issues
show
Bug introduced by
It seems like $password defined by $this->argument('password') on line 58 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...
67
        $user->create([
68
            'email'    => $email,
69
            'password' => $crypted_password,
70
        ]);
71
72
        $this->comment('User successfully created');
73
        $this->info('E-Mail: ' . $email);
74
        $this->info('Password: ' . $password);
75
    }
76
77
    protected function getGuard(): string
78
    {
79
        $guard = $this->input->getOption('guard');
80
81
        return $guard ?? config('auth.defaults.guard');
82
    }
83
84
}
85