Completed
Push — master ( 00ed35...63fbb3 )
by Oscar
10:20
created

TrailingSlash   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 63
Duplicated Lines 7.94 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 7
Bugs 1 Features 1
Metric Value
wmc 13
c 7
b 1
f 1
lcom 0
cbo 4
dl 5
loc 63
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C __invoke() 5 33 12

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 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\BasePathTrait;
15
    use Utils\RedirectTrait;
16
17
    /**
18
     * @var bool Add or remove the slash
19
     */
20
    private $addSlash;
21
22
    /**
23
     * Configure whether add or remove the slash.
24
     *
25
     * @param bool $addSlash
26
     */
27
    public function __construct($addSlash = false)
28
    {
29
        $this->addSlash = (boolean) $addSlash;
30
    }
31
32
    /**
33
     * Execute the middleware.
34
     *
35
     * @param RequestInterface  $request
36
     * @param ResponseInterface $response
37
     * @param callable          $next
38
     *
39
     * @return ResponseInterface
40
     */
41
    public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
42
    {
43
        $uri = $request->getUri();
44
        $path = $uri->getPath();
45
46
        //Test basePath
47
        if (!$this->testBasePath($path)) {
48
            return $next($request, $response);
49
        }
50
51
        //Add/remove slash
52
        if ($this->addSlash) {
53
            if (strlen($path) > 1 && substr($path, -1) !== '/' && !pathinfo($path, PATHINFO_EXTENSION)) {
54
                $path .= '/';
55
            }
56 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...
57
            if (strlen($path) > 1 && substr($path, -1) === '/') {
58
                $path = substr($path, 0, -1);
59
            }
60
        }
61
62
        //Ensure the path has one "/"
63
        if (empty($path) || $path === $this->basePath) {
64
            $path .= '/';
65
        }
66
67
        //redirect
68
        if (is_int($this->redirectStatus) && ($uri->getPath() !== $path)) {
69
            return self::getRedirectResponse($this->redirectStatus, $uri->withPath($path), $response);
70
        }
71
72
        return $next($request->withUri($uri->withPath($path)), $response);
73
    }
74
}
75