Passed
Push — master ( 4770e0...cf1e90 )
by Joel
02:26
created

RedirectionIO::findRedirect()   B

Complexity

Conditions 11
Paths 37

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 11.968

Importance

Changes 0
Metric Value
cc 11
eloc 31
nc 37
nop 0
dl 0
loc 52
ccs 28
cts 35
cp 0.8
crap 11.968
rs 7.3166
c 0
b 0
f 0

How to fix   Long Method    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
namespace RedirectionIO\Client\Wordpress;
4
5
use RedirectionIO\Client\Sdk\Client;
6
use RedirectionIO\Client\Sdk\HttpMessage\Request;
7
use RedirectionIO\Client\Sdk\HttpMessage\Response;
8
9
/**
10
 * Main plugin file.
11
 *
12
 * This class is the core logic of the plugin.
13
 */
14
class RedirectionIO
15
{
16
    private $client;
17
18
    private $lastRuleId = null;
19
20 4
    public function __construct()
21
    {
22 4
        add_action('plugins_loaded', [$this, 'findRedirect']);
0 ignored issues
show
Unused Code introduced by
The call to RedirectionIO\Client\Wordpress\add_action() has too many arguments starting with 'plugins_loaded'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
        /** @scrutinizer ignore-call */ 
23
        add_action('plugins_loaded', [$this, 'findRedirect']);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
23 4
        add_action('template_redirect', [$this, 'log']);
24 4
    }
25
26
    public function setUp()
27
    {
28
        add_option('redirectionio', [
29
            'connections' => [
30
                [
31
                    'name' => '',
32
                    'remote_socket' => '',
33
                ],
34
            ],
35
            'doNotRedirectAdmin' => true,
36
        ]);
37
    }
38
39 4
    public function findRedirect()
40
    {
41 4
        $options = get_option('redirectionio');
0 ignored issues
show
Unused Code introduced by
The call to RedirectionIO\Client\Wordpress\get_option() has too many arguments starting with 'redirectionio'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $options = /** @scrutinizer ignore-call */ get_option('redirectionio');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
42 4
        $connections = [];
43
44 4
        if (false === $options || !isset($options['connections'])) {
45
            return false;
46
        }
47
48 4
        foreach ($options['connections'] as $connection) {
49 4
            $connections[$connection['name']] = $connection['remote_socket'];
50 4
        }
51
52 4
        $this->client = new Client($connections);
53 4
        $scheme = 'http';
54
55 4
        if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
56
            $scheme = $_SERVER['HTTP_X_FORWARDED_PROTO'];
57 4
        } elseif (!empty($_SERVER['HTTPS'])) {
58
            $scheme = 'https';
59
        }
60
61 4
        $request = new Request(
62 4
            $_SERVER['HTTP_HOST'],
63 4
            $_SERVER['REQUEST_URI'],
64 4
            $_SERVER['HTTP_USER_AGENT'],
65 4
            $_SERVER['HTTP_REFERER'],
66
            $scheme
67 4
        );
68
69 4
        if ($this->isAdminPage($request) && $options['doNotRedirectAdmin']) {
70
            return false;
71
        }
72
73 4
        $response = $this->client->findRedirect($request);
74
75 4
        if (null === $response) {
76 2
            return false;
77
        }
78
79 2
        if (method_exists($response, 'getRuleId')) {
80
            $this->lastRuleId = $response->getRuleId();
81
        }
82
83 2
        if ($response->getStatusCode() === 410) {
84 1
            define('DONOTCACHEPAGE', true); // WP Super Cache and W3 Total Cache recognise this
85 1
            status_header(410);
86 1
        } else {
87 1
            wp_redirect($response->getLocation(), $response->getStatusCode());
0 ignored issues
show
introduced by
The method getLocation() does not exist on RedirectionIO\Client\Sdk\HttpMessage\Response. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
            wp_redirect($response->/** @scrutinizer ignore-call */ getLocation(), $response->getStatusCode());
Loading history...
88
        }
89
90 2
        $this->exitCode();
91 2
    }
92
93
    public function log()
94
    {
95
        $scheme = 'http';
96
97
        if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
98
            $scheme = $_SERVER['HTTP_X_FORWARDED_PROTO'];
99
        } elseif (!empty($_SERVER['HTTPS'])) {
100
            $scheme = 'https';
101
        }
102
103
        $location = null;
104
        $headers = headers_list();
105
106
        foreach ($headers as $header) {
107
            $locationPos = stripos($header, 'Location: ');
108
109
            if ($locationPos !== false) {
110
                $location = substr($header, $locationPos);
111
            }
112
        }
113
114
        $request = new Request(
115
            $_SERVER['HTTP_HOST'],
116
            $_SERVER['REQUEST_URI'],
117
            $_SERVER['HTTP_USER_AGENT'],
118
            $_SERVER['HTTP_REFERER'],
119
            $scheme
120
        );
121
122
        $response = new Response(http_response_code(), $this->lastRuleId, $location);
0 ignored issues
show
Unused Code introduced by
The call to RedirectionIO\Client\Sdk...Response::__construct() has too many arguments starting with $this->lastRuleId. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
        $response = /** @scrutinizer ignore-call */ new Response(http_response_code(), $this->lastRuleId, $location);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
Are you sure the usage of http_response_code() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
123
124
        $this->client->log($request, $response);
125
    }
126
127
    public function exitCode()
128
    {
129
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
130
    }
131
132
    /**
133
     * Check if the requested page belongs to admin area.
134
     *
135
     * @param Request $request
136
     */
137 4
    private function isAdminPage(Request $request)
138
    {
139 4
        $adminRoot = str_replace(get_site_url(), '', get_admin_url());
0 ignored issues
show
Bug introduced by
Are you sure the usage of get_site_url() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
Are you sure the usage of get_admin_url() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
140 4
        $requestPath = substr($request->getPath(), 0, strlen($adminRoot));
141
142 4
        if ($adminRoot === $requestPath) {
143 4
            return true;
144
        }
145
146
        return false;
147
    }
148
}
149