Http::http()   B
last analyzed

Complexity

Conditions 6
Paths 1

Size

Total Lines 56
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 56
c 0
b 0
f 0
rs 8.9297
cc 6
nc 1
nop 1

How to fix   Long Method   

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
 * SW client http
4
 * User: moyo
5
 * Date: 2018/7/2
6
 * Time: 3:25 PM
7
 */
8
9
namespace Carno\HTTP\Powered\Swoole\Chips;
10
11
use Carno\HTTP\Standard\Response;
12
use Carno\HTTP\Standard\Streams\Form;
13
use Psr\Http\Message\RequestInterface as Request;
14
use Swoole\Http\Client as SWHClient;
0 ignored issues
show
Bug introduced by
The type Swoole\Http\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
trait Http
17
{
18
    /**
19
     * @param Request $request
20
     * @return array
21
     */
22
    private function http(Request $request) : array
23
    {
24
        /**
25
         * @var SWHClient $http
26
         */
27
28
        $http = $this->http;
29
30
        // -- request init
31
32
        $http->setMethod($request->getMethod());
33
34
        // -- net sender
35
36
        $executor = function ($fn) use ($http, $request) {
37
            $uri = $this->getUriPath($request->getUri());
0 ignored issues
show
Bug introduced by
It seems like getUriPath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

37
            /** @scrutinizer ignore-call */ 
38
            $uri = $this->getUriPath($request->getUri());
Loading history...
38
39
            $stream = $request->getBody();
40
41
            if ($stream instanceof Form) {
42
                foreach ($stream->files() as $name => $file) {
43
                    $http->addFile($file->path(), $name);
44
                }
45
                $http->post($uri, $stream->data(), $fn);
46
                return;
47
            }
48
49
            if ($stream->getSize() > 0) {
50
                $http->setData((string)$stream);
51
            }
52
53
            $http->execute($uri, $fn);
54
        };
55
56
        // -- net receiver
57
58
        $receiver = function (SWHClient $c) use ($request) {
59
            $code = $c->statusCode;
60
            $headers = (array)$c->headers;
61
            $response = $c->body;
62
63
            // reset cli headers !!
64
            foreach (array_keys($c->headers ?? []) as $k) {
65
                unset($c->headers[$k]);
66
            }
67
68
            if ($code > 0) {
69
                return new Response($code, $headers, $response);
70
            }
71
72
            $this->throwing($request, $c);
0 ignored issues
show
Bug introduced by
It seems like throwing() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

72
            $this->/** @scrutinizer ignore-call */ 
73
                   throwing($request, $c);
Loading history...
73
        };
74
75
        // -- packing
76
77
        return [$executor, $receiver];
78
    }
79
}
80