| Conditions | 7 |
| Paths | 18 |
| Total Lines | 51 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 2 | Features | 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 |
||
| 42 | public function parseImport($source, Request $request) |
||
| 43 | { |
||
| 44 | $request->validate([ |
||
| 45 | 'csv_file' => 'required|file|mimes:csv,txt', |
||
| 46 | ]); |
||
| 47 | |||
| 48 | $path = $request->file('csv_file')->getRealPath(); |
||
| 49 | |||
| 50 | $data = array_map('str_getcsv', file($path)); |
||
|
|
|||
| 51 | |||
| 52 | if ($request->has('header')) { |
||
| 53 | array_shift($data); |
||
| 54 | } |
||
| 55 | |||
| 56 | array_shift($data); |
||
| 57 | |||
| 58 | Storage::disk('local')->put($this->saved_file, json_encode($data)); |
||
| 59 | |||
| 60 | $db_cols = $this->getDbCols($source); |
||
| 61 | |||
| 62 | if (is_null($db_cols)) { |
||
| 63 | return response()->json(['message'=>'No Such Model Found in App'], 500); |
||
| 64 | } |
||
| 65 | |||
| 66 | $relations = $this->model->relationships(); |
||
| 67 | |||
| 68 | $relationship_array = []; |
||
| 69 | |||
| 70 | foreach ($relations as $key => $value) { |
||
| 71 | $model = new $value['model']; |
||
| 72 | |||
| 73 | if (! empty($model->getFillable())) { |
||
| 74 | if (empty($model->getHidden())) { |
||
| 75 | $relationship_array[] = implode(' ', array_values(array_unique(array_map(function ($v) use ($key) { |
||
| 76 | return $key.'.'.$v; |
||
| 77 | }, $model->getFillable())))); |
||
| 78 | } else { |
||
| 79 | $relationship_array[] = implode(' ', array_values(array_unique(array_merge(array_map(function ($v) use ($key) { |
||
| 80 | return $key.'.'.$v; |
||
| 81 | }, $model->getFillable()), array_map(function ($v) use ($key) { |
||
| 82 | return $key.'.'.$v; |
||
| 83 | }, $model->getHidden()))))); |
||
| 84 | } |
||
| 85 | } //not a fillable model |
||
| 86 | } |
||
| 87 | |||
| 88 | $returnArray['csv_sample_row'] = ($data[0]); |
||
| 89 | $returnArray['database_columns'] = (! empty($relationship_array)) ? array_merge(array_unique($db_cols), $relationship_array) : array_unique($db_cols); |
||
| 90 | $returnArray['relations'] = $relations; |
||
| 91 | |||
| 92 | return response()->json($returnArray); |
||
| 93 | } |
||
| 109 |