| Conditions | 9 |
| Paths | 60 |
| Total Lines | 72 |
| Code Lines | 44 |
| 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 |
||
| 71 | public function indexAction() |
||
| 72 | { |
||
| 73 | $this->subMenu['explore']['active'] = true; |
||
| 74 | |||
| 75 | $today = Date::now(); |
||
| 76 | $startDate = Date::now(); |
||
| 77 | |||
| 78 | // Check if we have another starting date |
||
| 79 | if (Binput::has('start_date')) { |
||
| 80 | try { |
||
| 81 | // If date provided is valid |
||
| 82 | $oldDate = Date::createFromFormat('Y-m-d', Binput::get('start_date')); |
||
| 83 | |||
| 84 | // If trying to get a future date fallback to today |
||
| 85 | if ($today->gt($oldDate)) { |
||
| 86 | $startDate = $oldDate; |
||
| 87 | } |
||
| 88 | } catch (Exception $e) { |
||
| 89 | // Fallback to today |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | $daysToShow = Setting::get('app_issue_days', 0) - 1; |
||
| 94 | if ($daysToShow < 0) { |
||
| 95 | $daysToShow = 0; |
||
| 96 | $issueDays = []; |
||
| 97 | } else { |
||
| 98 | $issueDays = range(0, $daysToShow); |
||
| 99 | } |
||
| 100 | $dateTimeZone = Setting::get('app_timezone'); |
||
| 101 | |||
| 102 | $issueVisiblity = Auth::check() ? 0 : 1; |
||
| 103 | |||
| 104 | $allIssues = Issue::whereBetween('created_at', [ |
||
| 105 | $startDate->copy()->subDays($daysToShow)->format('Y-m-d').' 00:00:00', |
||
| 106 | $startDate->format('Y-m-d').' 23:59:59', |
||
| 107 | ])->orderBy('created_at', 'desc')->get()->groupBy(function (Issue $issue) use ($dateTimeZone) { |
||
| 108 | // If it's scheduled, get the scheduled at date. |
||
| 109 | if ($issue->is_scheduled) { |
||
| 110 | return (new Date($issue->scheduled_at)) |
||
| 111 | ->setTimezone($dateTimeZone)->toDateString(); |
||
| 112 | } |
||
| 113 | |||
| 114 | return (new Date($issue->created_at)) |
||
| 115 | ->setTimezone($dateTimeZone)->toDateString(); |
||
| 116 | }); |
||
| 117 | |||
| 118 | // Add in days that have no issues |
||
| 119 | foreach ($issueDays as $i) { |
||
| 120 | $date = (new Date($startDate))->setTimezone($dateTimeZone)->subDays($i); |
||
| 121 | |||
| 122 | if (!isset($allIssues[$date->toDateString()])) { |
||
| 123 | $allIssues[$date->toDateString()] = []; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | // Sort the array so it takes into account the added days |
||
| 128 | $allIssues = $allIssues->sortBy(function ($value, $key) { |
||
| 129 | return strtotime($key); |
||
| 130 | }, SORT_REGULAR, true)->all(); |
||
| 131 | |||
| 132 | return View::make('explore.index') |
||
| 133 | ->withPageTitle(trans('dashboard.explore')) |
||
| 134 | ->withProjects([]) |
||
| 135 | ->withSubMenu($this->subMenu) |
||
| 136 | ->withDaysToShow($daysToShow) |
||
| 137 | ->withAllIssues($allIssues) |
||
| 138 | ->withCanPageForward((bool) $today->gt($startDate)) |
||
| 139 | ->withCanPageBackward(Issue::where('created_at', '<', $startDate->format('Y-m-d'))->count() > 0) |
||
| 140 | ->withPreviousDate($startDate->copy()->subDays($daysToShow)->toDateString()) |
||
| 141 | ->withNextDate($startDate->copy()->addDays($daysToShow)->toDateString()); |
||
| 142 | } |
||
| 143 | |||
| 182 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: