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 LineFormatter implements FormatterInterface |
||
11 | { |
||
12 | const DEFAULT_MESSAGE_FORMAT = '[%datetime%] %channel%.%level_name%: %message% %context% %extras%'; |
||
13 | const DEFAULT_DATE_FORMAT = 'Y-m-d H:i:s'; |
||
14 | |||
15 | /** @var string */ |
||
16 | protected $messageFormat; |
||
17 | |||
18 | /** @var string */ |
||
19 | protected $dateFormat; |
||
20 | |||
21 | /** |
||
22 | * @param string $messageFormat |
||
23 | * @param string $dateFormat |
||
24 | */ |
||
25 | public function __construct($messageFormat = null, $dateFormat = null) |
||
30 | |||
31 | /** |
||
32 | * @return string |
||
33 | */ |
||
34 | public function getMessageFormat() |
||
38 | |||
39 | /** |
||
40 | * @return string |
||
41 | */ |
||
42 | public function getDateFormat() |
||
46 | |||
47 | /** |
||
48 | * @inheritdoc |
||
49 | */ |
||
50 | public function format(Record $record) |
||
80 | |||
81 | /** |
||
82 | * @todo use a minc helper class for string converting |
||
83 | * @param mixed $data |
||
84 | * @param bool $ignoreEmptyData |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | protected function convertToString($data, $ignoreEmptyData = false) |
||
108 | } |
||
109 |
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.