Completed
Push — master ( bf6805...5227c9 )
by Maxime
07:09
created

Secure   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 29
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 12 4
1
<?php
2
3
namespace Distilleries\Expendable\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Contracts\Foundation\Application;
7
8
class Secure {
9
10
    protected $app;
11
12
    public function __construct(Application $app)
13
    {
14
        $this->app = $app;
15
    }
16
17
    /**
18
     * Handle an incoming request.
19
     *
20
     * @param \Illuminate\Http\Request $request
21
     * @param \Closure $next
22
     * @return mixed
23
     */
24
    public function handle($request, Closure $next)
25
    {
26
27
        if (! $request->isSecure() and env('SECURE_COOKIE', false)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
28
            if (strpos($request->getRequestUri(), '/storage/') === false) {
29
                return redirect()->secure($request->getRequestUri());
30
            }
31
32
        }
33
34
        return $next($request);
35
    }
36
}