Completed
Push — master ( 1f8bb9...0c53f4 )
by Phecho
06:42 queued 03:18
created

ExploreController::index()   C

Complexity

Conditions 9
Paths 60

Size

Total Lines 68
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 68
rs 6.2813
cc 9
eloc 41
nc 60
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B ExploreController::__construct() 0 28 1

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 * 
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gitamin\Http\Controllers;
13
14
use Exception;
15
use Gitamin\Facades\Setting;
16
use Gitamin\Models\Issue;
17
use GrahamCampbell\Binput\Facades\Binput;
18
use Illuminate\Routing\Controller;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Gitamin\Http\Controllers\Controller.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
19
use Illuminate\Support\Facades\Auth;
20
use Illuminate\Support\Facades\View;
21
use Jenssegers\Date\Date;
22
23
class ExploreController extends Controller
24
{
25
    /**
26
     * Array of sub-menu items.
27
     *
28
     * @var array
29
     */
30
    protected $subMenu = [];
31
32
    /**
33
     * Creates a new project controller instance.
34
     *
35
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
36
     */
37
    public function __construct()
38
    {
39
        $this->subMenu = [
40
            'yours' => [
41
                'title'  => trans('dashboard.projects.yours'),
42
                'url'    => route('dashboard.projects.index'),
43
                'icon'   => 'fa fa-edit',
44
                'active' => false,
45
            ],
46
            'starred' => [
47
                'title'  => trans('dashboard.projects.starred'),
48
                'url'    => route('dashboard.projects.starred'),
49
                'icon'   => 'fa fa-umbrella',
50
                'active' => false,
51
            ],
52
            'explore' => [
53
                'title'  => trans('dashboard.projects.explore'),
54
                'url'    => route('explore.index'),
55
                'icon'   => 'fa fa-eye',
56
                'active' => false,
57
            ],
58
        ];
59
60
        View::share([
61
            'sub_menu'  => $this->subMenu,
62
            'sub_title' => trans_choice('dashboard.projects.projects', 2),
63
        ]);
64
    }
65
66
    /**
67
     * Displays the explore page.
68
     *
69
     * @return \Illuminate\View\View
70
     */
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;
0 ignored issues
show
Unused Code introduced by
$issueVisiblity is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
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) {
0 ignored issues
show
Documentation introduced by
The property is_scheduled does not exist on object<Gitamin\Models\Issue>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
110
                return (new Date($issue->scheduled_at))
0 ignored issues
show
Documentation introduced by
The property scheduled_at does not exist on object<Gitamin\Models\Issue>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
111
                    ->setTimezone($dateTimeZone)->toDateString();
112
            }
113
114
            return (new Date($issue->created_at))
0 ignored issues
show
Documentation introduced by
The property created_at does not exist on object<Gitamin\Models\Issue>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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
144
    public function groupsAction()
145
    {
146
        $this->subMenu = [
147
            'yours' => [
148
                'title'  => trans('gitamin.groups.yours'),
149
                'url'    => route('dashboard.groups.index'),
150
                'icon'   => 'fa fa-edit',
151
                'active' => false,
152
            ],
153
            'explore' => [
154
                'title'  => trans('gitamin.groups.explore'),
155
                'url'    => route('explore.groups'),
156
                'icon'   => 'fa fa-eye',
157
                'active' => false,
158
            ],
159
        ];
160
161
        $this->subMenu['explore']['active'] = true;
162
163
        return View::make('explore.groups')
164
            ->withPageTitle(trans('dashboard.explore'))
165
            ->withSubMenu($this->subMenu)
166
            ->withProjects([]);
167
    }
168
169
    /**
170
     * Shows an issue in more detail.
171
     *
172
     * @param \Gitamin\Models\Issue $issue
173
     *
174
     * @return \Illuminate\View\View
175
     */
176
    public function showIssue(Issue $issue)
177
    {
178
        return View::make('issue')
179
            ->withIssue($issue);
180
    }
181
}
182