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 |
||
19 | class SignedShortReader |
||
20 | { |
||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | protected static $endianess; |
||
25 | |||
26 | /** |
||
27 | * @var StreamInterface |
||
28 | */ |
||
29 | protected $stream; |
||
30 | |||
31 | /** |
||
32 | * @var int |
||
33 | */ |
||
34 | protected $endian; |
||
35 | |||
36 | /** |
||
37 | * Create signed short (16-bit integer) reader object |
||
38 | * |
||
39 | * @throws Exception\InvalidArgumentException An exception will be thrown for non-readable streams or an invalid |
||
40 | * endian value |
||
41 | * |
||
42 | * @param StreamInterface $stream |
||
43 | * @param int $endian |
||
44 | */ |
||
45 | View Code Duplication | public function __construct(StreamInterface $stream, $endian) |
|
58 | |||
59 | /** |
||
60 | * Get stream |
||
61 | * |
||
62 | * @return StreamInterface |
||
63 | */ |
||
64 | public function getStream() |
||
68 | |||
69 | /** |
||
70 | * Get endian |
||
71 | * |
||
72 | * @return int |
||
73 | */ |
||
74 | public function getEndian() |
||
78 | |||
79 | /** |
||
80 | * Get endianess |
||
81 | * |
||
82 | * @return int |
||
83 | */ |
||
84 | protected function getEndianess() |
||
97 | |||
98 | /** |
||
99 | * Read string data from the stream |
||
100 | * |
||
101 | * @throws Exception\IOException An exception will be thrown for invalid stream resources or when the data could |
||
102 | * not be read |
||
103 | * |
||
104 | * @return int |
||
105 | */ |
||
106 | public function read() |
||
123 | } |
||
124 |
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.