Conditions | 5 |
Paths | 9 |
Total Lines | 51 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 9 | ||
Bugs | 1 | Features | 2 |
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 |
||
41 | public function takeReservation(array $request) |
||
42 | { |
||
43 | $issuer = $request['issuer']; |
||
44 | $service = $request['service']; |
||
45 | $contact = $request['contact']; |
||
46 | $comments = $request['comments']; |
||
47 | |||
48 | $vacancies = $this->calendar() |
||
49 | ->forService($service->id) |
||
50 | ->forDate($request['date']) |
||
51 | ->atTime($request['time']) |
||
52 | ->find(); |
||
53 | |||
54 | if ($vacancies->count() == 0) { |
||
55 | // Log failure feedback message |
||
56 | return false; |
||
57 | } |
||
58 | |||
59 | if ($vacancies->count() > 1) { |
||
60 | // Log unexpected behavior message |
||
61 | $vacancy = $vacancies->first(); |
||
62 | } |
||
63 | |||
64 | if ($vacancies->count() == 1) { |
||
65 | $vacancy = $vacancies->first(); |
||
66 | } |
||
67 | |||
68 | $startAt = $this->makeDateTimeUTC($request['date'], $request['time'], $request['timezone']); |
||
69 | $finishAt = $startAt->copy()->addMinutes($service->duration); |
||
70 | |||
71 | $appointment = $this->generateAppointment( |
||
72 | $issuer, |
||
73 | $this->business->id, |
||
74 | $contact->id, |
||
75 | $service->id, |
||
76 | $startAt, |
||
77 | $finishAt, |
||
78 | $comments |
||
79 | ); |
||
80 | |||
81 | /* Should be moved inside generateAppointment() */ |
||
82 | if ($appointment->duplicates()) { |
||
83 | throw new DuplicatedAppointmentException; |
||
84 | } |
||
85 | |||
86 | /* Should be moved inside generateAppointment() */ |
||
87 | $appointment->vacancy()->associate($vacancy); |
||
|
|||
88 | $appointment->save(); |
||
89 | |||
90 | return $appointment; |
||
91 | } |
||
92 | |||
120 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: