Issues (2)

src/HttpHelper.php (2 issues)

Labels
Severity
1
<?php
2
3
use Laminas\Diactoros\Request\Serializer as RequestSerializer;
4
use Laminas\Diactoros\Response\Serializer as ResponseSerializer;
5
6
if (!function_exists('request_to_string')) {
7
    /**
8
     * Serializes a Request (PSR7 or Symfony Http Foundation) into http format
9
     *
10
     * @param Psr\Http\Message\RequestInterface|Symfony\Component\HttpFoundation\Request $request
11
     * @return string
12
     */
13
    function request_to_string($request): string
14
    {
15
        $requestString = "unknown\r\n";
16
17
        if ($request instanceof Symfony\Component\HttpFoundation\Request &&
18
            class_exists(Symfony\Component\HttpFoundation\Request::class)
19
        ) {
20
            $requestString = $request->__toString();
21
        }
22
23
        if ($request instanceof Psr\Http\Message\RequestInterface &&
24
            interface_exists(Psr\Http\Message\RequestInterface::class)
25
        ) {
26
            $requestString = RequestSerializer::toString($request);
27
        }
28
29
        return $requestString;
30
    }
31
}
32
33
if (!function_exists('request_to_curl')) {
34
    /**
35
     * Serializes a Request (PSR7 or Symfony Http Foundation) into curl syntax
36
     *
37
     * @param Psr\Http\Message\RequestInterface|Symfony\Component\HttpFoundation\Request $request
38
     * @return string
39
     */
40
    function request_to_curl($request): string
41
    {
42
        $curl = sprintf('curl -X %s %s', $request->getMethod(), $request->getUri());
43
44
        foreach ($request->getHeaders() as $name => $values) {
0 ignored issues
show
The method getHeaders() does not exist on Symfony\Component\HttpFoundation\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        foreach ($request->/** @scrutinizer ignore-call */ getHeaders() as $name => $values) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
            $curl .= sprintf(" -H '%s'", $name . ': ' . implode(', ', $values));
46
        }
47
48
        $body = (string)$request->getBody();
0 ignored issues
show
The method getBody() does not exist on Symfony\Component\HttpFoundation\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        $body = (string)$request->/** @scrutinizer ignore-call */ getBody();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
50
        if ($body) {
51
            $curl .= sprintf(" -d '%s'", $body);
52
        }
53
54
        return $curl;
55
    }
56
}
57
58
if (!function_exists('request_to_file')) {
59
    /**
60
     * Serializes a Request (PSR7 or Symfony Http Foundation) into http format and writes it into a file.
61
     * If the file already exists, the string will be appended, using PhpStorm .http formats ###
62
     * as separator
63
     *
64
     * @param Psr\Http\Message\RequestInterface|Symfony\Component\HttpFoundation\Request $request
65
     * @param string|null $path
66
     * @return string
67
     */
68
    function request_to_file($request, string $path = null): string
69
    {
70
        $httpRequest = request_to_string($request);
71
72
        if (empty($path)) {
73
            $path = (string) $_SERVER['DOCUMENT_ROOT'];
74
        }
75
76
        if (empty($path)) {
77
            $path = (string) getenv('PWD');
78
        }
79
80
        $filename = $path . '/request.http';
81
82
        $flags = 0;
83
84
        // Append request to file if it already exists, using ### as separator
85
        // which is understood by PhpStorm
86
        if (file_exists($filename)) {
87
            $httpRequest = "\n\n### " . date(DATE_RFC822) . "\n\n" . $httpRequest;
88
            $flags = FILE_APPEND;
89
        }
90
91
        file_put_contents($filename, $httpRequest, $flags);
92
93
        return $filename;
94
    }
95
}
96
97
if (!function_exists('response_to_string')) {
98
    /**
99
     * Serializes a Request (PSR7 or Symfony Http Foundation) into http format
100
     *
101
     * @param Psr\Http\Message\ResponseInterface|Symfony\Component\HttpFoundation\Response $response
102
     * @return string
103
     */
104
    function response_to_string($response): string
105
    {
106
        $responseString = "unknown\r\n";
107
108
        if ($response instanceof Symfony\Component\HttpFoundation\Response &&
109
            class_exists(Symfony\Component\HttpFoundation\Response::class)
110
        ) {
111
            $responseString = $response->__toString();
112
        }
113
114
        if ($response instanceof Psr\Http\Message\ResponseInterface &&
115
            interface_exists(Psr\Http\Message\ResponseInterface::class)
116
        ) {
117
            $responseString = ResponseSerializer::toString($response);
118
        }
119
120
        return $responseString;
121
    }
122
}
123
124
if (!function_exists('response_to_file')) {
125
    /**
126
     * Serializes a Request (PSR7 or Symfony Http Foundation) into http format and writes it into a file.
127
     * If the file already exists, the string will be appended, using PhpStorm .http formats ###
128
     * as separator
129
     *
130
     * @param Psr\Http\Message\ResponseInterface|Symfony\Component\HttpFoundation\Response $response
131
     * @param string|null $path
132
     * @return string
133
     */
134
    function response_to_file($response, string $path = null): string
135
    {
136
        $httpResponse = response_to_string($response);
137
138
        if (empty($path)) {
139
            $path = (string) $_SERVER['DOCUMENT_ROOT'];
140
        }
141
142
        if (empty($path)) {
143
            $path = (string) getenv('PWD');
144
        }
145
146
        $filename = $path . '/response.http';
147
148
        $flags = 0;
149
150
        // Append request to file if it already exists, using ### as separator
151
        // which is understood by PhpStorm
152
        if (file_exists($filename)) {
153
            $httpResponse = "\n\n### " . date(DATE_RFC822) . "\n\n" . $httpResponse;
154
            $flags = FILE_APPEND;
155
        }
156
157
        file_put_contents($filename, $httpResponse, $flags);
158
159
        return $filename;
160
    }
161
}
162