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 |
||
24 | class TraceableNetwork extends AbstractNetworkDecorator implements NetworkDecoratorInterface |
||
25 | { |
||
26 | const TYPE_DATA = 'data'; |
||
27 | const TYPE_CONNECT = 'connect'; |
||
28 | const TYPE_DISCONNECT = 'disconnect'; |
||
29 | const TYPE_BEGIN_GROUP = 'begin.group'; |
||
30 | const TYPE_END_GROUP = 'end.group'; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $actions; |
||
36 | |||
37 | /** |
||
38 | * @var LoggerInterface |
||
39 | */ |
||
40 | private $logger; |
||
41 | |||
42 | /** |
||
43 | * TraceableNetwork constructor. |
||
44 | * |
||
45 | * @param NetworkInterface $network |
||
46 | * @param LoggerInterface $logger |
||
47 | */ |
||
48 | 2 | public function __construct(NetworkInterface $network, LoggerInterface $logger) |
|
63 | |||
64 | /** |
||
65 | * Wrap the creation of the callback |
||
66 | * |
||
67 | * @param string $type |
||
68 | * @return \Closure |
||
69 | */ |
||
70 | private function trace(string $type) : \Closure |
||
86 | |||
87 | /** |
||
88 | * @param array $args |
||
89 | * @param string $type |
||
90 | */ |
||
91 | private function traceData(array $args, string $type) |
||
109 | |||
110 | /** |
||
111 | * @param array $args |
||
112 | * @param string $type |
||
113 | */ |
||
114 | private function traceAction(array $args, string $type) |
||
127 | } |
||
128 |
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.