WebSocketServer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 21.21 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 7
loc 33
rs 10
c 0
b 0
f 0
ccs 0
cts 17
cp 0
wmc 5
lcom 0
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B doMessage() 7 22 5

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
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      https://fastdlabs.com
8
 */
9
10
namespace FastD\Servitization\Server;
11
12
use FastD\Http\ServerRequest;
13
use FastD\Packet\Json;
14
use FastD\Servitization\OnWorkerStart;
15
use FastD\Swoole\Server\WebSocket;
16
use swoole_server;
17
use swoole_websocket_frame;
18
19
/**
20
 * Class WebSocketServer.
21
 */
22
class WebSocketServer extends WebSocket
23
{
24
    use OnWorkerStart;
25
26
    /**
27
     * @param swoole_server          $server
28
     * @param swoole_websocket_frame $frame
29
     *
30
     * @return int
31
     */
32
    public function doMessage(swoole_server $server, swoole_websocket_frame $frame)
33
    {
34
        $data = $frame->data;
35
        $data = Json::decode($data);
36
        $request = new ServerRequest($data['method'], $data['path']);
37 View Code Duplication
        if (isset($data['args'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
38
            if ('GET' === $request->getMethod()) {
39
                $request->withQueryParams($data['args']);
40
            } else {
41
                $request->withParsedBody($data['args']);
42
            }
43
        }
44
        $response = app()->handleRequest($request);
45
        $fd = null !== ($fd = $response->getFileDescriptor()) ? $fd : $frame->fd;
0 ignored issues
show
Bug introduced by
The method getFileDescriptor does only exist in FastD\Http\Response, but not in Symfony\Component\HttpFoundation\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
46
        if (false === $server->connection_info($fd)) {
47
            return -1;
48
        }
49
        $server->push($fd, (string) $response->getBody());
0 ignored issues
show
Bug introduced by
The method getBody does only exist in FastD\Http\Response, but not in Symfony\Component\HttpFoundation\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
50
        app()->shutdown($request, $response);
51
52
        return 0;
53
    }
54
}
55