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 |
||
10 | class RotatingFileHandler implements HandlerInterface |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | private $filename; |
||
16 | /** |
||
17 | * @var \DateInterval |
||
18 | */ |
||
19 | private $rotateEvery; |
||
20 | /** |
||
21 | * @var DateTimeImmutable |
||
22 | */ |
||
23 | private $rotationStartedAt; |
||
24 | /** |
||
25 | * @var FormatterInterface |
||
26 | */ |
||
27 | private $formatter; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $defaultCurrentTime = 'now'; |
||
33 | |||
34 | public function __construct(FormatterInterface $formatter, string $filename, \DateInterval $rotateEvery = null) |
||
41 | 3 | ||
42 | View Code Duplication | public function add(string $name, string $level, $message, array $context = []): void |
|
50 | |||
51 | /** |
||
52 | 2 | * Return the file name appending the $this->rotationStartedAt. |
|
53 | */ |
||
54 | private function getRotatedFilename(): string |
||
66 | 3 | ||
67 | /** |
||
68 | * Return a current DateTime. Extracted for be easily tested. |
||
69 | 3 | */ |
|
70 | 3 | private function getCurrentDateTime(): DateTimeImmutable |
|
74 | } |
||
75 |
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.