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 |
||
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) |
|
38 | |||
39 | /** |
||
40 | * |
||
41 | */ |
||
42 | 15 | public function __destruct() |
|
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) |
|
74 | |||
75 | /** |
||
76 | * @override |
||
77 | * @inheritDoc |
||
78 | */ |
||
79 | 3 | public function getComponent() |
|
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) |
|
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) |
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) |
|
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) |
178 | |||
179 | /** |
||
180 | * Close socket. |
||
181 | * |
||
182 | * @param SocketInterface $socket |
||
183 | */ |
||
184 | 1 | protected function close(SocketInterface $socket) |
|
188 | } |
||
189 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: