Completed
Push — master ( 8927df...f70445 )
by Abdelrahman
02:05
created

src/Console/Commands/RoleCreateCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Fort\Console\Commands;
17
18
use Illuminate\Support\Str;
19
use Illuminate\Console\Command;
20
use Illuminate\Support\Facades\Lang;
21
use Illuminate\Contracts\Validation\Factory;
22
23
class RoleCreateCommand extends Command
24
{
25
    /**
26
     * The name and signature of the console command.
27
     *
28
     * @var string
29
     */
30
    protected $signature = 'fort:role:create
31
                            {name? : The name of the role}
32
                            {slug? : The slug of the role}
33
                            {--D|description= : The description of the role}';
34
35
    /**
36
     * The console command description.
37
     *
38
     * @var string
39
     */
40
    protected $description = 'Create a new role.';
41
42
    /**
43
     * Execute the console command.
44
     *
45
     * @return void
46
     */
47
    public function handle()
48
    {
49
        $data = array_filter([
50
51
            // Required role attributes
52
            'name'        => $name = $this->argument('name') ?: $this->ask(Lang::get('rinvex.fort::artisan.role.name')),
53
            'slug'        => $this->argument('slug') ?: Str::slug($name),
0 ignored issues
show
It seems like $name defined by $this->argument('name') ...t::artisan.role.name')) on line 52 can also be of type array; however, Illuminate\Support\Str::slug() 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...
54
55
            // Optional role attributes
56
            'description' => $this->option('description'),
57
58
        ]);
59
60
        $rules = [
61
            'name' => 'required|max:255',
62
            'slug' => 'required|max:255|alpha_dash|unique:'.config('rinvex.fort.tables.roles'),
63
        ];
64
65
        $validator = app(Factory::class)->make($data, $rules);
66
67
        if ($validator->fails()) {
68
            $this->error('Errors:');
69
70
            foreach ($validator->errors()->getMessages() as $key => $messages) {
71
                $this->error('- '.$key.': '.$messages[0]);
72
            }
73
        } else {
74
            $role = $this->laravel['rinvex.fort.role']->create($data);
75
76
            $this->info(Lang::get('rinvex.fort::artisan.role.created').' ['.Lang::get('rinvex.fort::artisan.role.id').': '.$role->id.', '.Lang::get('rinvex.fort::artisan.role.name').': '.$role->name.', '.Lang::get('rinvex.fort::artisan.role.slug').': '.$role->slug.']');
77
        }
78
    }
79
}
80