Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#7)
by
unknown
02:44
created

PermissionGeneratorBackpackCommand::handle()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 53
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 8.7155
c 0
b 0
f 0
cc 6
eloc 33
nc 10
nop 0

How to fix   Long Method   

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 Backpack\Generators\Console\Commands;
4
5
use Artisan;
6
use Illuminate\Console\Command;
7
use Spatie\Permission\Models\Permission;
8
9
class PermissionGeneratorBackpackCommand extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
     protected $signature = 'backpack:permissions {route : Name of the route e.g admin/teams} {--P|permission=* : A permission you wish to add, can accept multiple instances of -P|permission}';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 193 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Will generate permissions for routes and save to the database';
24
25
    /**
26
     * Execute the console command.
27
     *
28
     * @return mixed
29
     */
30
    public function handle()
31
    {
32
        $routeName         = $this->argument('route');
33
        $permissions       = ['list', 'create', 'update', 'reorder', 'delete'];
34
        $permissionsAdded  = 0;
35
        $customPermissions = $this->option('permission');
36
37
        if( count($customPermissions) > 0 ){
38
            $permissions = $customPermissions;
39
        }
40
41
        $bar = $this->output->createProgressBar(count($permissions));
42
43
        foreach($permissions as $permission){
0 ignored issues
show
Bug introduced by
The expression $permissions of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
44
45
            $permissionName = trim($routeName, '/') . '/' . trim($permission, '/');
46
            $permissionName = strtolower($permissionName);
47
48
            $existingPermission = Permission::where(['name' => $permissionName])->first();
49
50
            if( $existingPermission ){
51
52
                $continueAdding = $this->confirm("$permissionName already exists, do you want to carry on? [y|N]");
53
54
                if( $continueAdding ){
55
                    $bar->setMessage("Skipping $permissionName");
56
                    $bar->advance();
57
                    continue;
58
                }
59
                else {
60
                    $bar->finish();
61
                    return $this->error("\nCancelled adding any more permissions.");
62
                }
63
            }
64
            else {
65
                $newPermission = Permission::create(['name' => $permissionName]);
66
                $newPermission->save();
67
68
                if( $newPermission->id ){
69
                    $permissionsAdded++;
70
                    $bar->setMessage("$newPermission->name added with ID: $newPermission->id");
71
                    $bar->advance();
72
                }
73
                else {
74
                    $bar->setMessage("$permissionName could not be created.");
75
                    $bar->advance();
76
                }
77
            }
78
        }
79
80
        $bar->finish();
81
        $this->info("\nFinished adding $permissionsAdded new permissions");
82
    }
83
}
84