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

DefaultAuthenticationSuccessHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 30
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C determineTargetUrl() 0 22 8
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