SocketServer   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 175
Duplicated Lines 18.29 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 21
lcom 2
cbo 6
dl 32
loc 175
ccs 52
cts 56
cp 0.9286
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A __destruct() 0 5 1
A setComponent() 0 4 2
A getComponent() 0 4 1
A handleConnect() 0 21 3
A handleDisconnect() 17 17 3
A handleData() 0 15 3
A handleError() 15 15 3
A close() 0 4 1
A start() 0 4 1
A stop() 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 Dazzle\Http\Socket;
3
4
use Dazzle\Socket\SocketInterface;
5
use Dazzle\Socket\SocketListenerInterface;
6
use Dazzle\Http\Null\NullServer;
7
use Dazzle\Http\NetworkComponentAwareInterface;
8
use Dazzle\Http\NetworkConnection;
9
use Dazzle\Http\NetworkMessage;
10
use Dazzle\Http\NetworkComponentInterface;
11
use Error;
12
use Exception;
13
14
class SocketServer implements SocketServerInterface, NetworkComponentAwareInterface
15
{
16
    /**
17
     * @var SocketListenerInterface
18
     */
19
    protected $socket;
20
21
    /**
22
     * @var NetworkComponentInterface
23
     */
24
    protected $component;
25
26
    /**
27
     * @param NetworkComponentInterface $component
28
     * @param SocketListenerInterface $socket
29
     */
30 55
    public function __construct(SocketListenerInterface $socket, NetworkComponentInterface $component = null)
31
    {
32
33 55
        $this->socket = $socket;
34 55
        $this->component = $component === null ? new NullServer() : $component;
35
36 55
        $socket->on('connect', [ $this, 'handleConnect' ]);
37 55
    }
38
39
    /**
40
     *
41
     */
42 15
    public function __destruct()
43
    {
44 15
        unset($this->socket);
45 15
        unset($this->component);
46 15
    }
47
48
    /**
49
     * @override
50
     * @inheritDoc
51
     */
52 1
    public function start()
53
    {
54 1
        $this->socket->start();
55 1
    }
56
57
    /**
58
     * @override
59
     * @inheritDoc
60
     */
61 3
    public function stop()
62
    {
63 3
        $this->socket->close();
64 3
    }
65
66
    /**
67
     * @override
68
     * @inheritDoc
69
     */
70 24
    public function setComponent(NetworkComponentInterface $component = null)
71
    {
72 24
        $this->component = $component === null ? new NullServer() : $component;
73 24
    }
74
75
    /**
76
     * @override
77
     * @inheritDoc
78
     */
79 3
    public function getComponent()
80
    {
81 3
        return $this->component;
82
    }
83
84
    /**
85
     * Handler triggered when a new connection is received from SocketListener.
86
     *
87
     * @param SocketListenerInterface $server
88
     * @param SocketInterface $socket
89
     */
90 7
    public function handleConnect($server, $socket)
91
    {
92 7
        $socket->conn = new NetworkConnection($socket);
0 ignored issues
show
Bug introduced by
Accessing conn on the interface Dazzle\Socket\SocketInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
93
94
        try
95
        {
96 7
            $this->component->handleConnect($socket->conn);
0 ignored issues
show
Bug introduced by
Accessing conn on the interface Dazzle\Socket\SocketInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
97
98 6
            $socket->on('data',  [ $this, 'handleData' ]);
99 6
            $socket->on('error', [ $this, 'handleError' ]);
100 6
            $socket->on('close', [ $this, 'handleDisconnect' ]);
101
        }
102 1
        catch (Error $ex)
103
        {
104
            $this->close($socket);
105
        }
106 1
        catch (Exception $ex)
107
        {
108 1
            $this->close($socket);
109
        }
110 7
    }
111
112
    /**
113
     * Handler triggered when an existing connection is being closed.
114
     *
115
     * @param SocketInterface $socket
116
     */
117 6 View Code Duplication
    public function handleDisconnect($socket)
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...
118
    {
119
        try
120
        {
121 6
            $this->component->handleDisconnect($socket->conn);
0 ignored issues
show
Bug introduced by
Accessing conn on the interface Dazzle\Socket\SocketInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
122
        }
123 1
        catch (Error $ex)
124
        {
125
            $this->handleError($socket, $ex);
126
        }
127 1
        catch (Exception $ex)
128
        {
129 1
            $this->handleError($socket, $ex);
130
        }
131
132 6
        unset($socket->conn);
133 6
    }
134
135
    /**
136
     * Handler triggered when a new data is received from existing connection.
137
     *
138
     * @param SocketInterface $socket
139
     * @param mixed $data
140
     */
141 6
    public function handleData($socket, $data)
142
    {
143
        try
144
        {
145 6
            $this->component->handleMessage($socket->conn, new NetworkMessage($data));
0 ignored issues
show
Bug introduced by
Accessing conn on the interface Dazzle\Socket\SocketInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
146
        }
147 1
        catch (Error $ex)
148
        {
149
            $this->handleError($socket, $ex);
150
        }
151 1
        catch (Exception $ex)
152
        {
153 1
            $this->handleError($socket, $ex);
154
        }
155 6
    }
156
157
    /**
158
     * Handler triggered when an error has occured during doing operation on existing connection.
159
     *
160
     * @param SocketInterface $socket
161
     * @param Error|Exception $ex
162
     */
163 2 View Code Duplication
    public function handleError($socket, $ex)
0 ignored issues
show
Unused Code introduced by
The parameter $ex is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
164
    {
165
        try
166
        {
167 2
            $this->component->handleError($socket->conn, $ex);
0 ignored issues
show
Bug introduced by
Accessing conn on the interface Dazzle\Socket\SocketInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
168
        }
169 1
        catch (Error $ex)
170
        {
171
            $this->close($socket);
172
        }
173 1
        catch (Exception $ex)
174
        {
175 1
            $this->close($socket);
176
        }
177 2
    }
178
179
    /**
180
     * Close socket.
181
     *
182
     * @param SocketInterface $socket
183
     */
184 1
    protected function close(SocketInterface $socket)
185
    {
186 1
        $socket->close();
187 1
    }
188
}
189