Completed
Push — master ( 75955e...d5bb65 )
by Freek
01:22
created

AddCspHeaders   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 11 1
B getPolicys() 0 28 4
1
<?php
2
3
namespace Spatie\Csp;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Collection;
8
9
class AddCspHeaders
10
{
11
    public function handle(Request $request, Closure $next, $customPolicyClass = null)
12
    {
13
        $response = $next($request);
14
15
        $this
16
            ->getPolicys($customPolicyClass, $response)
0 ignored issues
show
Unused Code introduced by
The call to AddCspHeaders::getPolicys() has too many arguments starting with $response.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
17
            ->filter->shouldBeApplied($request, $response)
18
            ->each->applyTo($response);
19
20
        return $response;
21
    }
22
23
    protected function getPolicys(string $customPolicyClass = null): Collection
24
    {
25
        $policys = collect();
26
27
        if ($customPolicyClass) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $customPolicyClass of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
28
            $policys->push(PolicyFactory::create($customPolicyClass));
29
30
            return $policys;
31
        }
32
33
        $policyClass = config('csp.policy');
34
35
        if (! empty($policyClass)) {
36
            $policys->push(PolicyFactory::create($policyClass));
37
        }
38
39
        $reportOnlyPolicyClass = config('csp.report_only_policy');
40
41
        if (! empty($reportOnlyPolicyClass)) {
42
            $policy = PolicyFactory::create($reportOnlyPolicyClass);
43
44
            $policy->reportOnly();
45
46
            $policys->push($policy);
47
        }
48
49
        return $policys;
50
    }
51
}
52