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 |
||
17 | class LazyWrite extends Decorator |
||
18 | { |
||
19 | /** |
||
20 | * Write stack. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $stack = []; |
||
25 | |||
26 | /** |
||
27 | * Save messages in stack. |
||
28 | * |
||
29 | * @var bool |
||
30 | */ |
||
31 | protected $lazy_write = true; |
||
32 | |||
33 | /** |
||
34 | * @param array|string $messages |
||
35 | * @param bool|false $newline |
||
36 | * @param int $type |
||
37 | */ |
||
38 | 30 | public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL) |
|
49 | |||
50 | /** |
||
51 | * @param array|string $messages |
||
52 | * @param int $type |
||
53 | */ |
||
54 | 15 | public function writeln($messages, $type = self::OUTPUT_NORMAL) |
|
65 | |||
66 | /** |
||
67 | * Write all messages from stack. |
||
68 | */ |
||
69 | 18 | public function writeAll() |
|
75 | |||
76 | /** |
||
77 | * @return bool |
||
78 | */ |
||
79 | 1 | public function isLazyWrite() |
|
83 | |||
84 | /** |
||
85 | * @param bool $lazy_write |
||
86 | */ |
||
87 | 10 | public function setLazyWrite($lazy_write) |
|
91 | } |
||
92 |
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.