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 |
||
10 | abstract class SecureSocket implements SocketStream |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * The socket handle |
||
15 | * |
||
16 | * @var resource |
||
17 | */ |
||
18 | protected $handle; |
||
19 | |||
20 | /** |
||
21 | * The socket endpoint |
||
22 | * |
||
23 | * @var Endpoint |
||
24 | */ |
||
25 | protected $endpoint; |
||
26 | |||
27 | /** |
||
28 | * The stream context |
||
29 | * |
||
30 | * @var resource |
||
31 | */ |
||
32 | private $streamContext; |
||
33 | |||
34 | /** |
||
35 | * Create a new socket |
||
36 | * |
||
37 | * @param Endpoint $endpoint |
||
38 | * The endpoint for the socket |
||
39 | */ |
||
40 | 11 | public function __construct(Endpoint $endpoint) |
|
45 | |||
46 | /** |
||
47 | * |
||
48 | * {@inheritdoc} |
||
49 | * @see \Generics\Streams\InputStream::read() |
||
50 | */ |
||
51 | 9 | public function read($length = 1, $offset = null): string |
|
55 | |||
56 | /** |
||
57 | * |
||
58 | * {@inheritdoc} |
||
59 | * @see \Generics\Streams\Stream::isOpen() |
||
60 | */ |
||
61 | public function isOpen(): bool |
||
65 | |||
66 | /** |
||
67 | * |
||
68 | * {@inheritdoc} |
||
69 | * @see \Generics\Streams\OutputStream::flush() |
||
70 | */ |
||
71 | public function flush() |
||
75 | |||
76 | /** |
||
77 | * |
||
78 | * {@inheritdoc} |
||
79 | * @see \Generics\Streams\Stream::ready() |
||
80 | */ |
||
81 | 10 | View Code Duplication | public function ready(): bool |
109 | |||
110 | /** |
||
111 | * |
||
112 | * {@inheritdoc} |
||
113 | * @see Countable::count() |
||
114 | */ |
||
115 | public function count() |
||
126 | |||
127 | /** |
||
128 | * |
||
129 | * {@inheritdoc} |
||
130 | * @see \Generics\Resettable::reset() |
||
131 | */ |
||
132 | View Code Duplication | public function reset() |
|
141 | |||
142 | /** |
||
143 | * |
||
144 | * {@inheritdoc} |
||
145 | * @see \Generics\Streams\Stream::close() |
||
146 | */ |
||
147 | 6 | public function close() |
|
154 | |||
155 | /** |
||
156 | * |
||
157 | * {@inheritdoc} |
||
158 | * @see \Generics\Streams\OutputStream::write() |
||
159 | */ |
||
160 | 11 | public function write($buffer) |
|
178 | |||
179 | /** |
||
180 | * |
||
181 | * {@inheritdoc} |
||
182 | * @see \Generics\Streams\OutputStream::isWriteable() |
||
183 | */ |
||
184 | 11 | View Code Duplication | public function isWriteable(): bool |
212 | |||
213 | 11 | private function open() |
|
224 | |||
225 | 11 | private function prepareStreamContext() |
|
243 | |||
244 | /** |
||
245 | * Retrieve end point object |
||
246 | * |
||
247 | * @return Endpoint |
||
248 | */ |
||
249 | 11 | public function getEndPoint(): Endpoint |
|
253 | } |