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 |
||
18 | View Code Duplication | class UnsignedCharReader |
|
|
|||
19 | { |
||
20 | /** |
||
21 | * @var StreamInterface |
||
22 | */ |
||
23 | protected $stream; |
||
24 | |||
25 | /** |
||
26 | * Create unsigned char (8-bit integer) reader object |
||
27 | * |
||
28 | * @throws Exception\InvalidArgumentException An exception will be thrown for non-readable streams |
||
29 | * |
||
30 | * @param StreamInterface $stream |
||
31 | */ |
||
32 | public function __construct(StreamInterface $stream) |
||
40 | |||
41 | /** |
||
42 | * Get stream |
||
43 | * |
||
44 | * @return StreamInterface |
||
45 | */ |
||
46 | public function getStream() |
||
50 | |||
51 | /** |
||
52 | * Read string data from the stream |
||
53 | * |
||
54 | * @throws Exception\IOException An exception will be thrown for invalid stream resources or when the data could |
||
55 | * not be read |
||
56 | * |
||
57 | * @return int |
||
58 | */ |
||
59 | public function read() |
||
65 | } |
||
66 |
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.