TCPServer::doWork()   B
last analyzed

Complexity

Conditions 6
Paths 13

Size

Total Lines 28
Code Lines 19

Duplication

Lines 7
Ratio 25 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 13
nop 4
dl 7
loc 28
rs 8.439
c 0
b 0
f 0
ccs 0
cts 21
cp 0
crap 42
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\TCP;
16
use swoole_server;
17
18
/**
19
 * Class TCPServer.
20
 */
21
class TCPServer extends TCP
22
{
23
    use OnWorkerStart;
24
25
    /**
26
     * @param swoole_server $server
27
     * @param $fd
28
     * @param $data
29
     * @param $from_id
30
     *
31
     * @return int
32
     */
33
    public function doWork(swoole_server $server, $fd, $data, $from_id)
34
    {
35
        if ('quit' === $data) {
36
            $server->close($fd);
37
38
            return 0;
39
        }
40
        $data = Json::decode($data);
41
        $request = new ServerRequest($data['method'], $data['path']);
42 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...
43
            if ('GET' === $request->getMethod()) {
44
                $request->withQueryParams($data['args']);
45
            } else {
46
                $request->withParsedBody($data['args']);
47
            }
48
        }
49
        $response = app()->handleRequest($request);
50
        if (null !== $response->getFileDescriptor()) {
51
            $fd = $response->getFileDescriptor();
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...
52
        }
53
        if (false === $server->connection_info($fd)) {
54
            return -1;
55
        }
56
        $server->send($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...
57
        app()->shutdown($request, $response);
58
59
        return 0;
60
    }
61
}
62