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 Inserter implements LoggerAwareInterface |
||
10 | { |
||
11 | /** |
||
12 | * @var \tomzx\IRCStats\DatabaseProxy |
||
13 | */ |
||
14 | protected $databaseProxy; |
||
15 | /** |
||
16 | * @var \Psr\Log\LoggerInterface |
||
17 | */ |
||
18 | protected $logger; |
||
19 | |||
20 | /** |
||
21 | * @param \tomzx\IRCStats\DatabaseProxy $databaseProxy |
||
22 | */ |
||
23 | public function __construct(DatabaseProxy $databaseProxy) |
||
28 | |||
29 | /** |
||
30 | * @return \Illuminate\Database\Connection |
||
31 | */ |
||
32 | protected function getDatabase() |
||
36 | |||
37 | /** |
||
38 | * @param \Psr\Log\LoggerInterface $logger |
||
39 | * @return void |
||
40 | */ |
||
41 | public function setLogger(LoggerInterface $logger) |
||
45 | |||
46 | |||
47 | public function insert($server, $channel, $nick, $message, $timestamp) |
||
54 | |||
55 | View Code Duplication | protected function insertServer($server) |
|
67 | |||
68 | View Code Duplication | protected function insertChannel($targetNetwork, $channel) |
|
81 | |||
82 | View Code Duplication | protected function insertNick($targetNetwork, $nick) |
|
95 | |||
96 | public function insertLog($targetChannel, $targetNick, $timestamp, $message) |
||
106 | } |
||
107 |
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.