Completed
Pull Request — 4.0 (#3859)
by k-yamamura
06:07
created

determineTargetUrl()   B

Complexity

Conditions 11
Paths 9

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 9
nop 1
dl 0
loc 31
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Security\Http\Authentication;
15
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
18
use Symfony\Component\Security\Http\ParameterBagUtils;
19
use Symfony\Component\Security\Http\Util\TargetPathTrait;
20
21
class EccubeAuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler
22
{
23
    use TargetPathTrait;
24
25
    /**
26
     * Builds the target URL according to the defined options.
27
     *
28
     * @return string
29
     */
30
    protected function determineTargetUrl(Request $request)
31
    {
32
        if ($this->options['always_use_default_target_path']) {
33
            return $this->options['default_target_path'];
34
        }
35
36
        if ($targetUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['target_path_parameter'])) {
37
            if (preg_match('/^https?:\\\\/i', $targetUrl)) {
38
                $targetUrl = '/';
39
            }
40
41
            return $targetUrl;
42
        }
43
44
        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...
45
            $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...
46
47
            return $targetUrl;
48
        }
49
50
        if ($this->options['use_referer'] && $targetUrl = $request->headers->get('Referer')) {
51
            if (false !== $pos = strpos($targetUrl, '?')) {
52
                $targetUrl = substr($targetUrl, 0, $pos);
53
            }
54
            if ($targetUrl && $targetUrl !== $this->httpUtils->generateUri($request, $this->options['login_path'])) {
55
                return $targetUrl;
56
            }
57
        }
58
59
        return $this->options['default_target_path'];
60
    }
61
62
}
63