Completed
Push — master ( 27dc50...9a30da )
by Amine
02:16
created

ResourcesCommand::getResourceParams()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 58
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 7.6045
c 0
b 0
f 0
cc 7
eloc 25
nc 12
nop 2

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 namespace Wn\Generators\Commands;
2
3
use Symfony\Component\Yaml\Yaml;
4
5
6
class ResourcesCommand extends BaseCommand {
7
8
    protected $signature = 'wn:resources
9
        {file : Path to the file containing resources declarations}
10
        {--path=app : where to store the model files.}
11
        {--force= : override the existing files}
12
    ';
13
14
    protected $description = 'Generates multiple resources from a file';
15
16
    protected $pivotTables = [];
17
18
    public function handle()
19
    {
20
        $content = $this->fs->get($this->argument('file'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('file') targeting Illuminate\Console\Command::argument() can also be of type array; however, Illuminate\Filesystem\Filesystem::get() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
21
        $content = Yaml::parse($content);
22
23
        foreach ($content as $model => $i){
0 ignored issues
show
Bug introduced by
The expression $content of type array|string 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...
24
            $i = $this->getResourceParams($model, $i);
25
26
            $this->call('wn:resource', [
27
                'name' => $i['name'],
28
                'fields' => $i['fields'],
29
                '--add' => $i['add'],
30
                '--has-many' => $i['hasMany'],
31
                '--has-one' => $i['hasOne'],
32
                '--belongs-to' => $i['belongsTo'],
33
                '--belongs-to-many' => $i['belongsToMany'],
34
                '--path' => $this->option('path'),
35
                '--force' => $this->option('force')
36
            ]);
37
        }
38
39
        // $this->call('migrate'); // actually needed for pivot seeders !
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
40
41
        $this->pivotTables = array_map(
42
            'unserialize',
43
            array_unique(array_map('serialize', $this->pivotTables))
44
        );
45
46
        foreach ($this->pivotTables as $tables) {
47
            $this->call('wn:pivot-table', [
48
                'model1' => $tables[0],
49
                'model2' => $tables[1],
50
                '--force' => $this->option('force')
51
            ]);
52
53
            // $this->call('wn:pivot-seeder', [
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
54
            //     'model1' => $tables[0],
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55
            //     'model2' => $tables[1],
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
56
            //     '--force' => $this->option('force')
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
57
            // ]);
58
        }
59
60
        $this->call('migrate');
61
    }
62
63
    protected function getResourceParams($modelName, $i)
64
    {
65
        $i['name'] = snake_case($modelName);
66
67
        foreach(['hasMany', 'hasOne', 'add', 'belongsTo', 'belongsToMany'] as $relation){
68
            if(isset($i[$relation])){
69
                $i[$relation] = $this->convertArray($i[$relation], ' ', ',');
70
            } else {
71
                $i[$relation] = false;
72
            }
73
        }
74
75
        // if($i['belongsTo']){
0 ignored issues
show
Unused Code Comprehensibility introduced by
89% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
76
        //     $relations = $this->getArgumentParser('relations')->parse($i['belongsTo']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
77
        //     foreach ($relations as $relation){
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
78
        //         $foreignName = '';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
80
        //         if(! $relation['model']){
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
        //             $foreignName = snake_case($relation['name']) . '_id';
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
        //         } else {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
        //             $names = array_reverse(explode("\\", $relation['model']));
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
84
        //             $foreignName = snake_case($names[0]) . '_id';
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
85
        //         }
86
87
        //         $i['fields'][$foreignName] = [
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
88
        //             'schema' => 'integer',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
89
        //             'tags' => 'key'
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
90
        //         ];
91
        //     }
92
        // }
93
94
        if($i['belongsToMany']){
95
            $relations = $this->getArgumentParser('relations')->parse($i['belongsToMany']);
96
            foreach ($relations as $relation){
97
                $table = '';
0 ignored issues
show
Unused Code introduced by
$table 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...
98
99
                if(! $relation['model']){
100
                    $table = snake_case($relation['name']);
101
                } else {
102
                    $names = array_reverse(explode("\\", $relation['model']));
103
                    $table = snake_case($names[0]);
104
                }
105
106
                $tables = [ str_singular($table), $i['name'] ];
107
                sort($tables);
108
                $this->pivotTables[] = $tables;
109
            }
110
        }
111
112
        $fields = [];
113
        foreach($i['fields'] as $name => $value) {
0 ignored issues
show
Bug introduced by
The expression $i['fields'] of type string|false 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...
114
            $value['name'] = $name;
115
            $fields[] = $this->serializeField($value);
116
        }
117
        $i['fields'] = implode(' ', $fields);
118
119
        return $i;
120
    }
121
122
    protected function serializeField($field)
123
    {
124
        $name = $field['name'];
125
        $schema = $this->convertArray(str_replace(':', '.', $field['schema']), ' ', ':');
126
        $rules = (isset($field['rules'])) ? trim($field['rules']) : '';
127
        $tags = $this->convertArray($field['tags'], ' ', ',');
128
129
        $string = "{$name};{$schema};{$rules};{$tags}";
130
131
        if(isset($field['factory']) && !empty($field['factory'])){
132
            $string .= ';' . $field['factory'];
133
        }
134
135
        return $string;
136
    }
137
138
    protected function convertArray($list, $old, $new)
139
    {
140
        return implode($new, array_filter(explode($old, $list), function($item){
141
            return !empty($item);
142
        }));
143
    }
144
145
}
146