Completed
Push — master ( 1b2d0a...89ea1a )
by Oscar
03:00
created

TrailingSlash::__invoke()   D

Complexity

Conditions 10
Paths 16

Size

Total Lines 28
Code Lines 14

Duplication

Lines 5
Ratio 17.86 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 5
loc 28
rs 4.8196
cc 10
eloc 14
nc 16
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Utils;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Middleware to add or remove the trailing slash.
11
 */
12
class TrailingSlash
13
{
14
    use Utils\RedirectTrait;
15
16
    /**
17
     * @var bool Add or remove the slash
18
     */
19
    private $addSlash;
20
21
    /**
22
     * Configure whether add or remove the slash.
23
     *
24
     * @param bool $addSlash
25
     */
26
    public function __construct($addSlash = false)
27
    {
28
        $this->addSlash = (boolean) $addSlash;
29
    }
30
31
    /**
32
     * Execute the middleware.
33
     *
34
     * @param RequestInterface  $request
35
     * @param ResponseInterface $response
36
     * @param callable          $next
37
     *
38
     * @return ResponseInterface
39
     */
40
    public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
41
    {
42
        $uri = $request->getUri();
43
        $path = $uri->getPath();
44
45
        //Add/remove slash
46
        if ($this->addSlash) {
47
            if (strlen($path) > 1 && substr($path, -1) !== '/' && !pathinfo($path, PATHINFO_EXTENSION)) {
48
                $path .= '/';
49
            }
50 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
51
            if (strlen($path) > 1 && substr($path, -1) === '/') {
52
                $path = substr($path, 0, -1);
53
            }
54
        }
55
56
        //Ensure the path has one "/"
57
        if ($path === '') {
58
            $path = '/';
59
        }
60
61
        //redirect
62
        if ($this->redirectStatus !== false && ($uri->getPath() !== $path)) {
63
            return $this->getRedirectResponse($request, $uri->withPath($path), $response);
0 ignored issues
show
Compatibility introduced by
$request of type object<Psr\Http\Message\RequestInterface> is not a sub-type of object<Psr\Http\Message\ServerRequestInterface>. It seems like you assume a child interface of the interface Psr\Http\Message\RequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
64
        }
65
66
        return $next($request->withUri($uri->withPath($path)), $response);
67
    }
68
}
69