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.
Completed
Push — master ( 397d83...af0ff8 )
by Aden
03:32
created

AdminController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 11
c 4
b 1
f 0
lcom 1
cbo 8
dl 0
loc 128
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getDashboard() 0 10 2
A getLogin() 0 4 1
A postLogin() 0 16 2
A getLogout() 0 6 1
A getReset() 0 4 1
A loginRedirect() 0 8 2
A missingMethod() 0 4 1
1
<?php
2
3
namespace LaravelFlare\Flare\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Contracts\Auth\Guard;
7
use LaravelFlare\Flare\Admin\AdminManager;
8
use Illuminate\Foundation\Bus\DispatchesJobs;
9
use LaravelFlare\Flare\Permissions\Permissions;
10
use LaravelFlare\Flare\Admin\Widgets\WidgetAdminManager;
11
use LaravelFlare\Flare\Traits\Http\Controllers\AuthenticatesAndResetsPasswords;
12
13
class AdminController extends FlareController
14
{
15
    use AuthenticatesAndResetsPasswords, DispatchesJobs;
16
17
    /**
18
     * Auth.
19
     * 
20
     * @var Guard
21
     */
22
    protected $auth;
23
24
    /**
25
     * __construct.
26
     * 
27
     * @param Guard        $auth
28
     * @param AdminManager $adminManager
29
     */
30
    public function __construct(Guard $auth, AdminManager $adminManager)
31
    {
32
        parent::__construct($adminManager);
33
34
        $this->auth = $auth;
35
    }
36
37
    /**
38
     * Show the Dashboard.
39
     * 
40
     * @return \Illuminate\Http\Response
41
     */
42
    public function getDashboard()
43
    {
44
        $view = 'admin.dashboard';
45
46
        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...
47
            $view = 'flare::'.$view;
48
        }
49
50
        return view($view, ['widgetAdminManager' => (new WidgetAdminManager())]);
51
    }
52
53
    /**
54
     * Show the login form.
55
     *
56
     * @return \Illuminate\Http\Response
57
     */
58
    public function getLogin()
59
    {
60
        return view('flare::admin.login');
61
    }
62
63
    /**
64
     * Processes the login form.
65
     *
66
     * @param Request $request
67
     *
68
     * @return \Illuminate\Http\RedirectResponse
69
     */
70
    public function postLogin(Request $request)
71
    {
72
        $this->validate($request, ['email' => 'required|email', 'password' => 'required']);
73
74
        $credentials = $request->only('email', 'password');
75
76
        if ($this->auth->attempt($credentials, $request->has('remember'))) {
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 attempt() 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...
77
            return $this->loginRedirect();
78
        }
79
80
        return redirect(url('admin/login'))
0 ignored issues
show
Bug introduced by
It seems like url('admin/login') targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, redirect() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
81
                    ->withInput($request->only('email', 'remember'))
82
                    ->withErrors([
83
                        'email' => $this->getFailedLoginMessage(),
84
                    ]);
85
    }
86
87
    /**
88
     * Log the user.
89
     *
90
     * @return \Illuminate\Http\RedirectReponse
91
     */
92
    public function getLogout()
93
    {
94
        $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...
95
96
        return redirect('/');
97
    }
98
99
    /**
100
     * Display the form to request a password reset link.
101
     *
102
     * @return \Illuminate\Http\Response
103
     */
104
    public function getReset()
105
    {
106
        return view('flare::admin.password');
107
    }
108
109
    /**
110
     * Performs the login redirect action.
111
     *
112
     * If the authenticated user has admin permissions
113
     * then they will be redirected into the admin
114
     * panel.If they do no, they will be sent
115
     * to the homepage of the website.
116
     * 
117
     * @return \Illuminate\Http\RedirectReponse
118
     */
119
    protected function loginRedirect()
120
    {
121
        if (Permissions::check()) {
122
            return redirect()->intended(\Flare::adminUrl());
123
        }
124
125
        return redirect('/');
126
    }
127
128
    /**
129
     * Method is called when the appropriate controller
130
     * method is unable to be found or called.
131
     * 
132
     * @param array $parameters
133
     * 
134
     * @return \Illuminate\Http\Response
135
     */
136
    public function missingMethod($parameters = array())
137
    {
138
        return view('flare::admin.404', []);
139
    }
140
}
141