CurlCommandFormatter::formatRequest()   B
last analyzed

Complexity

Conditions 10
Paths 81

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 10.1371

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 10
eloc 27
c 4
b 0
f 0
nc 81
nop 1
dl 0
loc 42
ccs 24
cts 27
cp 0.8889
crap 10.1371
rs 7.6666

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 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 a cURL command for HTTP requests.
11
 *
12
 * @author Tobias Nyholm <[email protected]>
13
 */
14
class CurlCommandFormatter implements Formatter
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 6
    public function formatRequest(RequestInterface $request)
20
    {
21 6
        $command = sprintf('curl %s', escapeshellarg((string) $request->getUri()->withFragment('')));
22 6
        if ('1.0' === $request->getProtocolVersion()) {
23
            $command .= ' --http1.0';
24 6
        } elseif ('2.0' === $request->getProtocolVersion()) {
25 1
            $command .= ' --http2';
26
        }
27
28 6
        $method = strtoupper($request->getMethod());
29 6
        if ('HEAD' === $method) {
30
            $command .= ' --head';
31 6
        } elseif ('GET' !== $method) {
32 4
            $command .= ' --request '.$method;
33
        }
34
35 6
        $command .= $this->getHeadersAsCommandOptions($request);
36
37 6
        $body = $request->getBody();
38 6
        if ($body->getSize() > 0) {
39
            // escapeshellarg argument max length on Windows, but longer body in curl command would be impractical anyways
40 4
            if ($body->getSize() > 8192) {
41 1
                $data = '[too long stream omitted]';
42 3
            } elseif ($body->isSeekable()) {
43 2
                $data = $body->__toString();
44 2
                $body->rewind();
45 2
                // all non-printable ASCII characters and <DEL> except for \t, \r, \n
46 2
                if (preg_match('/([\x00-\x09\x0C\x0E-\x1F\x7F])/', $data)) {
47
                    $data = '[binary stream omitted]';
48
                }
49 1
            } else {
50
                $data = '[non-seekable stream omitted]';
51 4
            }
52 4
            $escapedData = @escapeshellarg($data);
53
            if (empty($escapedData)) {
54
                $escapedData = 'We couldn\'t not escape the data properly';
55
            }
56 4
57
            $command .= sprintf(' --data %s', $escapedData);
58
        }
59 6
60
        return $command;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65 1
     */
66
    public function formatResponse(ResponseInterface $response)
67 1
    {
68
        return '';
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    private function getHeadersAsCommandOptions(RequestInterface $request)
75 6
    {
76
        $command = '';
77 6
        foreach ($request->getHeaders() as $name => $values) {
78 6
            if ('host' === strtolower($name) && $values[0] === $request->getUri()->getHost()) {
79 2
                continue;
80
            }
81
82
            if ('user-agent' === strtolower($name)) {
83 2
                $command .= sprintf(' -A %s', escapeshellarg($values[0]));
84 1
85
                continue;
86 1
            }
87
88
            $command .= sprintf(' -H %s', escapeshellarg($name.': '.$request->getHeaderLine($name)));
89 1
        }
90
91
        return $command;
92 6
    }
93
}
94