Socket.php$0 ➔ socket()   B
last analyzed

Complexity

Conditions 4

Size

Total Lines 106

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 106
c 0
b 0
f 0
rs 8
cc 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A Socket.php$0 ➔ __construct() 0 3 1
A Socket.php$0 ➔ push() 0 3 1
A Socket.php$0 ➔ close() 0 3 1
A Socket.php$0 ➔ valid() 0 3 1
A Socket.php$0 ➔ status() 0 3 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 socket
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\Client\Frame;
12
use Carno\HTTP\Client\Framing;
13
use Carno\HTTP\Contracts\WSocket;
14
use Carno\HTTP\Contracts\WSOpcode;
15
use Carno\HTTP\Exception\ErrorResponseException;
16
use Carno\HTTP\Standard\Utils\CodePhrases;
17
use Psr\Http\Message\RequestInterface as Request;
18
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...
19
use Swoole\WebSocket\Frame as SWSFrame;
0 ignored issues
show
Bug introduced by
The type Swoole\WebSocket\Frame 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...
20
21
trait Socket
22
{
23
    /**
24
     * @param Request $request
25
     * @return array
26
     */
27
    private function socket(Request $request) : array
28
    {
29
        /**
30
         * @var SWHClient $http
31
         */
32
33
        $http = $this->http;
34
35
        // -- frame handler
36
37
        $framing = new Framing($socket = new class($http) implements WSocket {
38
            /**
39
             * @var SWHClient
40
             */
41
            private $c = null;
42
43
            /**
44
             * @var bool
45
             */
46
            private $v = false;
47
48
            /**
49
             * anonymous constructor.
50
             * @param SWHClient $c
51
             */
52
            public function __construct(SWHClient $c)
53
            {
54
                $this->c = $c;
55
            }
56
57
            /**
58
             * @param bool $valid
59
             */
60
            public function status(bool $valid) : void
61
            {
62
                $this->v = $valid;
63
            }
64
65
            /**
66
             * @return bool
67
             */
68
            public function valid() : bool
69
            {
70
                return $this->v;
71
            }
72
73
            /**
74
             * @param Frame $frame
75
             */
76
            public function push(Frame $frame) : void
77
            {
78
                $this->c->push($frame->data(), $frame->code());
79
            }
80
81
            /**
82
             */
83
            public function close() : void
84
            {
85
                $this->push(new Frame(pack('n', 1000), WSOpcode::CLOSING));
86
            }
87
        });
88
89
        // -- message event
90
91
        $http->on('message', static function (SWHClient $c, SWSFrame $f) use ($framing, $socket) {
92
            switch ($f->opcode) {
93
                case WSOpcode::CLOSING:
94
                    $socket->status(false);
95
                    $framing->message()->close();
96
                    return;
97
                default:
98
                    $framing->message()->send(new Frame($f->data, $f->opcode));
99
            }
100
        });
101
102
        $http->on('close', static function (SWHClient $c) use ($framing, $socket) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed. ( Ignorable by Annotation )

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

102
        $http->on('close', static function (/** @scrutinizer ignore-unused */ SWHClient $c) use ($framing, $socket) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
            $socket->status(false);
104
            $framing->message()->close();
105
        });
106
107
        // -- socket dial
108
109
        $connector = function ($fn) use ($http, $request) {
110
            $http->upgrade($this->getUriPath($request->getUri()), $fn);
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

110
            $http->upgrade($this->/** @scrutinizer ignore-call */ getUriPath($request->getUri()), $fn);
Loading history...
111
        };
112
113
        // -- socket resp
114
115
        $response = function (SWHClient $c) use ($request, $framing, $socket) {
116
            $code = $c->statusCode;
117
118
            if ($code === 101) {
119
                $socket->status(true);
120
                return $framing;
121
            }
122
123
            if ($code > 0) {
124
                throw new ErrorResponseException(CodePhrases::resolve($code), $code);
125
            }
126
127
            $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

127
            $this->/** @scrutinizer ignore-call */ 
128
                   throwing($request, $c);
Loading history...
128
        };
129
130
        // -- packing
131
132
        return [$connector, $response];
133
    }
134
}
135