Conditions | 11 |
Paths | 55 |
Total Lines | 62 |
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 |
||
15 | public function store(StoreEntryRequest $request): JsonResponse |
||
16 | { |
||
17 | $recordingId = null; |
||
18 | if ($request->has('recording')) { |
||
19 | $recordingId = (int)decrypt($request->get('recording')); |
||
20 | |||
21 | if (SessionRecording::whereId($recordingId)->count() === 0) { |
||
22 | throw new NotAcceptableHttpException(); |
||
23 | } |
||
24 | } |
||
25 | |||
26 | if (config('laravel-spyhole.track_request_session_id')) { |
||
27 | $sessionId = $request->session()->getId(); |
||
|
|||
28 | } else { |
||
29 | if (session()->has('spyhole_session_id')) { |
||
30 | $sessionId = session()->get('spyhole_session_id'); |
||
31 | } else { |
||
32 | do { |
||
33 | $sessionId = Str::uuid()->toString(); |
||
34 | } while ( |
||
35 | SessionRecording::whereSessionId($sessionId)->count() > 0 && |
||
36 | $sessionId !== $request->session()->getId() |
||
37 | ); |
||
38 | session()->put('spyhole_session_id', $sessionId); |
||
39 | } |
||
40 | } |
||
41 | |||
42 | $userId = null; |
||
43 | if (config('laravel-spyhole.record_user_id')) { |
||
44 | $user = Auth::user(); |
||
45 | $userId = $user ? $user->getAuthIdentifier() : null; |
||
46 | } |
||
47 | |||
48 | if ($recordingId === null) { |
||
49 | $recording = new SessionRecording(); |
||
50 | $recording->session_id = $sessionId; |
||
51 | $recording->user_id = $userId; |
||
52 | $recording->path = $request->get('path'); |
||
53 | $recording->recordings = $request->get('frames'); |
||
54 | } else { |
||
55 | $recording = SessionRecording::wherePath($request->get('path')) |
||
56 | ->whereId($recordingId) |
||
57 | ->first(); |
||
58 | |||
59 | if ($recording === null) { |
||
60 | throw new NotAcceptableHttpException(); |
||
61 | } |
||
62 | |||
63 | // Merge frames from the same session |
||
64 | $recording->recordings = array_merge( |
||
65 | $recording->recordings, |
||
66 | $request->get('frames') |
||
67 | ); |
||
68 | } |
||
69 | |||
70 | $recording->save(); |
||
71 | |||
72 | return response()->json([ |
||
73 | 'success' => true, |
||
74 | 'recording' => encrypt($recording->id), |
||
75 | ]); |
||
76 | } |
||
77 | } |
||
78 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.