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 |
||
23 | class Redis extends BaseEventEmitter implements RedisInterface |
||
24 | { |
||
25 | use LoopAwareTrait; |
||
26 | use Command\Compose\ApiChannelTrait; |
||
27 | use Command\Compose\ApiClusterTrait; |
||
28 | use Command\Compose\ApiConnTrait; |
||
29 | use Command\Compose\ApiCoreTrait; |
||
30 | use Command\Compose\ApiGeospatialTrait; |
||
31 | use Command\Compose\ApiHyperLogTrait; |
||
32 | use Command\Compose\ApiKeyValTrait; |
||
33 | use Command\Compose\ApiListTrait; |
||
34 | use Command\Compose\ApiSetTrait; |
||
35 | use Command\Compose\ApiSetHashTrait; |
||
36 | use Command\Compose\ApiSetSortedTrait; |
||
37 | use Command\Compose\ApiTransactionTrait; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $endpoint; |
||
43 | |||
44 | /** |
||
45 | * @var SocketInterface |
||
46 | */ |
||
47 | protected $stream; |
||
48 | |||
49 | /** |
||
50 | * @var DriverInterface |
||
51 | */ |
||
52 | protected $driver; |
||
53 | |||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $isConnected; |
||
58 | |||
59 | /** |
||
60 | * @var bool |
||
61 | */ |
||
62 | protected $isBeingDisconnected; |
||
63 | |||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | private $reqs; |
||
68 | |||
69 | /** |
||
70 | * @param string $endpoint |
||
71 | * @param LoopInterface $loop |
||
72 | */ |
||
73 | public function __construct($endpoint, LoopInterface $loop) |
||
85 | |||
86 | /** |
||
87 | * |
||
88 | */ |
||
89 | public function __destruct() |
||
93 | |||
94 | /** |
||
95 | * @override |
||
96 | * @inheritDoc |
||
97 | */ |
||
98 | 76 | public function isStarted() |
|
102 | |||
103 | /** |
||
104 | * @override |
||
105 | * @inheritDoc |
||
106 | */ |
||
107 | 76 | public function isBusy() |
|
111 | |||
112 | /** |
||
113 | * @override |
||
114 | * @inheritDoc |
||
115 | */ |
||
116 | 76 | public function start() |
|
150 | |||
151 | /** |
||
152 | * @override |
||
153 | * @inheritDoc |
||
154 | */ |
||
155 | 76 | public function stop() |
|
181 | |||
182 | /** |
||
183 | * @override |
||
184 | * @inheritDoc |
||
185 | */ |
||
186 | public function end() |
||
197 | |||
198 | /** |
||
199 | * Dispatch Redis request. |
||
200 | * |
||
201 | * @param Request $command |
||
202 | * @return PromiseInterface |
||
203 | */ |
||
204 | 76 | protected function dispatch(Request $command) |
|
221 | |||
222 | /** |
||
223 | * @internal |
||
224 | */ |
||
225 | 76 | View Code Duplication | protected function handleStart() |
233 | |||
234 | /** |
||
235 | * @internal |
||
236 | */ |
||
237 | 76 | View Code Duplication | protected function handleStop() |
245 | |||
246 | /** |
||
247 | * @internal |
||
248 | * @param SocketInterface $stream |
||
249 | * @param string $chunk |
||
250 | */ |
||
251 | 76 | public function handleData($stream, $chunk) |
|
252 | { |
||
253 | try |
||
254 | { |
||
255 | 76 | $models = $this->driver->parseResponse($chunk); |
|
256 | } |
||
257 | catch (ParserException $error) |
||
258 | { |
||
259 | $this->emit('error', [ $this, $error ]); |
||
260 | $this->stop(); |
||
261 | return; |
||
262 | } |
||
263 | |||
264 | 76 | foreach ($models as $data) |
|
265 | { |
||
266 | try |
||
267 | { |
||
268 | 76 | $this->handleMessage($data); |
|
269 | } |
||
270 | catch (UnderflowException $error) |
||
271 | { |
||
272 | $this->emit('error', [ $this, $error ]); |
||
273 | $this->stop(); |
||
274 | 76 | return; |
|
275 | } |
||
276 | } |
||
277 | 76 | } |
|
278 | |||
279 | /** |
||
280 | * @internal |
||
281 | * @param ModelInterface $message |
||
282 | */ |
||
283 | 76 | protected function handleMessage(ModelInterface $message) |
|
306 | |||
307 | /** |
||
308 | * Create socket client with connection to Redis database. |
||
309 | * |
||
310 | * @param string $endpoint |
||
311 | * @return SocketInterface |
||
312 | * @throws ExecutionException |
||
313 | */ |
||
314 | 76 | protected function createClient($endpoint) |
|
329 | }; |
||
330 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.