ResponseGuestIfAuthenticated::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Laravel Platfourm package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\Platfourm\Auth\Middleware;
12
13
use Closure;
14
use Longman\Platfourm\Contracts\Auth\AuthUserService as AuthUserServiceContract;
15
16
class ResponseGuestIfAuthenticated
17
{
18
    /**
19
     * The Guard implementation.
20
     *
21
     * @var Guard
22
     */
23
    protected $authService;
24
25
    /**
26
     * Create a new filter instance.
27
     *
28
     * @param  Guard $auth
0 ignored issues
show
Bug introduced by
There is no parameter named $auth. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
29
     * @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...
30
     */
31
    public function __construct(AuthUserServiceContract $authService)
32
    {
33
        $this->authService = $authService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $authService of type object<Longman\Platfourm...s\Auth\AuthUserService> is incompatible with the declared type object<Longman\Platfourm\Auth\Middleware\Guard> of property $authService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
    }
35
36
    /**
37
     * Handle an incoming request.
38
     *
39
     * @param  \Illuminate\Http\Request $request
40
     * @param  \Closure                 $next
41
     * @return mixed
42
     */
43
    public function handle($request, Closure $next)
44
    {
45
46
        if ($this->authService->check()) {
47
            if ($request->wantsJson()) {
48
                return response('Unauthorized.', 401);
49
            } else {
50
                return $this->redirect();
51
            }
52
        }
53
54
        return $next($request);
55
    }
56
57
    protected function redirect()
58
    {
59
        $url = app()->isAdmin() ? admin_url('login') : site_url('login');
60
        return redirect()->guest($url)->with(['error' => 'You must log in first']);
0 ignored issues
show
Bug introduced by
It seems like $url defined by app()->isAdmin() ? admin...n') : site_url('login') on line 59 can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Illuminate\Routing\Redirector::guest() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
61
    }
62
63
}
64