Completed
Pull Request — master (#1538)
by
unknown
15:50
created

AdaptiveCommandParams::getInputParams()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 0
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Permission\Traits;
4
5
use Illuminate\Support\Str;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Spatie\Permission\Contracts\Role as RoleContract;
9
10
trait AdaptiveCommandParams
11
{
12
    /**
13
     * The built-in columns.
14
     *
15
     * @var array
16
     */
17
    protected $builtInFields = ['id', 'name', 'guard_name', 'created_at', 'updated_at', 'deleted_at'];
18
19
    /**
20
     * The required extra columns.
21
     *
22
     * @var array
23
     */
24
    protected $requiredExtraFields = [];
25
26
    /**
27
     * The optional extra columns.
28
     *
29
     * @var array
30
     */
31
    protected $optionalExtraFields = [];
32
33
    /**
34
     * Register arguments or options for extra columns in database.
35
     *
36
     * @param object $model The model instance
37
     *
38
     * @return void
39
     */
40
    protected function registerCustomColumns($model)
41
    {
42
        $table = $model->getTable();
43
        $columns = $model->getConnection()->getDoctrineSchemaManager()->listTableColumns($table);
44
        $modelType = ($model instanceof RoleContract) ? 'role' : 'permission';
45
46
        foreach ($columns as $column) {
47
            // We will look for columns that are fillable and
48
            // are not the inherent columns of the package
49
50
            $columnName = $column->getName();
51
            $isAutoIncrement = $column->getAutoincrement();
52
53
            if (! in_array($columnName, $this->builtInFields) && $model->isFillable($columnName) && ! $isAutoIncrement) {
54
                $isNotNullable = $column->getNotnull();
55
                $defaultValue = $column->getDefault();
56
                $columnLength = $column->getLength();
0 ignored issues
show
Unused Code introduced by
$columnLength 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...
57
                $columnType = $column->getType();
58
                $suffixTitle = ('boolean' == Str::slug($columnType) && Str::startsWith($columnName, 'is')) ? 'status' : null;
59
                $columnTitle = is_null($suffixTitle) ? str_replace(['-', '_'], ' ', $columnName) : ($columnName.' '.$suffixTitle);
60
61
                if ($isNotNullable && is_null($defaultValue)) {
62
                    // This is the case that the column needs to fill in the value.
63
                    // We will register it in the "requiredExtraFields" property
64
65
                    $this->requiredExtraFields[] = [
66
                        $columnName,
67
                        InputArgument::REQUIRED,
68
                        'The '.Str::lower($columnTitle).' of the '.$modelType,
69
                    ];
70
                } else {
71
                    // This is the case that the column does not require or has a default value.
72
                    // We will register it in the "optionalExtraFields" property
73
74
                    $this->optionalExtraFields[] = [
75
                        $columnName,
76
                        null,
77
                        InputOption::VALUE_OPTIONAL,
78
                        'The '.Str::lower($columnTitle).' of the '.$modelType,
79
                        $defaultValue,
80
                    ];
81
                }
82
            }
83
        }
84
    }
85
86
    /**
87
     * Get the input values of the command parameters.
88
     *
89
     * @return array
90
     */
91
    protected function getInputParams()
92
    {
93
        $params = [];
94
95
        foreach ($this->getArguments() as $argument) {
0 ignored issues
show
Bug introduced by
It seems like getArguments() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
96
            $key = $argument[0];
97
            $value = $this->argument($key);
0 ignored issues
show
Bug introduced by
It seems like argument() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
98
99
            $params[$key] = $value;
100
        }
101
102
        foreach ($this->getOptions() as $option) {
0 ignored issues
show
Bug introduced by
It seems like getOptions() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
103
            $key = $option[0];
104
            $value = $this->option($key);
0 ignored issues
show
Bug introduced by
It seems like option() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
105
106
            if ('guard' == $key) {
107
                $params['guard_name'] = $value;
108
            } else {
109
                $params[$key] = $value;
110
            }
111
        }
112
113
        return $params;
114
    }
115
}
116