Route   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 172
Duplicated Lines 5.23 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
dl 9
loc 172
rs 10
c 0
b 0
f 0
wmc 18
lcom 2
cbo 6

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A setcookie() 9 19 6
A onWakeup() 0 10 1
A onSleep() 0 7 1
A onHandshake() 0 3 1
A onFrame() 0 3 1
A handleException() 0 4 1
A onFinish() 0 4 1
A gracefulShutdown() 0 4 1
A getCookieStr() 0 4 1
A setSessionState() 0 4 1
A getSessionState() 0 4 1

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
namespace PHPDaemon\WebSocket;
3
4
use PHPDaemon\Core\Daemon;
5
6
/**
7
 * Web socket route
8
 *
9
 * @package Core
10
 *
11
 * @author  Vasily Zorin <[email protected]>
12
 */
13
class Route implements RouteInterface
14
{
15
    use \PHPDaemon\Traits\StaticObjectWatchdog;
16
    use \PHPDaemon\Traits\Sessions;
17
    use \PHPDaemon\Traits\DeferredEventHandlers;
18
19
    public $attrs;
20
21
    /**
22
     * @var \PHPDaemon\Servers\WebSocket\Connection
23
     */
24
    public $client; // Remote client
25
    /**
26
     * @var \PHPDaemon\Core\AppInstance
27
     */
28
    public $appInstance;
29
30
    protected $running = true;
31
32
    /**
33
     * Called when client connected.
34
     * @param \PHPDaemon\Servers\WebSocket\Connection $client Remote client
35
     * @param \PHPDaemon\Core\AppInstance $appInstance
0 ignored issues
show
Documentation introduced by
Should the type for parameter $appInstance not be \PHPDaemon\Core\AppInstance|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
36
     */
37
    public function __construct($client, $appInstance = null)
38
    {
39
        $this->client = $client;
40
41
        $this->attrs = new \stdClass;
42
        $this->attrs->get =& $client->get;
43
        $this->attrs->cookie =& $client->cookie;
44
        $this->attrs->server =& $client->server;
45
        $this->attrs->session = null;
46
47
        if ($appInstance) {
48
            $this->appInstance = $appInstance;
49
        }
50
    }
51
52
    /**
53
     * Set the cookie
54
     * @param  string $name Name of cookie
55
     * @param  string $value Value
56
     * @param  integer $maxage Optional. Max-Age. Default is 0.
57
     * @param  string $path Optional. Path. Default is empty string.
58
     * @param  string $domain Optional. Domain. Default is empty string.
59
     * @param  boolean $secure Optional. Secure. Default is false.
60
     * @param  boolean $HTTPOnly Optional. HTTPOnly. Default is false.
61
     * @return void
62
     */
63 View Code Duplication
    public function setcookie(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
64
        $name,
65
        $value = '',
66
        $maxage = 0,
67
        $path = '',
68
        $domain = '',
69
        $secure = false,
70
        $HTTPOnly = false
71
    ) {
72
        $this->client->header(
73
            'Set-Cookie: ' . $name . '=' . rawurlencode($value)
74
            . (empty($domain) ? '' : '; Domain=' . $domain)
75
            . (empty($maxage) ? '' : '; Max-Age=' . $maxage)
76
            . (empty($path) ? '' : '; Path=' . $path)
77
            . (!$secure ? '' : '; Secure')
78
            . (!$HTTPOnly ? '' : '; HttpOnly'),
79
            false
80
        );
81
    }
82
83
    /**
84
     * Called when the request wakes up
85
     * @return void
86
     */
87
    public function onWakeup()
88
    {
89
        $this->running = true;
90
        Daemon::$context = $this;
91
        $_SESSION = &$this->attrs->session;
92
        $_GET = &$this->attrs->get;
93
        $_POST = [];
94
        $_COOKIE = &$this->attrs->cookie;
95
        Daemon::$process->setState(Daemon::WSTATE_BUSY);
0 ignored issues
show
Bug introduced by
The method setState does only exist in PHPDaemon\Thread\Worker, but not in PHPDaemon\Thread\IPC and PHPDaemon\Thread\Master.

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...
96
    }
97
98
    /**
99
     * Called when the request starts sleep
100
     * @return void
101
     */
102
    public function onSleep()
103
    {
104
        Daemon::$context = null;
105
        $this->running = false;
106
        unset($_SESSION, $_GET, $_POST, $_COOKIE);
107
        Daemon::$process->setState(Daemon::WSTATE_IDLE);
0 ignored issues
show
Bug introduced by
The method setState does only exist in PHPDaemon\Thread\Worker, but not in PHPDaemon\Thread\IPC and PHPDaemon\Thread\Master.

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...
108
    }
109
110
    /**
111
     * Called when the connection is handshaked.
112
     * @return void
113
     */
114
    public function onHandshake()
115
    {
116
    }
117
118
    /**
119
     * Called when new frame is received
120
     * @param string $data Frame's contents
121
     * @param integer $type Frame's type
122
     * @return void
123
     */
124
    public function onFrame($data, $type)
125
    {
126
    }
127
128
    /**
129
     * Uncaught exception handler
130
     * @param $e
131
     * @return boolean Handled?
132
     */
133
    public function handleException($e)
134
    {
135
        return false;
136
    }
137
138
    /**
139
     * Called when session finished.
140
     * @return void
141
     */
142
    public function onFinish()
143
    {
144
        $this->client = null;
145
    }
146
147
    /**
148
     * Called when the worker is going to shutdown.
149
     * @return boolean Ready to shutdown?
150
     */
151
    public function gracefulShutdown()
152
    {
153
        return true;
154
    }
155
156
    /**
157
     * Get cookie by name
158
     * @param  string $name Name of cookie
159
     * @return string       Contents
160
     */
161
    protected function getCookieStr($name)
162
    {
163
        return \PHPDaemon\HTTPRequest\Generic::getString($this->attrs->cookie[$name]);
164
    }
165
166
    /**
167
     * Set session state
168
     * @param  mixed $var
169
     * @return void
170
     */
171
    protected function setSessionState($var)
172
    {
173
        $this->attrs->session = $var;
174
    }
175
176
    /**
177
     * Get session state
178
     * @return mixed
179
     */
180
    protected function getSessionState()
181
    {
182
        return $this->attrs->session;
183
    }
184
}
185