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:
Complex classes like Redis often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Redis, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Redis extends BaseEventEmitter implements RedisInterface |
||
26 | { |
||
27 | use LoopAwareTrait; |
||
28 | use Command\Compose\ApiChannelTrait; |
||
29 | use Command\Compose\ApiClusterTrait; |
||
30 | use Command\Compose\ApiConnTrait; |
||
31 | use Command\Compose\ApiCoreTrait; |
||
32 | use Command\Compose\ApiGeospatialTrait; |
||
33 | use Command\Compose\ApiHyperLogTrait; |
||
34 | use Command\Compose\ApiKeyValTrait; |
||
35 | use Command\Compose\ApiListTrait; |
||
36 | use Command\Compose\ApiSetTrait; |
||
37 | use Command\Compose\ApiSetHashTrait; |
||
38 | use Command\Compose\ApiSetSortedTrait; |
||
39 | use Command\Compose\ApiTransactionTrait; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $endpoint; |
||
45 | |||
46 | /** |
||
47 | * @var SocketInterface |
||
48 | */ |
||
49 | protected $stream; |
||
50 | |||
51 | /** |
||
52 | * @var DriverInterface |
||
53 | */ |
||
54 | protected $driver; |
||
55 | |||
56 | /** |
||
57 | * @var bool |
||
58 | */ |
||
59 | protected $isConnected; |
||
60 | |||
61 | /** |
||
62 | * @var bool |
||
63 | */ |
||
64 | protected $isBeingDisconnected; |
||
65 | |||
66 | /** |
||
67 | * @var PromiseInterface|null; |
||
68 | */ |
||
69 | protected $endPromise; |
||
70 | |||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | private $reqs; |
||
75 | |||
76 | /** |
||
77 | * @param string $endpoint |
||
78 | * @param LoopInterface $loop |
||
79 | */ |
||
80 | public function __construct($endpoint, LoopInterface $loop) |
||
93 | |||
94 | /** |
||
95 | * |
||
96 | */ |
||
97 | public function __destruct() |
||
98 | { |
||
99 | $this->stop(); |
||
100 | parent::__destruct(); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @override |
||
105 | * @inheritDoc |
||
106 | */ |
||
107 | public function isPaused() |
||
111 | |||
112 | /** |
||
113 | * @override |
||
114 | * @inheritDoc |
||
115 | */ |
||
116 | public function pause() |
||
123 | |||
124 | /** |
||
125 | * @override |
||
126 | * @inheritDoc |
||
127 | */ |
||
128 | public function resume() |
||
135 | |||
136 | /** |
||
137 | * @override |
||
138 | * @inheritDoc |
||
139 | */ |
||
140 | 138 | public function isStarted() |
|
144 | |||
145 | /** |
||
146 | * @override |
||
147 | * @inheritDoc |
||
148 | */ |
||
149 | 138 | public function isBusy() |
|
153 | |||
154 | /** |
||
155 | * @override |
||
156 | * @inheritDoc |
||
157 | */ |
||
158 | 138 | public function start() |
|
190 | |||
191 | /** |
||
192 | * @override |
||
193 | * @inheritDoc |
||
194 | */ |
||
195 | 138 | public function stop() |
|
226 | |||
227 | /** |
||
228 | * @override |
||
229 | * @inheritDoc |
||
230 | */ |
||
231 | public function end() |
||
252 | |||
253 | /** |
||
254 | * Dispatch Redis request. |
||
255 | * |
||
256 | * @param Request $command |
||
257 | * @return PromiseInterface |
||
258 | */ |
||
259 | 138 | protected function dispatch(Request $command) |
|
276 | |||
277 | /** |
||
278 | * @internal |
||
279 | */ |
||
280 | 138 | View Code Duplication | protected function handleStart() |
288 | |||
289 | /** |
||
290 | * @internal |
||
291 | */ |
||
292 | 138 | View Code Duplication | protected function handleStop() |
300 | |||
301 | /** |
||
302 | * @internal |
||
303 | * @param SocketInterface $stream |
||
304 | * @param string $chunk |
||
305 | */ |
||
306 | 138 | public function handleData($stream, $chunk) |
|
333 | |||
334 | /** |
||
335 | * @internal |
||
336 | * @param ModelInterface $message |
||
337 | */ |
||
338 | 138 | protected function handleMessage(ModelInterface $message) |
|
361 | |||
362 | /** |
||
363 | * Create socket client with connection to Redis database. |
||
364 | * |
||
365 | * @param string $endpoint |
||
366 | * @return SocketInterface |
||
367 | * @throws ExecutionException |
||
368 | */ |
||
369 | 138 | protected function createClient($endpoint) |
|
384 | }; |
||
385 |
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.