Completed
Pull Request — master (#4)
by
unknown
01:48
created

ImpersonateController::take()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 28
rs 6.7272
cc 7
eloc 14
nc 32
nop 2
1
<?php
2
3
namespace Lab404\Impersonate\Controllers;
4
5
use Illuminate\Http\RedirectResponse;
6
use Illuminate\Http\Request;
7
use Illuminate\Routing\Controller;
8
use Lab404\Impersonate\Services\ImpersonateManager;
9
10
class ImpersonateController extends Controller
11
{
12
    /** @var ImpersonateManager */
13
    protected $manager;
14
15
    /**
16
     * ImpersonateController constructor.
17
     */
18
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
22
        $this->manager = app()->make(ImpersonateManager::class);
23
    }
24
25
    /**
26
     * @param   int $id
27
     * @return  RedirectResponse
28
     */
29
    public function take(Request $request, $id)
30
    {
31
        // Cannot impersonate yourself
32
        if ($id == $request->user()->id) {
33
            abort(403);
34
        }
35
36
        // Cannot impersonate again if you're already impersonate a user
37
        if ($this->manager->isImpersonating()) {
38
            abort(403);
39
        }
40
41
        if (!$request->user()->canImpersonate()) {
42
            abort(403);
43
        }
44
45
        if (!$this->manager->findUserById($id)) {
46
             abort(403);
47
        } else {
48
            $user_to_impersonate = $this->manager->findUserById($id);
49
        }
50
51
        if ($user_to_impersonate->canBeImpersonated() && $this->manager->take($request->user(), $user_to_impersonate)) {
0 ignored issues
show
Bug introduced by
The variable $user_to_impersonate does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
52
            return redirect()->to($this->manager->getTakeRedirectTo());
53
        }
54
55
        return redirect()->back();
56
    }
57
58
    /*
59
     * @return RedirectResponse
60
     */
61
    public function leave()
62
    {
63
        if (!$this->manager->isImpersonating()) {
64
            abort(403);
65
        }
66
67
        $this->manager->leave();
68
69
        return redirect()->to($this->manager->getLeaveRedirectTo());
70
    }
71
}
72