Completed
Push — master ( 00c1fc...304a6e )
by Phecho
04:19
created

ExploreController::index()   C

Complexity

Conditions 9
Paths 60

Size

Total Lines 67
Code Lines 40

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 67
rs 6.3449
cc 9
eloc 40
nc 60
nop 0

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
     * Displays the explore page.
27
     *
28
     * @return \Illuminate\View\View
29
     */
30
    public function index()
31
    {
32
        $today = Date::now();
33
        $startDate = Date::now();
34
35
        // Check if we have another starting date
36
        if (Binput::has('start_date')) {
37
            try {
38
                // If date provided is valid
39
                $oldDate = Date::createFromFormat('Y-m-d', Binput::get('start_date'));
40
41
                // If trying to get a future date fallback to today
42
                if ($today->gt($oldDate)) {
43
                    $startDate = $oldDate;
44
                }
45
            } catch (Exception $e) {
46
                // Fallback to today
47
            }
48
        }
49
50
        $daysToShow = Setting::get('app_issue_days', 0) - 1;
51
        if ($daysToShow < 0) {
52
            $daysToShow = 0;
53
            $issueDays = [];
54
        } else {
55
            $issueDays = range(0, $daysToShow);
56
        }
57
        $dateTimeZone = Setting::get('app_timezone');
58
59
        $issueVisiblity = Auth::check() ? 0 : 1;
60
61
        $allIssues = Issue::where('visible', '>=', $issueVisiblity)->whereBetween('created_at', [
62
            $startDate->copy()->subDays($daysToShow)->format('Y-m-d').' 00:00:00',
63
            $startDate->format('Y-m-d').' 23:59:59',
64
        ])->orderBy('scheduled_at', 'desc')->orderBy('created_at', 'desc')->get()->groupBy(function (Issue $issue) use ($dateTimeZone) {
65
            // If it's scheduled, get the scheduled at date.
66
            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...
67
                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...
68
                    ->setTimezone($dateTimeZone)->toDateString();
69
            }
70
71
            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...
72
                ->setTimezone($dateTimeZone)->toDateString();
73
        });
74
75
        // Add in days that have no issues
76
        foreach ($issueDays as $i) {
77
            $date = (new Date($startDate))->setTimezone($dateTimeZone)->subDays($i);
78
79
            if (!isset($allIssues[$date->toDateString()])) {
80
                $allIssues[$date->toDateString()] = [];
81
            }
82
        }
83
84
        // Sort the array so it takes into account the added days
85
        $allIssues = $allIssues->sortBy(function ($value, $key) {
86
            return strtotime($key);
87
        }, SORT_REGULAR, true)->all();
88
89
        return View::make('index')
90
            ->withDaysToShow($daysToShow)
91
            ->withAllIssues($allIssues)
92
            ->withCanPageForward((bool) $today->gt($startDate))
93
            ->withCanPageBackward(Issue::where('created_at', '<', $startDate->format('Y-m-d'))->count() > 0)
94
            ->withPreviousDate($startDate->copy()->subDays($daysToShow)->toDateString())
95
            ->withNextDate($startDate->copy()->addDays($daysToShow)->toDateString());
96
    }
97
98
    /**
99
     * Shows an issue in more detail.
100
     *
101
     * @param \Gitamin\Models\Issue $issue
102
     *
103
     * @return \Illuminate\View\View
104
     */
105
    public function showIssue(Issue $issue)
106
    {
107
        return View::make('issue')
108
            ->withIssue($issue);
109
    }
110
}
111