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 Stream extends StreamSeeker implements StreamInterface |
||
11 | { |
||
12 | /** |
||
13 | * @var bool |
||
14 | */ |
||
15 | protected $readable; |
||
16 | |||
17 | /** |
||
18 | * @var bool |
||
19 | */ |
||
20 | protected $writable; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | protected $bufferSize; |
||
26 | |||
27 | /** |
||
28 | * @param resource $resource |
||
29 | * @param bool $autoClose |
||
30 | * @throws InvalidArgumentException |
||
31 | */ |
||
32 | 38 | public function __construct($resource, $autoClose = true) |
|
40 | |||
41 | /** |
||
42 | * |
||
43 | */ |
||
44 | 29 | public function __destruct() |
|
52 | |||
53 | /** |
||
54 | * @override |
||
55 | * @inheritDoc |
||
56 | */ |
||
57 | 2 | public function isReadable() |
|
61 | |||
62 | /** |
||
63 | * @override |
||
64 | * @inheritDoc |
||
65 | */ |
||
66 | 2 | public function isWritable() |
|
70 | |||
71 | /** |
||
72 | * @override |
||
73 | * @inheritDoc |
||
74 | */ |
||
75 | 1 | public function setBufferSize($bufferSize) |
|
79 | |||
80 | /** |
||
81 | * @override |
||
82 | * @inheritDoc |
||
83 | */ |
||
84 | 3 | public function getBufferSize() |
|
88 | |||
89 | /** |
||
90 | * @override |
||
91 | * @inheritDoc |
||
92 | */ |
||
93 | 2 | View Code Duplication | public function write($text = '') |
116 | |||
117 | /** |
||
118 | * @override |
||
119 | * @inheritDoc |
||
120 | */ |
||
121 | 2 | View Code Duplication | public function read($length = null) |
155 | |||
156 | /** |
||
157 | * @override |
||
158 | * @inheritDoc |
||
159 | */ |
||
160 | 5 | View Code Duplication | public function close() |
175 | } |
||
176 |
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.