Conditions | 4 |
Paths | 4 |
Total Lines | 59 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
15 | public function adAnnounce(Request $request){ |
||
16 | |||
17 | $data = [ |
||
18 | "image" => Input::file('image'), |
||
19 | "name" => $request->name, |
||
20 | "email" => $request->email, |
||
21 | "phone" => $request->phone, |
||
22 | "description" => $request->description, |
||
23 | "chambres" => $request->chambres, |
||
24 | "pieces" => $request->pieces, |
||
25 | "surface" => $request->surface, |
||
26 | "prix" => $request->prix, |
||
27 | "aid" => $request->aid |
||
28 | ]; |
||
29 | |||
30 | $file = null; |
||
|
|||
31 | $data["email"] = $request->email; |
||
1 ignored issue
–
show
|
|||
32 | |||
33 | $input = array('image' => Input::file('image')); |
||
34 | |||
35 | $rules = array( |
||
36 | 'image' => 'image' |
||
37 | ); |
||
38 | |||
39 | $validator = Validator::make($input, $rules); |
||
40 | |||
41 | if ($validator->fails()) |
||
42 | { |
||
43 | return response()->json(['data' => 'Fichier invalid', 'state' => false]); |
||
44 | } else { |
||
45 | if ($request->hasFile('image')) { |
||
46 | |||
47 | $manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017'); |
||
48 | $collection = new \MongoDB\Collection($manager, 'builders', 'ads'); |
||
49 | $stat = [ |
||
50 | 'email' => $request->email, |
||
51 | 'data' => $data, |
||
52 | 'created' => new \DateTime("now"), |
||
53 | ]; |
||
54 | |||
55 | try{ |
||
56 | $collection->insertOne($stat); |
||
57 | } catch (\Exception $e){ |
||
58 | return response()->json(['state' => false]); |
||
59 | } |
||
60 | |||
61 | $file = $request->file('image'); |
||
62 | $filename = $file->getClientOriginalName(); |
||
63 | $destinationPath = public_path().'/uploads/ad'; |
||
64 | $file->move($destinationPath, $filename); |
||
65 | |||
66 | $data['image'] = asset($filename); |
||
67 | return response()->json(['data' => $data, 'state' => true]); |
||
68 | |||
69 | } |
||
70 | } |
||
71 | |||
72 | |||
73 | } |
||
74 | |||
97 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.