Completed
Pull Request — master (#7)
by Mateusz
01:54
created

RealIPProcessor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 5 1
B getClientIP() 0 21 7
1
<?php
2
3
namespace SilverStripe\Auditor;
4
5
class RealIPProcessor
6
{
7
8
    public function __invoke(array $record)
9
    {
10
		$record['extra']['real_ip'] = $this->getClientIP();
11
		return $record;
12
	}
13
14
    private function getClientIP()
0 ignored issues
show
Coding Style introduced by
getClientIP uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
15
    {
16
        $ipaddress = '';
0 ignored issues
show
Unused Code introduced by
$ipaddress is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
17
        if (@$_SERVER['HTTP_CLIENT_IP']) {
18
            $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
19
        } elseif (@$_SERVER['HTTP_X_FORWARDED_FOR']) {
20
            $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
21
        } elseif (@$_SERVER['HTTP_X_FORWARDED']) {
22
            $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
23
        } elseif (@$_SERVER['HTTP_FORWARDED_FOR']) {
24
            $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
25
        } elseif (@$_SERVER['HTTP_FORWARDED']) {
26
            $ipaddress = $_SERVER['HTTP_FORWARDED'];
27
        } elseif (@$_SERVER['REMOTE_ADDR']) {
28
            $ipaddress = $_SERVER['REMOTE_ADDR'];
29
        } else {
30
            $ipaddress = 'UNKNOWN';
31
        }
32
33
        return $ipaddress;
34
    }
35
36
}
37