Completed
Push — master ( 6a4ea3...4e0e71 )
by Paweł
11:05
created

determineTargetUrl()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 22
rs 6.6037
c 1
b 0
f 0
cc 8
eloc 11
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
namespace Sylius\Bundle\UserBundle\Authentication;
13
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler as SymfonyDefaultAuthenticationSuccessHandler;
16
use Symfony\Component\Security\Http\ParameterBagUtils;
17
use Symfony\Component\Security\Http\Util\TargetPathTrait;
18
19
/**
20
 * TODO: Workaround for regression introduced in Symfony 3.3. To be deleted when fixed.
21
 *
22
 * @see https://github.com/symfony/symfony/pull/23411
23
 * @see https://github.com/symfony/symfony/pull/23061
24
 *
25
 * @internal
26
 *
27
 * {@inheritdoc}
28
 */
29
class DefaultAuthenticationSuccessHandler extends SymfonyDefaultAuthenticationSuccessHandler
30
{
31
    use TargetPathTrait;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function determineTargetUrl(Request $request)
37
    {
38
        if ($this->options['always_use_default_target_path']) {
39
            return $this->options['default_target_path'];
40
        }
41
42
        if ($targetUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['target_path_parameter'])) {
43
            return $targetUrl;
44
        }
45
46
        if (null !== $this->providerKey && $targetUrl = $this->getTargetPath($request->getSession(), $this->providerKey)) {
0 ignored issues
show
Bug introduced by
It seems like $request->getSession() can be null; however, getTargetPath() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
47
            $this->removeTargetPath($request->getSession(), $this->providerKey);
0 ignored issues
show
Bug introduced by
It seems like $request->getSession() can be null; however, removeTargetPath() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
48
49
            return $targetUrl;
50
        }
51
52
        if ($this->options['use_referer'] && ($targetUrl = $request->headers->get('Referer')) && parse_url($targetUrl, PHP_URL_PATH) !== parse_url($this->httpUtils->generateUri($request, $this->options['login_path']), PHP_URL_PATH)) {
0 ignored issues
show
Bug Compatibility introduced by
The expression $request->headers->get('Referer'); of type string|array adds the type array to the return on line 53 which is incompatible with the return type of the parent method Symfony\Component\Securi...ler::determineTargetUrl of type string.
Loading history...
53
            return $targetUrl;
54
        }
55
56
        return $this->options['default_target_path'];
57
    }
58
}
59