Conditions | 9 |
Paths | 24 |
Total Lines | 55 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
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 |
||
19 | public function import(Request $request) |
||
20 | { |
||
21 | $request->validate([ |
||
22 | 'file' => ['required', 'file', 'mimes:xml'], |
||
23 | ]); |
||
24 | |||
25 | // save file as temp and convert to json |
||
26 | $res = $this->fileToJson($request); |
||
27 | |||
28 | $fileName = $res[0]; |
||
29 | $families = $res[1]['family']; |
||
30 | |||
31 | if (array_key_exists('husband', $families) || array_key_exists('wife', $families)) { |
||
32 | $families = [$families]; |
||
33 | } |
||
34 | |||
35 | $errors = []; |
||
36 | foreach ($families as $key => $family) { |
||
37 | // Validate family |
||
38 | $error = $this->validateFamily($family); |
||
39 | |||
40 | if ((is_countable($error) ? count($error) : 0) > 0) { |
||
41 | $errors['Family-'.$key + 1] = $error; |
||
42 | continue; |
||
43 | } |
||
44 | |||
45 | $description = $family['description']; |
||
46 | |||
47 | $husband = $this->createPerson($family['husband'], null, 'M'); |
||
48 | $wife = $this->createPerson($family['wife'], null, 'F'); |
||
49 | |||
50 | $fam = Family::where('husband_id', $husband->id)->where('wife_id', $wife->id)->first(); |
||
51 | |||
52 | if (! $fam) { |
||
53 | $fam = Family::create([ |
||
54 | 'description' => $description, |
||
55 | 'husband_id' => $husband->id, |
||
56 | 'wife_id' => $wife->id, |
||
57 | ]); |
||
58 | } |
||
59 | |||
60 | foreach ($family['child'] as $child) { |
||
61 | $this->createPerson($child, $fam->id); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | // remove temp file after importing |
||
66 | Storage::delete('files/temp/'.$fileName); |
||
67 | |||
68 | if ($errors !== []) { |
||
69 | return ['errors' => $errors]; |
||
70 | } |
||
71 | |||
72 | return json_encode([ |
||
73 | 'message' => 'File imported successfully', |
||
74 | ]); |
||
130 |