Completed
Push — travis-xenial ( c9786b...570728 )
by Kamil
05:21
created

CookieSetter::shouldMinkSessionBePrepared()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6346
c 0
b 0
f 0
cc 7
nc 5
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Service\Setter;
15
16
use Behat\Mink\Driver\Selenium2Driver;
17
use Behat\Mink\Session;
18
use DMore\ChromeDriver\ChromeDriver;
19
use FriendsOfBehat\SymfonyExtension\Driver\SymfonyDriver;
20
use Symfony\Component\BrowserKit\Cookie;
21
22
final class CookieSetter implements CookieSetterInterface
23
{
24
    /** @var Session */
25
    private $minkSession;
26
27
    /** @var array */
28
    private $minkParameters;
29
30
    public function __construct(Session $minkSession, $minkParameters)
31
    {
32
        if (!is_array($minkParameters) && !$minkParameters instanceof \ArrayAccess) {
33
            throw new \InvalidArgumentException(sprintf(
34
                '"$minkParameters" passed to "%s" has to be an array or implement "%s".',
35
                self::class,
36
                \ArrayAccess::class
37
            ));
38
        }
39
40
        $this->minkSession = $minkSession;
41
        $this->minkParameters = $minkParameters;
0 ignored issues
show
Documentation Bug introduced by
It seems like $minkParameters can also be of type object<ArrayAccess>. However, the property $minkParameters is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function setCookie($name, $value)
48
    {
49
        $driver = $this->minkSession->getDriver();
50
51
        if ($driver instanceof ChromeDriver) {
0 ignored issues
show
Bug introduced by
The class DMore\ChromeDriver\ChromeDriver does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
52
            if (!$driver->isStarted()) {
53
                $driver->start();
54
            }
55
        }
56
57
        $this->prepareMinkSessionIfNeeded($this->minkSession);
58
59
        if ($driver instanceof SymfonyDriver) {
0 ignored issues
show
Bug introduced by
The class FriendsOfBehat\SymfonyEx...on\Driver\SymfonyDriver does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
60
            $driver->getClient()->getCookieJar()->set(
61
                new Cookie($name, $value, null, null, parse_url($this->minkParameters['base_url'], \PHP_URL_HOST))
62
            );
63
64
            return;
65
        }
66
67
        $this->minkSession->setCookie($name, $value);
68
    }
69
70
    private function prepareMinkSessionIfNeeded(Session $session): void
71
    {
72
        if ($this->shouldMinkSessionBePrepared($session)) {
73
            $session->visit(rtrim($this->minkParameters['base_url'], '/') . '/');
74
        }
75
    }
76
77
    private function shouldMinkSessionBePrepared(Session $session): bool
78
    {
79
        $driver = $session->getDriver();
80
81
        if ($driver instanceof SymfonyDriver) {
0 ignored issues
show
Bug introduced by
The class FriendsOfBehat\SymfonyEx...on\Driver\SymfonyDriver does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
82
            return false;
83
        }
84
85
        if ($driver instanceof Selenium2Driver && $driver->getWebDriverSession() === null) {
86
            return true;
87
        }
88
89
        if ($driver instanceof ChromeDriver && $driver->isStarted() === false) {
0 ignored issues
show
Bug introduced by
The class DMore\ChromeDriver\ChromeDriver does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
90
            return true;
91
        }
92
93
        if (false !== strpos($session->getCurrentUrl(), $this->minkParameters['base_url'])) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !(false !== strpo...rameters['base_url']));.
Loading history...
94
            return false;
95
        }
96
97
        return true;
98
    }
99
}
100