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 namespace Cornford\Bencoded; |
||
6 | class Decoder implements DecodableInterface |
||
7 | { |
||
8 | const TYPE_END = 0; |
||
9 | const TYPE_DICTIONARY = 1; |
||
10 | const TYPE_INTEGER = 2; |
||
11 | const TYPE_LIST = 3; |
||
12 | const TYPE_STRING = 4; |
||
13 | |||
14 | const DELIMITER_END = 'e'; |
||
15 | const DELIMITER_DICTIONARY = 'd'; |
||
16 | const DELIMITER_INTEGER = 'i'; |
||
17 | const DELIMITER_LIST = 'l'; |
||
18 | const DELIMITER_STRING = '[0-9]'; |
||
19 | |||
20 | const SEPARATOR = ':'; |
||
21 | |||
22 | /** |
||
23 | * Content. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | private $content; |
||
28 | |||
29 | /** |
||
30 | * Current pointer position. |
||
31 | * |
||
32 | * @var integer |
||
33 | */ |
||
34 | private $pointer = 0; |
||
35 | |||
36 | /** |
||
37 | * BDecode content. |
||
38 | * |
||
39 | * @param string $content |
||
40 | * |
||
41 | * @throws InvalidBencodedDelimiterException |
||
42 | * |
||
43 | * @return string |
||
44 | */ |
||
45 | public function decode($content) |
||
52 | |||
53 | /** |
||
54 | * Process from the current position. |
||
55 | * |
||
56 | * @throws InvalidBencodedDelimiterException |
||
57 | * |
||
58 | * @return mixed |
||
59 | */ |
||
60 | private function process() |
||
75 | |||
76 | /** |
||
77 | * Get type of the value at the current pointer. |
||
78 | * |
||
79 | * @throws InvalidBencodedDelimiterException |
||
80 | * |
||
81 | * @return int |
||
82 | */ |
||
83 | private function getCurrentType() |
||
103 | |||
104 | /** |
||
105 | * Decode string. |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | private function decodeString() |
||
119 | |||
120 | /** |
||
121 | * Decode integer. |
||
122 | * |
||
123 | * @return int|float |
||
124 | */ |
||
125 | private function decodeInteger() |
||
142 | |||
143 | /** |
||
144 | * BDecode dictionary. |
||
145 | * |
||
146 | * @throws InvalidBencodedDelimiterException |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | View Code Duplication | private function decodeDictionary() |
|
165 | |||
166 | /** |
||
167 | * Decode list. |
||
168 | * |
||
169 | * @throws InvalidBencodedDelimiterException |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | View Code Duplication | private function decodeList() |
|
187 | } |
||
188 |
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.