| Conditions | 23 |
| Paths | > 20000 |
| Total Lines | 158 |
| Lines | 45 |
| Ratio | 28.48 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php namespace Wn\Generators\Commands; |
||
| 34 | public function handle() |
||
| 35 | { |
||
| 36 | $files = $this->argument('files'); |
||
| 37 | $this->nodes = []; |
||
| 38 | foreach ($files as $file) { |
||
|
|
|||
| 39 | $this->info("Reading file ".$file); |
||
| 40 | |||
| 41 | $content = $this->fs->get($file); |
||
| 42 | $content = Yaml::parse($content); |
||
| 43 | |||
| 44 | foreach ($content as $model => $i){ |
||
| 45 | /* |
||
| 46 | $i['modelname'] = as originally in YAML defined |
||
| 47 | $i['name'] = as originally defined in snake_case |
||
| 48 | $i['uniquename']= for key in singular studly_case |
||
| 49 | */ |
||
| 50 | $i['filename'] = $file; |
||
| 51 | $i['modelname'] = $model; |
||
| 52 | $model = studly_case(str_singular($model)); |
||
| 53 | $i['uniquename'] = $model; |
||
| 54 | |||
| 55 | if (empty($this->nodes[$model]) || $this->option('force-redefine')) { |
||
| 56 | View Code Duplication | if (!empty($this->nodes[$model])) { |
|
| 57 | $this->checkError($model . ": forced to redefine (in file " . $this->nodes[$model]['filename'] . ", redefined from file ".$file.")"); |
||
| 58 | } |
||
| 59 | $i = $this->getResourceParams($i); |
||
| 60 | $this->nodes[$model] = $i; |
||
| 61 | View Code Duplication | } else { |
|
| 62 | $this->checkError($model . ": already defined (in file " . $this->nodes[$model]['filename'] . ", trying to redefine from file ".$file."; Use --force-redefine to force redefinition)"); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | $this->line(''); |
||
| 68 | $this->info('Bringing models to order...'); |
||
| 69 | |||
| 70 | $this->nodes = $this->sortDependencies(); |
||
| 71 | |||
| 72 | if (! $this->option('skip-check')) { |
||
| 73 | $this->info('Checking Relationships...'); |
||
| 74 | $keys = array_keys($this->nodes); |
||
| 75 | foreach ($this->nodes as $model => $i) { |
||
| 76 | $this->checkRelations($i['belongsTo'], 'belongsTo', $i['filename'], $i['uniquename'], $keys); |
||
| 77 | // $this->checkRelations($i['hasManyThrough'], 'hasManyThrough', $file, $model); |
||
| 78 | } |
||
| 79 | $this->checkPivotRelations($this->pivotTables, 'pivot'); |
||
| 80 | $this->checkPivotRelations($this->morphTables, 'morph'); |
||
| 81 | } |
||
| 82 | |||
| 83 | View Code Duplication | if ($this->checkedErrors > 0) { |
|
| 84 | $this->line(''); |
||
| 85 | if ($this->option('check-only')) { |
||
| 86 | $this->info('Checking only, we have found ' . $this->checkedErrors . ' errors.'); |
||
| 87 | } |
||
| 88 | $this->printErrors(); |
||
| 89 | } |
||
| 90 | |||
| 91 | $proceed = (! $this->option('check-only') && $this->checkedErrors == 0) || $this->option('skip-check'); |
||
| 92 | View Code Duplication | if (! $this->option('check-only') && $this->checkedErrors > 0) { |
|
| 93 | $this->line(''); |
||
| 94 | $proceed = $this->confirm("We have found " . $this->checkedErrors . " errors. Are you sure you want to continue?"); |
||
| 95 | } |
||
| 96 | if ($proceed) { |
||
| 97 | $modelIndex = 0; |
||
| 98 | $migrationIdLength = strlen((string)count($this->nodes)); |
||
| 99 | foreach ($this->nodes as $i) { |
||
| 100 | $migrationName = 'Create' . ucwords(str_plural($i['name'])); |
||
| 101 | $migrationFile = date('Y_m_d_His') . '-' . str_pad($modelIndex , $migrationIdLength, 0, STR_PAD_LEFT) . '_' . snake_case($migrationName) . '_table'; |
||
| 102 | |||
| 103 | $this->line(''); |
||
| 104 | $this->info('Building Model ' . $i['uniquename']); |
||
| 105 | |||
| 106 | $options = [ |
||
| 107 | 'name' => $i['name'], |
||
| 108 | 'fields' => $i['fields'], |
||
| 109 | '--add' => $i['add'], |
||
| 110 | '--has-many' => $i['hasMany'], |
||
| 111 | '--has-one' => $i['hasOne'], |
||
| 112 | '--belongs-to' => $i['belongsTo'], |
||
| 113 | '--belongs-to-many' => $i['belongsToMany'], |
||
| 114 | '--has-many-through' => $i['hasManyThrough'], |
||
| 115 | '--morph-to' => $i['morphTo'], |
||
| 116 | '--morph-many' => $i['morphMany'], |
||
| 117 | '--morph-to-many' => $i['morphToMany'], |
||
| 118 | '--morphed-by-many' => $i['morphedByMany'], |
||
| 119 | '--no-routes' => $this->option('no-routes'), |
||
| 120 | '--no-controller' => $this->option('no-controllers'), |
||
| 121 | '--force' => $this->option('force'), |
||
| 122 | '--migration-file' => $migrationFile, |
||
| 123 | ]; |
||
| 124 | if ($this->option('laravel')) { |
||
| 125 | $options['--laravel'] = true; |
||
| 126 | } |
||
| 127 | if ($this->option('routes')) { |
||
| 128 | $options['--routes'] = $this->option('routes'); |
||
| 129 | } |
||
| 130 | if ($this->option('controllers')) { |
||
| 131 | $options['--controller'] = $this->option('controllers'); |
||
| 132 | } |
||
| 133 | if ($this->option('path')) { |
||
| 134 | $options['--path'] = $this->option('path'); |
||
| 135 | } |
||
| 136 | |||
| 137 | $this->call('wn:resource', $options); |
||
| 138 | $modelIndex++; |
||
| 139 | } |
||
| 140 | |||
| 141 | // if (!$this->option('no-migration')) { |
||
| 142 | // $this->call('migrate'); // actually needed for pivot seeders ! |
||
| 143 | // } |
||
| 144 | |||
| 145 | $this->pivotTables = array_map( |
||
| 146 | 'unserialize', |
||
| 147 | array_unique(array_map('serialize', $this->pivotTables)) |
||
| 148 | ); |
||
| 149 | |||
| 150 | $this->line(''); |
||
| 151 | View Code Duplication | foreach ($this->pivotTables as $tables) { |
|
| 152 | $this->info('Building Pivot-Table ' . $tables[0] . ' - ' . $tables[1]); |
||
| 153 | $this->call('wn:pivot-table', [ |
||
| 154 | 'model1' => $tables[0], |
||
| 155 | 'model2' => $tables[1], |
||
| 156 | '--force' => $this->option('force') |
||
| 157 | ]); |
||
| 158 | |||
| 159 | // $this->call('wn:pivot-seeder', [ |
||
| 160 | // 'model1' => $tables[0], |
||
| 161 | // 'model2' => $tables[1], |
||
| 162 | // '--force' => $this->option('force') |
||
| 163 | // ]); |
||
| 164 | } |
||
| 165 | |||
| 166 | $this->morphTables = array_map( |
||
| 167 | 'unserialize', |
||
| 168 | array_unique(array_map('serialize', $this->morphTables)) |
||
| 169 | ); |
||
| 170 | |||
| 171 | $this->line(''); |
||
| 172 | View Code Duplication | foreach ($this->morphTables as $tables) { |
|
| 173 | $this->info('Building Morph-Table ' . $tables[0] . ' - ' . $tables[1]); |
||
| 174 | $this->call('wn:morph-table', [ |
||
| 175 | 'model' => $tables[0], |
||
| 176 | 'morphable' => $tables[1], |
||
| 177 | '--force' => $this->option('force') |
||
| 178 | ]); |
||
| 179 | |||
| 180 | // $this->call('wn:pivot-seeder', [ |
||
| 181 | // 'model1' => $tables[0], |
||
| 182 | // 'model2' => $tables[1], |
||
| 183 | // '--force' => $this->option('force') |
||
| 184 | // ]); |
||
| 185 | } |
||
| 186 | |||
| 187 | if (!$this->option('no-migration')) { |
||
| 188 | $this->call('migrate'); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 425 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
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:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.