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