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

RequestHeader::getReferer()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
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