Completed
Push — master ( d5bc1c...a22fa1 )
by Oscar
05:09
created

IncludeResponse   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 9.72 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A continueOnError() 0 6 1
B __invoke() 7 29 5
A includeFile() 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 Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Utils;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Middleware to include a php with the response.
11
 */
12
class IncludeResponse
13
{
14
    use Utils\FileTrait;
15
    use Utils\StreamTrait;
16
17
    private $continueOnError = false;
18
19
    /**
20
     * Configure if continue to the next middleware if the response has not found.
21
     * 
22
     * @param bool $continueOnError
23
     * 
24
     * @return self
25
     */
26
    public function continueOnError($continueOnError = true)
27
    {
28
        $this->continueOnError = $continueOnError;
29
30
        return $this;
31
    }
32
33
    /**
34
     * Execute the middleware.
35
     *
36
     * @param ServerRequestInterface $request
37
     * @param ResponseInterface      $response
38
     * @param callable               $next
39
     *
40
     * @return ResponseInterface
41
     */
42
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
43
    {
44
        $file = $this->getFilename($request, 'php');
45
46 View Code Duplication
        if (!is_file($file)) {
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...
47
            if ($this->continueOnError) {
48
                return $next($request, $response);
49
            }
50
51
            return $response->withStatus(404);
52
        }
53
54
        if (strtolower(pathinfo($file, PATHINFO_EXTENSION)) === 'php') {
55
            $level = ob_get_level();
56
            ob_start();
57
            self::includeFile($file);
58
            $body = self::createStream();
59
            $body->write(Utils\Helpers::getOutput($level));
60
61
            foreach (headers_list() as $header) {
62
                list($name, $value) = array_map('trim', explode(':', $header, 2));
63
                $response = $response->withHeader($name, $value);
64
            }
65
66
            return $response->withBody($body);
67
        }
68
69
        return $next($request, $response);
70
    }
71
72
    /**
73
     * Includes a php file from a static context.
74
     * 
75
     * @param string $file
76
     * 
77
     * @return string
78
     */
79
    private static function includeFile($file)
80
    {
81
        include $file;
82
    }
83
}
84