Completed
Pull Request — master (#308)
by Sergey
03:14
created

ResolvesCurrentUser::resolveCurrentUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Traits;
4
5
use seregazhuk\PinterestBot\Api\Providers\User;
6
use seregazhuk\PinterestBot\Api\ProvidersContainer;
7
use seregazhuk\PinterestBot\Api\Providers\Core\Provider;
8
9
/**
10
 * @property ProvidersContainer container
11
 */
12
trait ResolvesCurrentUser
13
{
14
    /**
15
     * @return string
16
     */
17
    protected function resolveCurrentUsername()
18
    {
19
        return $this->getUserProvider()->username();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class seregazhuk\PinterestBot\...Providers\Core\Provider as the method username() does only exist in the following sub-classes of seregazhuk\PinterestBot\...Providers\Core\Provider: seregazhuk\PinterestBot\Api\Providers\User. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
20
    }
21
22
    /**
23
     * @return string
24
     */
25
    protected function resolveCurrentUserId()
26
    {
27
        return $this->getUserProvider()->id();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class seregazhuk\PinterestBot\...Providers\Core\Provider as the method id() does only exist in the following sub-classes of seregazhuk\PinterestBot\...Providers\Core\Provider: seregazhuk\PinterestBot\Api\Providers\User. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
28
    }
29
30
    /**
31
     * @return User|Provider
32
     */
33
    protected function getUserProvider()
34
    {
35
        return $this->container->getProvider('user');
36
    }
37
}
38