Passed
Push — master ( 30f57b...f3320a )
by huang
02:57
created

UDPServer::doPacket()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 14

Duplication

Lines 7
Ratio 36.84 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
nc 6
nop 3
dl 7
loc 19
ccs 0
cts 15
cp 0
crap 20
rs 9.2
c 1
b 0
f 0
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      http://www.fast-d.cn/
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']);
0 ignored issues
show
Documentation introduced by
$data['args'] is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
            } else {
40
                $request->withParsedBody($data['args']);
0 ignored issues
show
Documentation introduced by
$data['args'] is of type string, but the function expects a null|array|object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
            }
42
        }
43
        try {
44
            $response = app()->handleRequest($request);
45
        } catch (\Exception $e) {
46
            $response = app()->handleException($request, $e);
47
        }
48
        $server->sendto($clientInfo['address'], $clientInfo['port'], (string) $response->getBody());
49
        app()->shutdown($request, $response);
50
    }
51
}
52