Completed
Push — develop ( 1b547a...2f6a53 )
by Sean
02:41
created

src/Providers/Auth/Illuminate.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of jwt-auth
5
 *
6
 * (c) Sean Tymon <[email protected]>
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 Tymon\JWTAuth\Providers\Auth;
13
14
use Tymon\JWTAuth\Contracts\Providers\Auth;
15
use Illuminate\Contracts\Auth\Guard as GuardContract;
16
17
class Illuminate implements Auth
18
{
19
    /**
20
     * @var \Illuminate\Contracts\Auth\Guard
21
     */
22
    protected $auth;
23
24
    /**
25
     * @param \Illuminate\Contracts\Auth\Guard  $auth
26
     */
27 8
    public function __construct(GuardContract $auth)
28
    {
29 8
        $this->auth = $auth;
30 8
    }
31
32
    /**
33
     * Check a user's credentials
34
     *
35
     * @param  array  $credentials
36
     *
37
     * @return boolean
38
     */
39 2
    public function byCredentials(array $credentials)
40
    {
41 2
        return $this->auth->once($credentials);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Guard as the method once() does only exist in the following implementations of said interface: Illuminate\Auth\SessionGuard, Tymon\JWTAuth\JWTGuard.

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...
42
    }
43
44
    /**
45
     * Authenticate a user via the id
46
     *
47
     * @param  mixed  $id
48
     *
49
     * @return boolean
50
     */
51 4
    public function byId($id)
52
    {
53 4
        return $this->auth->onceUsingId($id);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Guard as the method onceUsingId() does only exist in the following implementations of said interface: Illuminate\Auth\SessionGuard, Tymon\JWTAuth\JWTGuard.

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...
54
    }
55
56
    /**
57
     * Get the currently authenticated user
58
     *
59
     * @return mixed
60
     */
61 2
    public function user()
62
    {
63 2
        return $this->auth->user();
64
    }
65
}
66