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