Completed
Pull Request — master (#44)
by Tobias
03:28
created

FullHttpMessageFormatter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 57.63 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 3
dl 34
loc 59
ccs 0
cts 32
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A formatRequest() 17 17 2
A formatResponse() 17 17 2

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 Http\Message\Formatter;
4
5
use Http\Message\Formatter;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * A formatter that prints the complete HTTP message.
11
 *
12
 * @author Tobias Nyholm <[email protected]>
13
 */
14
class FullHttpMessageFormatter implements Formatter
15
{
16
    /**
17
     * The maximum length of the body.
18
     *
19
     * @var int
20
     */
21
    private $maxBodyLendth;
22
23
    /**
24
     * @param int $maxBodyLendth number of chars.
25
     */
26
    public function __construct($maxBodyLendth = 1000)
27
    {
28
        $this->maxBodyLendth = $maxBodyLendth;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 View Code Duplication
    public function formatRequest(RequestInterface $request)
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...
35
    {
36
        $message = sprintf(
37
            "%s %s HTTP/%s\n",
38
            $request->getMethod(),
39
            $request->getRequestTarget(),
40
            $request->getProtocolVersion()
41
        );
42
43
        foreach ($request->getHeaders() as $name => $values) {
44
            $message .= $name.': '.implode(', ', $values)."\n";
45
        }
46
47
        $message .= "\n".mb_substr($request->getBody()->__toString(), 0, $this->maxBodyLendth);
48
49
        return $message;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 View Code Duplication
    public function formatResponse(ResponseInterface $response)
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...
56
    {
57
        $message = sprintf(
58
            "HTTP/%s %s %s\n",
59
            $response->getProtocolVersion(),
60
            $response->getStatusCode(),
61
            $response->getReasonPhrase()
62
        );
63
64
        foreach ($response->getHeaders() as $name => $values) {
65
            $message .= $name.': '.implode(', ', $values)."\n";
66
        }
67
68
        $message .= "\n".mb_substr($response->getBody()->__toString(), 0, $this->maxBodyLendth);
69
70
        return $message;
71
    }
72
}
73