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 |
||
9 | class Emitter implements EmitterInterface |
||
10 | { |
||
11 | /** |
||
12 | * Dispatcher driver |
||
13 | * |
||
14 | * @var DriverInterface |
||
15 | */ |
||
16 | private $driver; |
||
17 | |||
18 | /** |
||
19 | * Logger |
||
20 | * |
||
21 | * @var LoggerInterface |
||
22 | */ |
||
23 | private $logger; |
||
24 | |||
25 | /** |
||
26 | * Create new Dispatcher |
||
27 | * |
||
28 | * @param DriverInterface $driver |
||
29 | * @param LoggerInterface $logger |
||
30 | */ |
||
31 | public function __construct(DriverInterface $driver, LoggerInterface $logger = null) |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | View Code Duplication | public function emit(MessageInterface $message) |
|
51 | |||
52 | /** |
||
53 | * Serialize message to logger context |
||
54 | * |
||
55 | * @param MessageInterface $message |
||
56 | * |
||
57 | * @return array |
||
58 | */ |
||
59 | View Code Duplication | private function messageLoggerContext(MessageInterface $message) |
|
68 | |||
69 | /** |
||
70 | * Interal log method wrapper |
||
71 | * |
||
72 | * @param string $level |
||
73 | * @param string $message |
||
74 | * @param array $context |
||
75 | * |
||
76 | * @return void |
||
77 | */ |
||
78 | private function log($level, $message, array $context = array()) |
||
84 | } |
||
85 |
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.