Completed
Push — master ( e82fa0...66aef9 )
by Korotkov
02:22
created

HttpMiddleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 38.71 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 2
dl 12
loc 31
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 12 12 2
A next() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Http;
4
5
use Rudra\SetContainerTrait;
6
7
class HttpMiddleware
8
{
9
10
    use SetContainerTrait;
11
12
    /**
13
     * @param null $middleware
14
     *
15
     * @return bool
16
     */
17 View Code Duplication
    public function __invoke($middleware = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
__invoke 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...
18
    {
19
        // StartMiddleware
20
21
        if ($middleware[0][1]['int'] % 2) {
22
            echo json_encode($_SERVER);
23
        }
24
25
        // EndMiddleware
26
27
        $this->next($middleware);
28
    }
29
30
    /**
31
     * @param $middleware
32
     */
33
    protected function next($middleware)
34
    {
35
        $this->container()->get('router')->handleMiddleware($middleware, 1);
36
    }
37
}
38