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 declare(strict_types=1); |
||
11 | final class LogglyBulkLogger extends AbstractLogglyLogger |
||
12 | { |
||
13 | const LF = "\r\n"; |
||
14 | const MAX_BODY_LENGTH = 5242880; |
||
15 | const MAX_LINE_LENGTH = 1048576; |
||
16 | |||
17 | /** |
||
18 | * @var LoopInterface |
||
19 | */ |
||
20 | private $loop; |
||
21 | |||
22 | /** |
||
23 | * @var Client |
||
24 | */ |
||
25 | private $httpClient; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $token; |
||
31 | |||
32 | /** |
||
33 | * @var float |
||
34 | */ |
||
35 | private $timeout; |
||
36 | |||
37 | /** |
||
38 | * @var string[] |
||
39 | */ |
||
40 | private $buffer = []; |
||
41 | |||
42 | /** |
||
43 | * @var int |
||
44 | */ |
||
45 | private $bufferSize = 0; |
||
46 | |||
47 | /** |
||
48 | * @var TimerInterface |
||
49 | */ |
||
50 | private $timer; |
||
51 | |||
52 | View Code Duplication | public static function create(LoopInterface $loop, string $token, float $timeout = 5): self |
|
62 | |||
63 | 12 | public static function createFromHttpClient( |
|
71 | |||
72 | 12 | private function __construct(LoopInterface $loop, Client $httpClient, string $token, float $timeout) |
|
79 | |||
80 | 12 | protected function send(string $data) |
|
95 | |||
96 | 12 | private function ensureTimer() |
|
107 | |||
108 | 12 | private function sendBulk() |
|
130 | } |
||
131 |
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.