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
04:17 queued 01:36
created

PermissionGeneratorBackpackCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 71
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 49 6
1
<?php
2
3
namespace Backpack\Generators\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Spatie\Permission\Models\Permission;
7
8
class PermissionGeneratorBackpackCommand extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
     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...
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Will generate permissions for routes and save to the database';
23
24
    /**
25
     * Execute the console command.
26
     *
27
     * @return mixed
28
     */
29
    public function handle()
30
    {
31
        $routeName = $this->argument('route');
32
        $permissions = ['list', 'create', 'update', 'reorder', 'delete'];
33
        $permissionsAdded = 0;
34
        $customPermissions = $this->option('permission');
35
36
        if (count($customPermissions) > 0) {
37
            $permissions = $customPermissions;
38
        }
39
40
        $bar = $this->output->createProgressBar(count($permissions));
41
42
        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...
43
            $permissionName = trim($routeName, '/').'/'.trim($permission, '/');
44
            $permissionName = strtolower($permissionName);
45
46
            $existingPermission = Permission::where(['name' => $permissionName])->first();
47
48
            if ($existingPermission) {
49
                $continueAdding = $this->confirm("$permissionName already exists, do you want to carry on? [y|N]");
50
51
                if ($continueAdding) {
52
                    $bar->setMessage("Skipping $permissionName");
53
                    $bar->advance();
54
                    continue;
55
                } else {
56
                    $bar->finish();
57
58
                    return $this->error("\nCancelled adding any more permissions.");
59
                }
60
            } else {
61
                $newPermission = Permission::create(['name' => $permissionName]);
62
                $newPermission->save();
63
64
                if ($newPermission->id) {
65
                    $permissionsAdded++;
66
                    $bar->setMessage("$newPermission->name added with ID: $newPermission->id");
67
                    $bar->advance();
68
                } else {
69
                    $bar->setMessage("$permissionName could not be created.");
70
                    $bar->advance();
71
                }
72
            }
73
        }
74
75
        $bar->finish();
76
        $this->info("\nFinished adding $permissionsAdded new permissions");
77
    }
78
}
79