UDPServer::doPacket()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 7
Ratio 41.18 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 3
dl 7
loc 17
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 13
cp 0
crap 12
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\UDP;
16
use swoole_server;
17
18
/**
19
 * Class UDPServer.
20
 */
21
class UDPServer extends UDP
22
{
23
    use OnWorkerStart;
24
25
    /**
26
     * @param swoole_server $server
27
     * @param $data
28
     * @param $clientInfo
29
     *
30
     * @return mixed
31
     */
32
    public function doPacket(swoole_server $server, $data, $clientInfo)
33
    {
34
        $data = Json::decode($data);
35
        $request = new ServerRequest($data['method'], $data['path']);
36 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...
37
            if ('GET' === $request->getMethod()) {
38
                $request->withQueryParams($data['args']);
39
            } else {
40
                $request->withParsedBody($data['args']);
41
            }
42
        }
43
        $response = app()->handleRequest($request);
44
        $server->sendto($clientInfo['address'], $clientInfo['port'], (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...
45
        app()->shutdown($request, $response);
46
47
        return 0;
48
    }
49
}
50