Completed
Push — master ( fc7027...e72ab1 )
by Philip
24:06
created

tests/Acceptance/BaseAcceptanceTest.php (2 issues)

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
namespace App\Tests\Acceptance;
4
5
use App\Entity\User;
6
use App\Tests\Integration\BaseIntegrationTest;
7
use Symfony\Component\BrowserKit\AbstractBrowser;
8
use Symfony\Component\BrowserKit\Cookie;
9
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
10
11
abstract class BaseAcceptanceTest extends BaseIntegrationTest
12
{
13
    /**
14
     * @param User $user
15
     */
16
    protected function logIn(AbstractBrowser $client, User $user)
17
    {
18
        $session = $client->getContainer()->get('session');
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class Symfony\Component\BrowserKit\AbstractBrowser as the method getContainer() does only exist in the following sub-classes of Symfony\Component\BrowserKit\AbstractBrowser: Liip\FunctionalTestBundle\QueryCountClient, Symfony\Bundle\FrameworkBundle\Client, Symfony\Bundle\FrameworkBundle\KernelBrowser. 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...
19
20
        $firewall = 'main';
21
        $token = new UsernamePasswordToken($user, null, $firewall, $user->getRoles());
22
        $session->set('_security_' . $firewall, serialize($token));
23
        $session->save();
24
25
        $cookie = new Cookie($session->getName(), $session->getId());
26
        $client->getCookieJar()->set($cookie);
27
    }
28
29
    protected function logOut(AbstractBrowser $client)
30
    {
31
        $session = $client->getContainer()->get('session');
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class Symfony\Component\BrowserKit\AbstractBrowser as the method getContainer() does only exist in the following sub-classes of Symfony\Component\BrowserKit\AbstractBrowser: Liip\FunctionalTestBundle\QueryCountClient, Symfony\Bundle\FrameworkBundle\Client, Symfony\Bundle\FrameworkBundle\KernelBrowser. 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...
32
        $session->clear();
33
        $session->save();
34
35
        $client->getCookieJar()->clear();
36
    }
37
}
38