Completed
Push — symfony-3.3 ( 61ed24...4cbc43 )
by Kamil
35:18 queued 11:41
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
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 30
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C determineTargetUrl() 0 22 8
1
<?php
2
3
namespace Sylius\Bundle\CoreBundle\Symfony;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler as BaseDefaultAuthenticationSuccessHandler;
7
use Symfony\Component\Security\Http\ParameterBagUtils;
8
use Symfony\Component\Security\Http\Util\TargetPathTrait;
9
10
/**
11
 * TODO: Workaround for regression introduced in Symfony 3.3. To be deleted when fixed.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
12
 *
13
 * @see https://github.com/symfony/symfony/pull/23411
14
 * @see https://github.com/symfony/symfony/pull/23061
15
 *
16
 * @internal
17
 */
18
final class DefaultAuthenticationSuccessHandler extends BaseDefaultAuthenticationSuccessHandler
19
{
20
    use TargetPathTrait;
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function determineTargetUrl(Request $request)
26
    {
27
        if ($this->options['always_use_default_target_path']) {
28
            return $this->options['default_target_path'];
29
        }
30
31
        if ($targetUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['target_path_parameter'])) {
32
            return $targetUrl;
33
        }
34
35
        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...
36
            $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...
37
38
            return $targetUrl;
39
        }
40
41
        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)) {
42
            return $targetUrl;
43
        }
44
45
        return $this->options['default_target_path'];
46
    }
47
}
48