Conditions | 8 |
Paths | 60 |
Total Lines | 66 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
68 | public function indexAction() |
||
69 | { |
||
70 | $this->subMenu['explore']['active'] = true; |
||
71 | |||
72 | $today = Date::now(); |
||
73 | $startDate = Date::now(); |
||
74 | |||
75 | // Check if we have another starting date |
||
76 | if (Request::has('start_date')) { |
||
77 | try { |
||
78 | // If date provided is valid |
||
79 | $oldDate = Date::createFromFormat('Y-m-d', Request::get('start_date')); |
||
80 | |||
81 | // If trying to get a future date fallback to today |
||
82 | if ($today->gt($oldDate)) { |
||
83 | $startDate = $oldDate; |
||
84 | } |
||
85 | } catch (Exception $e) { |
||
86 | // Fallback to today |
||
87 | } |
||
88 | } |
||
89 | |||
90 | $daysToShow = Setting::get('app_issue_days', 0) - 1; |
||
91 | if ($daysToShow < 0) { |
||
92 | $daysToShow = 0; |
||
93 | $issueDays = []; |
||
94 | } else { |
||
95 | $issueDays = range(0, $daysToShow); |
||
96 | } |
||
97 | $dateTimeZone = Setting::get('app_timezone'); |
||
98 | |||
99 | $issueVisiblity = Auth::check() ? 0 : 1; |
||
100 | |||
101 | $allIssues = Issue::whereBetween('created_at', [ |
||
102 | $startDate->copy()->subDays($daysToShow)->format('Y-m-d').' 00:00:00', |
||
103 | $startDate->format('Y-m-d').' 23:59:59', |
||
104 | ])->orderBy('created_at', 'desc')->get()->groupBy(function (Issue $issue) use ($dateTimeZone) { |
||
105 | return (new Date($issue->created_at)) |
||
106 | ->setTimezone($dateTimeZone)->toDateString(); |
||
107 | }); |
||
108 | |||
109 | // Add in days that have no issues |
||
110 | foreach ($issueDays as $i) { |
||
111 | $date = (new Date($startDate))->setTimezone($dateTimeZone)->subDays($i); |
||
112 | |||
113 | if (! isset($allIssues[$date->toDateString()])) { |
||
114 | $allIssues[$date->toDateString()] = []; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | // Sort the array so it takes into account the added days |
||
119 | $allIssues = $allIssues->sortBy(function ($value, $key) { |
||
120 | return strtotime($key); |
||
121 | }, SORT_REGULAR, true)->all(); |
||
122 | |||
123 | return View::make('explore.index') |
||
124 | ->withPageTitle(trans('dashboard.explore')) |
||
125 | ->withProjects([]) |
||
126 | ->withSubMenu($this->subMenu) |
||
127 | ->withDaysToShow($daysToShow) |
||
128 | ->withAllIssues($allIssues) |
||
129 | ->withCanPageForward((bool) $today->gt($startDate)) |
||
130 | ->withCanPageBackward(Issue::where('created_at', '<', $startDate->format('Y-m-d'))->count() > 0) |
||
131 | ->withPreviousDate($startDate->copy()->subDays($daysToShow)->toDateString()) |
||
132 | ->withNextDate($startDate->copy()->addDays($daysToShow)->toDateString()); |
||
133 | } |
||
134 | |||
174 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.