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 |
||
15 | class SocketPair |
||
16 | { |
||
17 | const LEFT_SIDE = true; |
||
18 | const RIGHT_SIDE = false; |
||
19 | |||
20 | /** |
||
21 | * @var EventLoopInterface |
||
22 | */ |
||
23 | protected $loop; |
||
24 | |||
25 | /** |
||
26 | * @var Connection |
||
27 | */ |
||
28 | protected $connection; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $sockets; |
||
34 | |||
35 | /** |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $mode; |
||
39 | |||
40 | public function __construct(EventLoopInterface $loop) |
||
44 | |||
45 | public function create() |
||
53 | |||
54 | View Code Duplication | public function useLeft() : Connection |
|
65 | |||
66 | View Code Duplication | public function useRight() : Connection |
|
77 | |||
78 | public function shutdown() |
||
82 | |||
83 | public function getConnection() : Connection |
||
87 | } |
||
88 |
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.