GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AdminController::getReset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace LaravelFlare\Flare\Http\Controllers\LTS;
4
5
use Flare;
6
use Response;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Contracts\Auth\Guard;
10
use LaravelFlare\Flare\Admin\AdminManager;
11
use Illuminate\Foundation\Bus\DispatchesJobs;
12
use Illuminate\Foundation\Auth\RegistersUsers;
13
use LaravelFlare\Flare\Permissions\Permissions;
14
use Illuminate\Foundation\Auth\ThrottlesLogins;
15
use Illuminate\Foundation\Auth\ResetsPasswords;
16
use Illuminate\Foundation\Auth\AuthenticatesUsers;
17
use LaravelFlare\Flare\Http\Controllers\FlareController;
18
use LaravelFlare\Flare\Admin\Widgets\WidgetAdminManager;
19
20
class AdminController extends FlareController
21
{
22
    use AuthenticatesUsers;
23
    use ThrottlesLogins;
24
    use ResetsPasswords;
25
    use RegistersUsers;
26
    use DispatchesJobs;
27
28
    /**
29
     * Auth.
30
     * 
31
     * @var Guard
32
     */
33
    protected $auth;
34
35
    /**
36
     * __construct.
37
     * 
38
     * @param Guard        $auth
39
     * @param AdminManager $adminManager
40
     */
41
    public function __construct(Guard $auth, AdminManager $adminManager)
42
    {
43
        parent::__construct($adminManager);
44
45
        $this->auth = $auth;
46
    }
47
48
    /**
49
     * Show the Dashboard.
50
     * 
51
     * @return \Illuminate\Http\Response
52
     */
53 View Code Duplication
    public function getDashboard()
54
    {
55
        $view = 'admin.dashboard';
56
57
        if (!view()->exists($view)) {
0 ignored issues
show
Bug introduced by
The method exists does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
58
            $view = 'flare::'.$view;
59
        }
60
61
        return view($view, ['widgets' => (new WidgetAdminManager())]);
62
    }
63
64
    /**
65
     * Show the login form.
66
     *
67
     * @return \Illuminate\Http\Response
68
     */
69
    public function getLogin()
70
    {
71
        return view('flare::admin.login');
72
    }
73
74
    /**
75
     * Log the user.
76
     *
77
     * @return \Illuminate\Http\RedirectReponse
78
     */
79
    public function getLogout()
80
    {
81
        $this->auth->logout();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Guard as the method logout() does only exist in the following implementations of said interface: Illuminate\Auth\SessionGuard.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
82
83
        return redirect('/');
84
    }
85
86
    /**
87
     * Display the form to request a password reset link.
88
     *
89
     * @return \Illuminate\Http\Response
90
     */
91
    public function getEmail()
92
    {
93
        return view('flare::admin.password');
94
    }
95
96
    /**
97
     * Display the form to request a password reset link.
98
     *
99
     * @return \Illuminate\Http\Response
100
     */
101
    public function getReset()
102
    {
103
        return view('flare::admin.reset');
104
    }
105
106
    /**
107
     * Performs the login redirect action.
108
     *
109
     * If the authenticated user has admin permissions
110
     * then they will be redirected into the admin
111
     * panel. If they do no, they will be sent
112
     * to the homepage of the website.
113
     * 
114
     * @return \Illuminate\Http\RedirectReponse
115
     */
116
    public function redirectPath()
117
    {
118
        if (Permissions::check()) {
119
            return Flare::adminUrl();
120
        }
121
122
        return '/';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return '/'; (string) is incompatible with the return type documented by LaravelFlare\Flare\Http\...ontroller::redirectPath of type Illuminate\Http\RedirectReponse.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
123
    }
124
125
    /**
126
     * Get the path to the login route.
127
     *
128
     * @return string
129
     */
130
    public function loginPath()
131
    {
132
        return Flare::adminUrl('login');
133
    }
134
135
    /**
136
     * Method is called when the appropriate controller
137
     * method is unable to be found or called.
138
     * 
139
     * @param array $parameters
140
     * 
141
     * @return \Illuminate\Http\Response
142
     */
143
    public function missingMethod($parameters = array())
144
    {
145
        return Response::view('flare::admin.404', [], 404);
146
    }
147
}
148