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 |
||
9 | View Code Duplication | class Int32 implements TypeInterface |
|
|
|||
10 | { |
||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | private $endianBig = 'N'; |
||
15 | |||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | private $endianLittle = 'V'; |
||
20 | |||
21 | /** |
||
22 | * Returns an Unsigned 32-bit Integer |
||
23 | * |
||
24 | * @param \PhpBinaryReader\BinaryReader $br |
||
25 | * @param null $length |
||
26 | * @return int |
||
27 | * @throws \OutOfBoundsException |
||
28 | */ |
||
29 | 26 | public function read(BinaryReader &$br, $length = null) |
|
30 | { |
||
31 | 26 | if (!$br->canReadBytes(4)) { |
|
32 | 4 | throw new \OutOfBoundsException('Cannot read 32-bit int, it exceeds the boundary of the file'); |
|
33 | } |
||
34 | |||
35 | 22 | $endian = $br->getEndian() == Endian::ENDIAN_BIG ? $this->endianBig : $this->endianLittle; |
|
36 | 22 | $segment = $br->readFromHandle(4); |
|
37 | |||
38 | 22 | $data = unpack($endian, $segment); |
|
39 | 22 | $data = $data[1]; |
|
40 | |||
41 | 22 | if ($br->getCurrentBit() != 0) { |
|
42 | 4 | $data = $this->bitReader($br, $data); |
|
43 | } |
||
44 | |||
45 | 22 | return $data; |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * Returns a Signed 32-Bit Integer |
||
50 | * |
||
51 | * @param \PhpBinaryReader\BinaryReader $br |
||
52 | * @return int |
||
53 | */ |
||
54 | 12 | public function readSigned(&$br) |
|
72 | |||
73 | /** |
||
74 | * @param \PhpBinaryReader\BinaryReader $br |
||
75 | * @param int $data |
||
76 | * @return int |
||
77 | */ |
||
78 | 4 | private function bitReader(&$br, $data) |
|
90 | |||
91 | /** |
||
92 | * @param string $endianBig |
||
93 | */ |
||
94 | 13 | public function setEndianBig($endianBig) |
|
98 | |||
99 | /** |
||
100 | * @return string |
||
101 | */ |
||
102 | 1 | public function getEndianBig() |
|
106 | |||
107 | /** |
||
108 | * @param string $endianLittle |
||
109 | */ |
||
110 | 13 | public function setEndianLittle($endianLittle) |
|
114 | |||
115 | /** |
||
116 | * @return string |
||
117 | */ |
||
118 | 1 | public function getEndianLittle() |
|
122 | } |
||
123 |
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.