Completed
Push — master ( 36ffde...a7fc49 )
by Sebastian
10s
created

RequestHeader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 23
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getReferer() 0 20 4
1
<?php
2
3
namespace Spatie\Referer\Sources;
4
5
use Spatie\Referer\Source;
6
use Illuminate\Http\Request;
7
use Spatie\Referer\Helpers\Url;
8
9
class RequestHeader implements Source
10
{
11
    public function getReferer(Request $request): string
12
    {
13
        $referer = $request->header('referer', '');
14
15
        if (empty($referer)) {
16
            return '';
17
        }
18
19
        $refererHost = Url::host($referer);
0 ignored issues
show
Bug introduced by
It seems like $referer defined by $request->header('referer', '') on line 13 can also be of type array; however, Spatie\Referer\Helpers\Url::host() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
20
21
        if (empty($refererHost)) {
22
            return '';
23
        }
24
25
        if ($refererHost === $request->getHost()) {
26
            return '';
27
        }
28
29
        return $refererHost;
30
    }
31
}
32